summaryrefslogtreecommitdiff
path: root/tests/regressiontests/test_utils/python_25.py
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2010-10-12 03:33:19 +0000
committerAlex Gaynor <alex.gaynor@gmail.com>2010-10-12 03:33:19 +0000
commit5506653b777d7547d21ea2d74e9588fb94314b77 (patch)
tree34b6e6c3de02848bf793bd3088681ae29c44238d /tests/regressiontests/test_utils/python_25.py
parentceef628c192127d5e231613bc280d68dc0927fa3 (diff)
Fixed #5416 -- Added TestCase.assertNumQueries, which tests that a given function executes the correct number of queries.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14183 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/test_utils/python_25.py')
-rw-r--r--tests/regressiontests/test_utils/python_25.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/regressiontests/test_utils/python_25.py b/tests/regressiontests/test_utils/python_25.py
new file mode 100644
index 0000000000..a1e8a94d1e
--- /dev/null
+++ b/tests/regressiontests/test_utils/python_25.py
@@ -0,0 +1,30 @@
+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