2010-10-12 11:33:19 +08:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from models import Person
|
|
|
|
|
|
|
|
|
2011-01-20 12:47:47 +08:00
|
|
|
class AssertNumQueriesContextManagerTests(TestCase):
|
2010-10-12 11:33:19 +08:00
|
|
|
def test_simple(self):
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertNumQueries(1):
|
2010-10-17 09:49:36 +08:00
|
|
|
Person.objects.count()
|
2010-10-12 11:33:19 +08:00
|
|
|
|
|
|
|
with self.assertNumQueries(2):
|
2010-10-17 09:49:36 +08:00
|
|
|
Person.objects.count()
|
|
|
|
Person.objects.count()
|
2010-10-12 11:33:19 +08:00
|
|
|
|
|
|
|
def test_failure(self):
|
|
|
|
with self.assertRaises(AssertionError) as exc_info:
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
Person.objects.count()
|
2010-10-30 21:04:02 +08:00
|
|
|
self.assertIn("1 queries executed, 2 expected", str(exc_info.exception))
|
2010-10-12 11:33:19 +08:00
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
with self.assertNumQueries(4000):
|
|
|
|
raise TypeError
|
2011-01-20 12:47:47 +08:00
|
|
|
|
|
|
|
def test_with_client(self):
|
|
|
|
person = Person.objects.create(name="test")
|
|
|
|
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
self.client.get("/test_utils/get_person/%s/" % person.pk)
|
|
|
|
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
self.client.get("/test_utils/get_person/%s/" % person.pk)
|
|
|
|
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
self.client.get("/test_utils/get_person/%s/" % person.pk)
|
|
|
|
self.client.get("/test_utils/get_person/%s/" % person.pk)
|