Author: Alex
Date: 2011-01-19 22:47:47 -0600 (Wed, 19 Jan 2011)
New Revision: 15251

Added:
   django/trunk/tests/regressiontests/test_utils/urls.py
   django/trunk/tests/regressiontests/test_utils/views.py
Modified:
   django/trunk/django/test/testcases.py
   django/trunk/tests/regressiontests/test_utils/tests.py
   django/trunk/tests/regressiontests/test_utils/tests_25.py
   django/trunk/tests/urls.py
Log:
Fixed #14774 -- the test client and assertNumQueries didn't work well together. 
 Thanks to Jonas Obrist for the initial patch.

Modified: django/trunk/django/test/testcases.py
===================================================================
--- django/trunk/django/test/testcases.py       2011-01-20 01:00:47 UTC (rev 
15250)
+++ django/trunk/django/test/testcases.py       2011-01-20 04:47:47 UTC (rev 
15251)
@@ -6,8 +6,10 @@
 from django.conf import settings
 from django.core import mail
 from django.core.management import call_command
+from django.core.signals import request_started
 from django.core.urlresolvers import clear_url_caches
-from django.db import transaction, connection, connections, DEFAULT_DB_ALIAS
+from django.db import (transaction, connection, connections, DEFAULT_DB_ALIAS,
+    reset_queries)
 from django.http import QueryDict
 from django.test import _doctest as doctest
 from django.test.client import Client
@@ -220,10 +222,12 @@
         self.old_debug_cursor = self.connection.use_debug_cursor
         self.connection.use_debug_cursor = True
         self.starting_queries = len(self.connection.queries)
+        request_started.disconnect(reset_queries)
         return self
 
     def __exit__(self, exc_type, exc_value, traceback):
         self.connection.use_debug_cursor = self.old_debug_cursor
+        request_started.connect(reset_queries)
         if exc_type is not None:
             return
 

Modified: django/trunk/tests/regressiontests/test_utils/tests.py
===================================================================
--- django/trunk/tests/regressiontests/test_utils/tests.py      2011-01-20 
01:00:47 UTC (rev 15250)
+++ django/trunk/tests/regressiontests/test_utils/tests.py      2011-01-20 
04:47:47 UTC (rev 15251)
@@ -2,12 +2,24 @@
 
 from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
 
+from models import Person
 
 if sys.version_info >= (2, 5):
-    from tests_25 import AssertNumQueriesTests
+    from tests_25 import AssertNumQueriesContextManagerTests
 
 
 class SkippingTestCase(TestCase):
+    def test_skip_unless_db_feature(self):
+        "A test that might be skipped is actually called."
+        # Total hack, but it works, just want an attribute that's always true.
+        @skipUnlessDBFeature("__class__")
+        def test_func():
+            raise ValueError
+
+        self.assertRaises(ValueError, test_func)
+
+
+class AssertNumQueriesTests(TestCase):
     def test_assert_num_queries(self):
         def test_func():
             raise ValueError
@@ -16,18 +28,28 @@
             self.assertNumQueries, 2, test_func
         )
 
-    def test_skip_unless_db_feature(self):
-        "A test that might be skipped is actually called."
-        # Total hack, but it works, just want an attribute that's always true.
-        @skipUnlessDBFeature("__class__")
+    def test_assert_num_queries_with_client(self):
+        person = Person.objects.create(name='test')
+
+        self.assertNumQueries(
+            1,
+            self.client.get,
+            "/test_utils/get_person/%s/" % person.pk
+        )
+
+        self.assertNumQueries(
+            1,
+            self.client.get,
+            "/test_utils/get_person/%s/" % person.pk
+        )
+
         def test_func():
-            raise ValueError
+            self.client.get("/test_utils/get_person/%s/" % person.pk)
+            self.client.get("/test_utils/get_person/%s/" % person.pk)
+        self.assertNumQueries(2, test_func)
 
-        self.assertRaises(ValueError, test_func)
 
-
 class SaveRestoreWarningState(TestCase):
-
     def test_save_restore_warnings_state(self):
         """
         Ensure save_warnings_state/restore_warnings_state work correctly.

Modified: django/trunk/tests/regressiontests/test_utils/tests_25.py
===================================================================
--- django/trunk/tests/regressiontests/test_utils/tests_25.py   2011-01-20 
01:00:47 UTC (rev 15250)
+++ django/trunk/tests/regressiontests/test_utils/tests_25.py   2011-01-20 
04:47:47 UTC (rev 15251)
@@ -5,7 +5,7 @@
 from models import Person
 
 
-class AssertNumQueriesTests(TestCase):
+class AssertNumQueriesContextManagerTests(TestCase):
     def test_simple(self):
         with self.assertNumQueries(0):
             pass
@@ -26,3 +26,16 @@
         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)

Added: django/trunk/tests/regressiontests/test_utils/urls.py
===================================================================
--- django/trunk/tests/regressiontests/test_utils/urls.py                       
        (rev 0)
+++ django/trunk/tests/regressiontests/test_utils/urls.py       2011-01-20 
04:47:47 UTC (rev 15251)
@@ -0,0 +1,8 @@
+from django.conf.urls.defaults import patterns
+
+import views
+
+
+urlpatterns = patterns('',
+    (r'^get_person/(\d+)/$', views.get_person),
+)

Added: django/trunk/tests/regressiontests/test_utils/views.py
===================================================================
--- django/trunk/tests/regressiontests/test_utils/views.py                      
        (rev 0)
+++ django/trunk/tests/regressiontests/test_utils/views.py      2011-01-20 
04:47:47 UTC (rev 15251)
@@ -0,0 +1,7 @@
+from django.http import HttpResponse
+from django.shortcuts import get_object_or_404
+from models import Person
+
+def get_person(request, pk):
+    person = get_object_or_404(Person, pk=pk)
+    return HttpResponse(person.name)
\ No newline at end of file

Modified: django/trunk/tests/urls.py
===================================================================
--- django/trunk/tests/urls.py  2011-01-20 01:00:47 UTC (rev 15250)
+++ django/trunk/tests/urls.py  2011-01-20 04:47:47 UTC (rev 15251)
@@ -1,5 +1,6 @@
 from django.conf.urls.defaults import *
 
+
 urlpatterns = patterns('',
     # test_client modeltest urls
     (r'^test_client/', include('modeltests.test_client.urls')),
@@ -41,4 +42,7 @@
 
     # special headers views
     (r'special_headers/', include('regressiontests.special_headers.urls')),
+
+    # test util views
+    (r'test_utils/', include('regressiontests.test_utils.urls')),
 )

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to