PageRenderTime 313ms CodeModel.GetById 110ms app.highlight 4ms RepoModel.GetById 198ms app.codeStats 0ms

/tests/regressiontests/test_utils/tests_25.py

https://code.google.com/p/mango-py/
Python | 41 lines | 29 code | 12 blank | 0 comment | 0 complexity | c3b06b1bd875e33a90a0afecb9179832 MD5 | raw file
 1from __future__ import with_statement
 2
 3from django.test import TestCase
 4
 5from models import Person
 6
 7
 8class AssertNumQueriesContextManagerTests(TestCase):
 9    def test_simple(self):
10        with self.assertNumQueries(0):
11            pass
12
13        with self.assertNumQueries(1):
14            Person.objects.count()
15
16        with self.assertNumQueries(2):
17            Person.objects.count()
18            Person.objects.count()
19
20    def test_failure(self):
21        with self.assertRaises(AssertionError) as exc_info:
22            with self.assertNumQueries(2):
23                Person.objects.count()
24        self.assertIn("1 queries executed, 2 expected", str(exc_info.exception))
25
26        with self.assertRaises(TypeError):
27            with self.assertNumQueries(4000):
28                raise TypeError
29
30    def test_with_client(self):
31        person = Person.objects.create(name="test")
32
33        with self.assertNumQueries(1):
34            self.client.get("/test_utils/get_person/%s/" % person.pk)
35
36        with self.assertNumQueries(1):
37            self.client.get("/test_utils/get_person/%s/" % person.pk)
38
39        with self.assertNumQueries(2):
40            self.client.get("/test_utils/get_person/%s/" % person.pk)
41            self.client.get("/test_utils/get_person/%s/" % person.pk)