mirror of
https://github.com/django/django.git
synced 2024-12-26 11:06:07 +00:00
5506653b77
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14183 bcc190cf-cafb-0310-a4f2-bffc1f526a37
31 lines
940 B
Python
31 lines
940 B
Python
from __future__ import with_statement
|
|
|
|
from django.test import TestCase
|
|
|
|
from models import Person
|
|
|
|
|
|
class AssertNumQueriesTests(TestCase):
|
|
def test_simple(self):
|
|
with self.assertNumQueries(0):
|
|
pass
|
|
|
|
with self.assertNumQueries(1):
|
|
# Guy who wrote Linux
|
|
Person.objects.create(name="Linus Torvalds")
|
|
|
|
with self.assertNumQueries(2):
|
|
# Guy who owns the bagel place I like
|
|
Person.objects.create(name="Uncle Ricky")
|
|
self.assertEqual(Person.objects.count(), 2)
|
|
|
|
def test_failure(self):
|
|
with self.assertRaises(AssertionError) as exc_info:
|
|
with self.assertNumQueries(2):
|
|
Person.objects.count()
|
|
self.assertEqual(str(exc_info.exception), "1 != 2 : 1 queries executed, 2 expected")
|
|
|
|
with self.assertRaises(TypeError):
|
|
with self.assertNumQueries(4000):
|
|
raise TypeError
|