From a5e691e5de3bededca32ec8d9d997abae5ca81c9 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 12 Oct 2011 19:34:34 +0000 Subject: [PATCH] Convert the contettypes tests to use self.assertNumQueries, rather than hand rolling their own version. Thanks to carl for the review. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16962 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/contenttypes/tests.py | 31 ++++++++++++---------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py index fa3878c553..e7f843ddd0 100644 --- a/django/contrib/contenttypes/tests.py +++ b/django/contrib/contenttypes/tests.py @@ -1,12 +1,13 @@ +from __future__ import with_statement + import urllib -from django import db -from django.conf import settings + +from django.db import models from django.contrib.contenttypes.models import ContentType -from django.contrib.sites.models import Site from django.contrib.contenttypes.views import shortcut +from django.contrib.sites.models import Site from django.http import HttpRequest, Http404 from django.test import TestCase -from django.db import models from django.utils.encoding import smart_str @@ -33,16 +34,10 @@ class FooWithUrl(FooWithoutUrl): class ContentTypesTests(TestCase): def setUp(self): - # First, let's make sure we're dealing with a blank slate (and that - # DEBUG is on so that queries get logged) - self.old_DEBUG = settings.DEBUG self.old_Site_meta_installed = Site._meta.installed - settings.DEBUG = True ContentType.objects.clear_cache() - db.reset_queries() def tearDown(self): - settings.DEBUG = self.old_DEBUG Site._meta.installed = self.old_Site_meta_installed ContentType.objects.clear_cache() @@ -54,19 +49,19 @@ class ContentTypesTests(TestCase): """ # At this point, a lookup for a ContentType should hit the DB - ContentType.objects.get_for_model(ContentType) - self.assertEqual(1, len(db.connection.queries)) + with self.assertNumQueries(1): + ContentType.objects.get_for_model(ContentType) # A second hit, though, won't hit the DB, nor will a lookup by ID - ct = ContentType.objects.get_for_model(ContentType) - self.assertEqual(1, len(db.connection.queries)) - ContentType.objects.get_for_id(ct.id) - self.assertEqual(1, len(db.connection.queries)) + with self.assertNumQueries(0): + ct = ContentType.objects.get_for_model(ContentType) + with self.assertNumQueries(0): + ContentType.objects.get_for_id(ct.id) # Once we clear the cache, another lookup will again hit the DB ContentType.objects.clear_cache() - ContentType.objects.get_for_model(ContentType) - self.assertEqual(2, len(db.connection.queries)) + with self.assertNumQueries(1): + ContentType.objects.get_for_model(ContentType) def test_shortcut_view(self): """