mirror of
https://github.com/django/django.git
synced 2024-12-25 18:46:22 +00:00
8308ad4f05
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15251 bcc190cf-cafb-0310-a4f2-bffc1f526a37
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
from __future__ import with_statement
|
|
|
|
from django.test import TestCase
|
|
|
|
from models import Person
|
|
|
|
|
|
class AssertNumQueriesContextManagerTests(TestCase):
|
|
def test_simple(self):
|
|
with self.assertNumQueries(0):
|
|
pass
|
|
|
|
with self.assertNumQueries(1):
|
|
Person.objects.count()
|
|
|
|
with self.assertNumQueries(2):
|
|
Person.objects.count()
|
|
Person.objects.count()
|
|
|
|
def test_failure(self):
|
|
with self.assertRaises(AssertionError) as exc_info:
|
|
with self.assertNumQueries(2):
|
|
Person.objects.count()
|
|
self.assertIn("1 queries executed, 2 expected", str(exc_info.exception))
|
|
|
|
with self.assertRaises(TypeError):
|
|
with self.assertNumQueries(4000):
|
|
raise TypeError
|
|
|
|
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)
|