Ticket #5416: django-assert-num-queries.3.diff

File django-assert-num-queries.3.diff, 29.8 KB (added by Alex Gaynor, 14 years ago)
  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 4bbaa1c..4883e0b 100644
    a b class BaseDatabaseWrapper(local):  
    2121        self.settings_dict = settings_dict
    2222        self.alias = alias
    2323        self.vendor = 'unknown'
     24        self.use_debug_cursor = None
    2425
    2526    def __eq__(self, other):
    2627        return self.settings_dict == other.settings_dict
    class BaseDatabaseWrapper(local):  
    7475    def cursor(self):
    7576        from django.conf import settings
    7677        cursor = self._cursor()
    77         if settings.DEBUG:
     78        if (self.use_debug_cursor or
     79            (self.use_debug_cursor is None and settings.DEBUG)):
    7880            return self.make_debug_cursor(cursor)
    7981        return cursor
    8082
  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 06b6eb3..65664a1 100644
    a b  
    11import re
     2import sys
    23from urlparse import urlsplit, urlunsplit
    34from xml.dom.minidom import parseString, Node
    45
    class DocTestRunner(doctest.DocTestRunner):  
    205206        for conn in connections:
    206207            transaction.rollback_unless_managed(using=conn)
    207208
     209class _AssertNumQueriesContext(object):
     210    def __init__(self, test_case, num, connection):
     211        self.test_case = test_case
     212        self.num = num
     213        self.connection = connection
     214
     215    def __enter__(self):
     216        self.old_debug_cursor = self.connection.use_debug_cursor
     217        self.connection.use_debug_cursor = True
     218        self.starting_queries = len(self.connection.queries)
     219        return self
     220
     221    def __exit__(self, exc_type, exc_value, traceback):
     222        if exc_type is not None:
     223            return
     224
     225        self.connection.use_debug_cursor = self.old_debug_cursor
     226        final_queries = len(self.connection.queries)
     227        executed = final_queries - self.starting_queries
     228
     229        self.test_case.assertEqual(
     230            executed, self.num, "%d queries executed, %d expected" % (
     231                executed, self.num
     232            )
     233        )
     234
     235
    208236class TransactionTestCase(unittest.TestCase):
    209237    # The class we'll use for the test client self.client.
    210238    # Can be overridden in derived classes.
    class TransactionTestCase(unittest.TestCase):  
    469497    def assertQuerysetEqual(self, qs, values, transform=repr):
    470498        return self.assertEqual(map(transform, qs), values)
    471499
     500    def assertNumQueries(self, num, func=None, *args, **kwargs):
     501        using = kwargs.pop("using", DEFAULT_DB_ALIAS)
     502        connection = connections[using]
     503
     504        context = _AssertNumQueriesContext(self, num, connection)
     505        if func is None:
     506            return context
     507
     508        # Basically emulate the `with` statement here.
     509
     510        context.__enter__()
     511        try:
     512            func(*args, **kwargs)
     513        finally:
     514            context.__exit__(*sys.exc_info())
     515
    472516def connections_support_transactions():
    473517    """
    474518    Returns True if all connections support transactions.  This is messy
  • docs/topics/testing.txt

    diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
    index 904e707..848e272 100644
    a b cause of an failure in your test suite.  
    13721372    implicit ordering, you will need to apply a ``order_by()`` clause to your
    13731373    queryset to ensure that the test will pass reliably.
    13741374
     1375.. method:: TestCase.assertNumQueries(num, func, *args, **kwargs):
     1376
     1377    .. versionadded:: 1.3
     1378
     1379    Asserts that when ``func`` is called with ``*args`` and ``**kwargs`` that
     1380    ``num`` database queries are executed.
     1381
     1382    If a ``"using"`` key is present in ``kwargs`` it is used as the database
     1383    alias for which to check the number of queries.  If you wish to call a
     1384    function with a ``using`` parameter you can do it by wrapping the call with
     1385    a ``lambda`` to add an extra parameter::
     1386
     1387        self.assertNumQueries(7, lambda: my_function(using=7))
     1388
     1389    If you're using Python 2.5 or greater you can also use this as a context
     1390    manager::
     1391
     1392        # This is necessary in Python 2.5 to enable the with statement, in 2.6
     1393        # and up it is no longer necessary.
     1394        from __future__ import with_statement
     1395
     1396        with self.assertNumQueries(2):
     1397            Person.objects.create(name="Aaron")
     1398            Person.objects.create(name="Daniel")
     1399
     1400
    13751401.. _topics-testing-email:
    13761402
    13771403E-mail services
  • tests/modeltests/select_related/tests.py

    diff --git a/tests/modeltests/select_related/tests.py b/tests/modeltests/select_related/tests.py
    index a211102..301ce93 100644
    a b  
    11from django.test import TestCase
    2 from django.conf import settings
    3 from django import db
    42
    53from models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species
    64
    class SelectRelatedTests(TestCase):  
    3634        # queries so we'll set it to True here and reset it at the end of the
    3735        # test case.
    3836        self.create_base_data()
    39         settings.DEBUG = True
    40         db.reset_queries()
    41 
    42     def tearDown(self):
    43         settings.DEBUG = False
    4437
    4538    def test_access_fks_without_select_related(self):
    4639        """
    4740        Normally, accessing FKs doesn't fill in related objects
    4841        """
    49         fly = Species.objects.get(name="melanogaster")
    50         domain = fly.genus.family.order.klass.phylum.kingdom.domain
    51         self.assertEqual(domain.name, 'Eukaryota')
    52         self.assertEqual(len(db.connection.queries), 8)
     42        def test():
     43            fly = Species.objects.get(name="melanogaster")
     44            domain = fly.genus.family.order.klass.phylum.kingdom.domain
     45            self.assertEqual(domain.name, 'Eukaryota')
     46        self.assertNumQueries(8, test)
    5347
    5448    def test_access_fks_with_select_related(self):
    5549        """
    5650        A select_related() call will fill in those related objects without any
    5751        extra queries
    5852        """
    59         person = Species.objects.select_related(depth=10).get(name="sapiens")
    60         domain = person.genus.family.order.klass.phylum.kingdom.domain
    61         self.assertEqual(domain.name, 'Eukaryota')
    62         self.assertEqual(len(db.connection.queries), 1)
     53        def test():
     54            person = Species.objects.select_related(depth=10).get(name="sapiens")
     55            domain = person.genus.family.order.klass.phylum.kingdom.domain
     56            self.assertEqual(domain.name, 'Eukaryota')
     57        self.assertNumQueries(1, test)
    6358
    6459    def test_list_without_select_related(self):
    6560        """
    6661        select_related() also of course applies to entire lists, not just
    6762        items. This test verifies the expected behavior without select_related.
    6863        """
    69         world = Species.objects.all()
    70         families = [o.genus.family.name for o in world]
    71         self.assertEqual(families, [
    72             'Drosophilidae',
    73             'Hominidae',
    74             'Fabaceae',
    75             'Amanitacae',
    76         ])
    77         self.assertEqual(len(db.connection.queries), 9)
     64        def test():
     65            world = Species.objects.all()
     66            families = [o.genus.family.name for o in world]
     67            self.assertEqual(families, [
     68                'Drosophilidae',
     69                'Hominidae',
     70                'Fabaceae',
     71                'Amanitacae',
     72            ])
     73        self.assertNumQueries(9, test)
    7874
    7975    def test_list_with_select_related(self):
    8076        """
    8177        select_related() also of course applies to entire lists, not just
    8278        items. This test verifies the expected behavior with select_related.
    8379        """
    84         world = Species.objects.all().select_related()
    85         families = [o.genus.family.name for o in world]
    86         self.assertEqual(families, [
    87             'Drosophilidae',
    88             'Hominidae',
    89             'Fabaceae',
    90             'Amanitacae',
    91         ])
    92         self.assertEqual(len(db.connection.queries), 1)
     80        def test():
     81            world = Species.objects.all().select_related()
     82            families = [o.genus.family.name for o in world]
     83            self.assertEqual(families, [
     84                'Drosophilidae',
     85                'Hominidae',
     86                'Fabaceae',
     87                'Amanitacae',
     88            ])
     89        self.assertNumQueries(1, test)
    9390
    9491    def test_depth(self, depth=1, expected=7):
    9592        """
    9693        The "depth" argument to select_related() will stop the descent at a
    9794        particular level.
    9895        """
    99         pea = Species.objects.select_related(depth=depth).get(name="sativum")
    100         self.assertEqual(
    101             pea.genus.family.order.klass.phylum.kingdom.domain.name,
    102             'Eukaryota'
    103         )
     96        def test():
     97            pea = Species.objects.select_related(depth=depth).get(name="sativum")
     98            self.assertEqual(
     99                pea.genus.family.order.klass.phylum.kingdom.domain.name,
     100                'Eukaryota'
     101            )
    104102        # Notice: one fewer queries than above because of depth=1
    105         self.assertEqual(len(db.connection.queries), expected)
     103        self.assertNumQueries(expected, test)
    106104
    107105    def test_larger_depth(self):
    108106        """
    class SelectRelatedTests(TestCase):  
    116114        The "depth" argument to select_related() will stop the descent at a
    117115        particular level. This can be used on lists as well.
    118116        """
    119         world = Species.objects.all().select_related(depth=2)
    120         orders = [o.genus.family.order.name for o in world]
    121         self.assertEqual(orders,
    122             ['Diptera', 'Primates', 'Fabales', 'Agaricales'])
    123         self.assertEqual(len(db.connection.queries), 5)
     117        def test():
     118            world = Species.objects.all().select_related(depth=2)
     119            orders = [o.genus.family.order.name for o in world]
     120            self.assertEqual(orders,
     121                ['Diptera', 'Primates', 'Fabales', 'Agaricales'])
     122        self.assertNumQueries(5, test)
    124123
    125124    def test_select_related_with_extra(self):
    126125        s = Species.objects.all().select_related(depth=1)\
    class SelectRelatedTests(TestCase):  
    136135        In this case, we explicitly say to select the 'genus' and
    137136        'genus.family' models, leading to the same number of queries as before.
    138137        """
    139         world = Species.objects.select_related('genus__family')
    140         families = [o.genus.family.name for o in world]
    141         self.assertEqual(families,
    142             ['Drosophilidae', 'Hominidae', 'Fabaceae', 'Amanitacae'])
    143         self.assertEqual(len(db.connection.queries), 1)
     138        def test():
     139            world = Species.objects.select_related('genus__family')
     140            families = [o.genus.family.name for o in world]
     141            self.assertEqual(families,
     142                ['Drosophilidae', 'Hominidae', 'Fabaceae', 'Amanitacae'])
     143        self.assertNumQueries(1, test)
    144144
    145145    def test_more_certain_fields(self):
    146146        """
    147147        In this case, we explicitly say to select the 'genus' and
    148148        'genus.family' models, leading to the same number of queries as before.
    149149        """
    150         world = Species.objects.filter(genus__name='Amanita')\
    151             .select_related('genus__family')
    152         orders = [o.genus.family.order.name for o in world]
    153         self.assertEqual(orders, [u'Agaricales'])
    154         self.assertEqual(len(db.connection.queries), 2)
     150        def test():
     151            world = Species.objects.filter(genus__name='Amanita')\
     152                .select_related('genus__family')
     153            orders = [o.genus.family.order.name for o in world]
     154            self.assertEqual(orders, [u'Agaricales'])
     155        self.assertNumQueries(2, test)
    155156
    156157    def test_field_traversal(self):
    157         s = Species.objects.all().select_related('genus__family__order'
    158             ).order_by('id')[0:1].get().genus.family.order.name
    159         self.assertEqual(s, u'Diptera')
    160         self.assertEqual(len(db.connection.queries), 1)
     158        def test():
     159            s = Species.objects.all().select_related('genus__family__order'
     160                ).order_by('id')[0:1].get().genus.family.order.name
     161            self.assertEqual(s, u'Diptera')
     162        self.assertNumQueries(1, test)
    161163
    162164    def test_depth_fields_fails(self):
    163165        self.assertRaises(TypeError,
  • tests/modeltests/validation/test_unique.py

    diff --git a/tests/modeltests/validation/test_unique.py b/tests/modeltests/validation/test_unique.py
    index 2b824ae..d239b84 100644
    a b import datetime  
    22
    33from django.conf import settings
    44from django.db import connection
     5from django.test import TestCase
    56from django.utils import unittest
    67
    7 from models import CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, UniqueForDateModel, ModelToValidate
     8from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
     9    UniqueForDateModel, ModelToValidate)
    810
    911
    1012class GetUniqueCheckTests(unittest.TestCase):
    class GetUniqueCheckTests(unittest.TestCase):  
    5153            ), m._get_unique_checks(exclude='start_date')
    5254        )
    5355
    54 class PerformUniqueChecksTest(unittest.TestCase):
    55     def setUp(self):
    56         # Set debug to True to gain access to connection.queries.
    57         self._old_debug, settings.DEBUG = settings.DEBUG, True
    58         super(PerformUniqueChecksTest, self).setUp()
    59 
    60     def tearDown(self):
    61         # Restore old debug value.
    62         settings.DEBUG = self._old_debug
    63         super(PerformUniqueChecksTest, self).tearDown()
    64 
     56class PerformUniqueChecksTest(TestCase):
    6557    def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self):
    6658        # Regression test for #12560
    67         query_count = len(connection.queries)
    68         mtv = ModelToValidate(number=10, name='Some Name')
    69         setattr(mtv, '_adding', True)
    70         mtv.full_clean()
    71         self.assertEqual(query_count, len(connection.queries))
     59        def test():
     60            mtv = ModelToValidate(number=10, name='Some Name')
     61            setattr(mtv, '_adding', True)
     62            mtv.full_clean()
     63        self.assertNumQueries(0, test)
    7264
    7365    def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
    7466        # Regression test for #12560
    75         query_count = len(connection.queries)
    76         mtv = ModelToValidate(number=10, name='Some Name', id=123)
    77         setattr(mtv, '_adding', True)
    78         mtv.full_clean()
    79         self.assertEqual(query_count + 1, len(connection.queries))
     67        def test():
     68            mtv = ModelToValidate(number=10, name='Some Name', id=123)
     69            setattr(mtv, '_adding', True)
     70            mtv.full_clean()
     71        self.assertNumQueries(1, test)
    8072
    8173    def test_primary_key_unique_check_not_performed_when_not_adding(self):
    8274        # Regression test for #12132
    83         query_count= len(connection.queries)
    84         mtv = ModelToValidate(number=10, name='Some Name')
    85         mtv.full_clean()
    86         self.assertEqual(query_count, len(connection.queries))
    87 
     75        def test():
     76            mtv = ModelToValidate(number=10, name='Some Name')
     77            mtv.full_clean()
     78        self.assertNumQueries(0, test)
  • tests/modeltests/validation/tests.py

    diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
    index 4dff410..142688f 100644
    a b from modeltests.validation.models import Author, Article, ModelToValidate  
    66
    77# Import other tests for this package.
    88from modeltests.validation.validators import TestModelsWithValidators
    9 from modeltests.validation.test_unique import GetUniqueCheckTests, PerformUniqueChecksTest
     9from modeltests.validation.test_unique import (GetUniqueCheckTests,
     10    PerformUniqueChecksTest)
    1011from modeltests.validation.test_custom_messages import CustomMessagesTest
    1112
    1213
    class ModelFormsTests(TestCase):  
    111112        article = Article(author_id=self.author.id)
    112113        form = ArticleForm(data, instance=article)
    113114        self.assertEqual(form.errors.keys(), ['pub_date'])
    114 
  • tests/regressiontests/defer_regress/tests.py

    diff --git a/tests/regressiontests/defer_regress/tests.py b/tests/regressiontests/defer_regress/tests.py
    index affb0e2..6f1b023 100644
    a b from models import ResolveThis, Item, RelatedItem, Child, Leaf  
    1111
    1212
    1313class DeferRegressionTest(TestCase):
    14     def assert_num_queries(self, n, func, *args, **kwargs):
    15         old_DEBUG = settings.DEBUG
    16         settings.DEBUG = True
    17         starting_queries = len(connection.queries)
    18         try:
    19             func(*args, **kwargs)
    20         finally:
    21             settings.DEBUG = old_DEBUG
    22         self.assertEqual(starting_queries + n, len(connection.queries))
    23 
    24 
    2514    def test_basic(self):
    2615        # Deferred fields should really be deferred and not accidentally use
    2716        # the field's default value just because they aren't passed to __init__
    class DeferRegressionTest(TestCase):  
    3322        def test():
    3423            self.assertEqual(obj.name, "first")
    3524            self.assertEqual(obj.other_value, 0)
    36         self.assert_num_queries(0, test)
     25        self.assertNumQueries(0, test)
    3726
    3827        def test():
    3928            self.assertEqual(obj.value, 42)
    40         self.assert_num_queries(1, test)
     29        self.assertNumQueries(1, test)
    4130
    4231        def test():
    4332            self.assertEqual(obj.text, "xyzzy")
    44         self.assert_num_queries(1, test)
     33        self.assertNumQueries(1, test)
    4534
    4635        def test():
    4736            self.assertEqual(obj.text, "xyzzy")
    48         self.assert_num_queries(0, test)
     37        self.assertNumQueries(0, test)
    4938
    5039        # Regression test for #10695. Make sure different instances don't
    5140        # inadvertently share data in the deferred descriptor objects.
  • tests/regressiontests/forms/models.py

    diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
    index 028ff9b..a4891df 100644
    a b  
    11# -*- coding: utf-8 -*-
    22import datetime
    3 import tempfile
    43import shutil
     4import tempfile
    55
    6 from django.db import models, connection
    7 from django.conf import settings
     6from django.db import models
    87# Can't import as "forms" due to implementation details in the test suite (the
    98# current file is called "forms" and is already imported).
    109from django import forms as django_forms
    class TestTicket12510(TestCase):  
    7776    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
    7877    def setUp(self):
    7978        self.groups = [Group.objects.create(name=name) for name in 'abc']
    80         self.old_debug = settings.DEBUG
    81         # turn debug on to get access to connection.queries
    82         settings.DEBUG = True
    83 
    84     def tearDown(self):
    85         settings.DEBUG = self.old_debug
    8679
    8780    def test_choices_not_fetched_when_not_rendering(self):
    88         initial_queries = len(connection.queries)
    89         field = django_forms.ModelChoiceField(Group.objects.order_by('-name'))
    90         self.assertEqual('a', field.clean(self.groups[0].pk).name)
     81        def test():
     82            field = django_forms.ModelChoiceField(Group.objects.order_by('-name'))
     83            self.assertEqual('a', field.clean(self.groups[0].pk).name)
    9184        # only one query is required to pull the model from DB
    92         self.assertEqual(initial_queries+1, len(connection.queries))
     85        self.assertNumQueries(1, test)
    9386
    9487class ModelFormCallableModelDefault(TestCase):
    9588    def test_no_empty_option(self):
  • tests/regressiontests/model_forms_regress/tests.py

    diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
    index 397651a..d695104 100644
    a b  
    11import unittest
    22from datetime import date
    33
    4 from django import db
    54from django import forms
    65from django.forms.models import modelform_factory, ModelChoiceField
    7 from django.conf import settings
    86from django.test import TestCase
    97from django.core.exceptions import FieldError, ValidationError
    108from django.core.files.uploadedfile import SimpleUploadedFile
    from models import Person, RealPerson, Triple, FilePathModel, Article, \  
    1412
    1513
    1614class ModelMultipleChoiceFieldTests(TestCase):
    17 
    18     def setUp(self):
    19         self.old_debug = settings.DEBUG
    20         settings.DEBUG = True
    21 
    22     def tearDown(self):
    23         settings.DEBUG = self.old_debug
    24 
    2515    def test_model_multiple_choice_number_of_queries(self):
    2616        """
    2717        Test that ModelMultipleChoiceField does O(1) queries instead of
    class ModelMultipleChoiceFieldTests(TestCase):  
    3020        for i in range(30):
    3121            Person.objects.create(name="Person %s" % i)
    3222
    33         db.reset_queries()
    3423        f = forms.ModelMultipleChoiceField(queryset=Person.objects.all())
    35         selected = f.clean([1, 3, 5, 7, 9])
    36         self.assertEquals(len(db.connection.queries), 1)
     24        self.assertNumQueries(1, f.clean, [1, 3, 5, 7, 9])
    3725
    3826class TripleForm(forms.ModelForm):
    3927    class Meta:
    class InvalidFieldAndFactory(TestCase):  
    312300                    model = Person
    313301                    fields = ('name', 'no-field')
    314302        except FieldError, e:
    315             # Make sure the exception contains some reference to the 
     303            # Make sure the exception contains some reference to the
    316304            # field responsible for the problem.
    317305            self.assertTrue('no-field' in e.args[0])
    318306        else:
  • tests/regressiontests/select_related_onetoone/tests.py

    diff --git a/tests/regressiontests/select_related_onetoone/tests.py b/tests/regressiontests/select_related_onetoone/tests.py
    index 4ccb584..ab35fec 100644
    a b from models import (User, UserProfile, UserStat, UserStatResult, StatDetails,  
    77
    88class ReverseSelectRelatedTestCase(TestCase):
    99    def setUp(self):
    10         # Explicitly enable debug for these tests - we need to count
    11         # the queries that have been issued.
    12         self.old_debug = settings.DEBUG
    13         settings.DEBUG = True
    14 
    1510        user = User.objects.create(username="test")
    1611        userprofile = UserProfile.objects.create(user=user, state="KS",
    1712                                                 city="Lawrence")
    class ReverseSelectRelatedTestCase(TestCase):  
    2621                                                  results=results2)
    2722        StatDetails.objects.create(base_stats=advstat, comments=250)
    2823
    29         db.reset_queries()
    30 
    31     def assertQueries(self, queries):
    32         self.assertEqual(len(db.connection.queries), queries)
    33 
    34     def tearDown(self):
    35         settings.DEBUG = self.old_debug
    36 
    3724    def test_basic(self):
    38         u = User.objects.select_related("userprofile").get(username="test")
    39         self.assertEqual(u.userprofile.state, "KS")
    40         self.assertQueries(1)
     25        def test():
     26            u = User.objects.select_related("userprofile").get(username="test")
     27            self.assertEqual(u.userprofile.state, "KS")
     28        self.assertNumQueries(1, test)
    4129
    4230    def test_follow_next_level(self):
    43         u = User.objects.select_related("userstat__results").get(username="test")
    44         self.assertEqual(u.userstat.posts, 150)
    45         self.assertEqual(u.userstat.results.results, 'first results')
    46         self.assertQueries(1)
     31        def test():
     32            u = User.objects.select_related("userstat__results").get(username="test")
     33            self.assertEqual(u.userstat.posts, 150)
     34            self.assertEqual(u.userstat.results.results, 'first results')
     35        self.assertNumQueries(1, test)
    4736
    4837    def test_follow_two(self):
    49         u = User.objects.select_related("userprofile", "userstat").get(username="test")
    50         self.assertEqual(u.userprofile.state, "KS")
    51         self.assertEqual(u.userstat.posts, 150)
    52         self.assertQueries(1)
     38        def test():
     39            u = User.objects.select_related("userprofile", "userstat").get(username="test")
     40            self.assertEqual(u.userprofile.state, "KS")
     41            self.assertEqual(u.userstat.posts, 150)
     42        self.assertNumQueries(1, test)
    5343
    5444    def test_follow_two_next_level(self):
    55         u = User.objects.select_related("userstat__results", "userstat__statdetails").get(username="test")
    56         self.assertEqual(u.userstat.results.results, 'first results')
    57         self.assertEqual(u.userstat.statdetails.comments, 259)
    58         self.assertQueries(1)
     45        def test():
     46            u = User.objects.select_related("userstat__results", "userstat__statdetails").get(username="test")
     47            self.assertEqual(u.userstat.results.results, 'first results')
     48            self.assertEqual(u.userstat.statdetails.comments, 259)
     49        self.assertNumQueries(1, test)
    5950
    6051    def test_forward_and_back(self):
    61         stat = UserStat.objects.select_related("user__userprofile").get(user__username="test")
    62         self.assertEqual(stat.user.userprofile.state, 'KS')
    63         self.assertEqual(stat.user.userstat.posts, 150)
    64         self.assertQueries(1)
     52        def test():
     53            stat = UserStat.objects.select_related("user__userprofile").get(user__username="test")
     54            self.assertEqual(stat.user.userprofile.state, 'KS')
     55            self.assertEqual(stat.user.userstat.posts, 150)
     56        self.assertNumQueries(1, test)
    6557
    6658    def test_back_and_forward(self):
    67         u = User.objects.select_related("userstat").get(username="test")
    68         self.assertEqual(u.userstat.user.username, 'test')
    69         self.assertQueries(1)
     59        def test():
     60            u = User.objects.select_related("userstat").get(username="test")
     61            self.assertEqual(u.userstat.user.username, 'test')
     62        self.assertNumQueries(1, test)
    7063
    7164    def test_not_followed_by_default(self):
    72         u = User.objects.select_related().get(username="test")
    73         self.assertEqual(u.userstat.posts, 150)
    74         self.assertQueries(2)
     65        def test():
     66            u = User.objects.select_related().get(username="test")
     67            self.assertEqual(u.userstat.posts, 150)
     68        self.assertNumQueries(2, test)
    7569
    7670    def test_follow_from_child_class(self):
    77         stat = AdvancedUserStat.objects.select_related('user', 'statdetails').get(posts=200)
    78         self.assertEqual(stat.statdetails.comments, 250)
    79         self.assertEqual(stat.user.username, 'bob')
    80         self.assertQueries(1)
     71        def test():
     72            stat = AdvancedUserStat.objects.select_related('user', 'statdetails').get(posts=200)
     73            self.assertEqual(stat.statdetails.comments, 250)
     74            self.assertEqual(stat.user.username, 'bob')
     75        self.assertNumQueries(1, test)
    8176
    8277    def test_follow_inheritance(self):
    83         stat = UserStat.objects.select_related('user', 'advanceduserstat').get(posts=200)
    84         self.assertEqual(stat.advanceduserstat.posts, 200)
    85         self.assertEqual(stat.user.username, 'bob')
    86         self.assertEqual(stat.advanceduserstat.user.username, 'bob')
    87         self.assertQueries(1)
     78        def test():
     79            stat = UserStat.objects.select_related('user', 'advanceduserstat').get(posts=200)
     80            self.assertEqual(stat.advanceduserstat.posts, 200)
     81            self.assertEqual(stat.user.username, 'bob')
     82            self.assertEqual(stat.advanceduserstat.user.username, 'bob')
     83        self.assertNumQueries(1, test)
    8884
    8985    def test_nullable_relation(self):
    9086        im = Image.objects.create(name="imag1")
  • tests/regressiontests/test_utils/models.py

    diff --git a/tests/regressiontests/test_utils/models.py b/tests/regressiontests/test_utils/models.py
    index e69de29..4da7a07 100644
    a b  
     1from django.db import models
     2
     3
     4class Person(models.Model):
     5    name = models.CharField(max_length=100)
  • new file tests/regressiontests/test_utils/python_25.py

    diff --git a/tests/regressiontests/test_utils/python_25.py b/tests/regressiontests/test_utils/python_25.py
    new file mode 100644
    index 0000000..a1e8a94
    - +  
     1from __future__ import with_statement
     2
     3from django.test import TestCase
     4
     5from models import Person
     6
     7
     8class AssertNumQueriesTests(TestCase):
     9    def test_simple(self):
     10        with self.assertNumQueries(0):
     11            pass
     12
     13        with self.assertNumQueries(1):
     14            # Guy who wrote Linux
     15            Person.objects.create(name="Linus Torvalds")
     16
     17        with self.assertNumQueries(2):
     18            # Guy who owns the bagel place I like
     19            Person.objects.create(name="Uncle Ricky")
     20            self.assertEqual(Person.objects.count(), 2)
     21
     22    def test_failure(self):
     23        with self.assertRaises(AssertionError) as exc_info:
     24            with self.assertNumQueries(2):
     25                Person.objects.count()
     26        self.assertEqual(str(exc_info.exception), "1 != 2 : 1 queries executed, 2 expected")
     27
     28        with self.assertRaises(TypeError):
     29            with self.assertNumQueries(4000):
     30                raise TypeError
  • tests/regressiontests/test_utils/tests.py

    diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py
    index a2539bf..4f92a40 100644
    a b  
    1 r"""
     1import sys
     2
     3if sys.version_info >= (2, 5):
     4    from python_25 import AssertNumQueriesTests
     5
     6
     7__test__ = {"API_TEST": r"""
    28# Some checks of the doctest output normalizer.
    3 # Standard doctests do fairly 
     9# Standard doctests do fairly
    410>>> from django.utils import simplejson
    511>>> from django.utils.xmlutils import SimplerXMLGenerator
    612>>> from StringIO import StringIO
    r"""  
    5561>>> produce_json()
    5662'["foo", {"whiz": 42, "bar": ["baz", null, 1.0, 2]}]'
    5763
    58 # XML output is normalized for attribute order, so it doesn't matter 
     64# XML output is normalized for attribute order, so it doesn't matter
    5965# which order XML element attributes are listed in output
    6066>>> produce_xml()
    6167'<?xml version="1.0" encoding="UTF-8"?>\n<foo aaa="1.0" bbb="2.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
    r"""  
    6975>>> produce_xml_fragment()
    7076'<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>'
    7177
    72 """
    73  No newline at end of file
     78"""}
Back to Top