diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 4bbaa1c..4883e0b 100644
a
|
b
|
class BaseDatabaseWrapper(local):
|
21 | 21 | self.settings_dict = settings_dict |
22 | 22 | self.alias = alias |
23 | 23 | self.vendor = 'unknown' |
| 24 | self.use_debug_cursor = None |
24 | 25 | |
25 | 26 | def __eq__(self, other): |
26 | 27 | return self.settings_dict == other.settings_dict |
… |
… |
class BaseDatabaseWrapper(local):
|
74 | 75 | def cursor(self): |
75 | 76 | from django.conf import settings |
76 | 77 | cursor = self._cursor() |
77 | | if settings.DEBUG: |
| 78 | if (self.use_debug_cursor or |
| 79 | (self.use_debug_cursor is None and settings.DEBUG)): |
78 | 80 | return self.make_debug_cursor(cursor) |
79 | 81 | return cursor |
80 | 82 | |
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 06b6eb3..65664a1 100644
a
|
b
|
|
1 | 1 | import re |
| 2 | import sys |
2 | 3 | from urlparse import urlsplit, urlunsplit |
3 | 4 | from xml.dom.minidom import parseString, Node |
4 | 5 | |
… |
… |
class DocTestRunner(doctest.DocTestRunner):
|
205 | 206 | for conn in connections: |
206 | 207 | transaction.rollback_unless_managed(using=conn) |
207 | 208 | |
| 209 | class _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 | |
208 | 236 | class TransactionTestCase(unittest.TestCase): |
209 | 237 | # The class we'll use for the test client self.client. |
210 | 238 | # Can be overridden in derived classes. |
… |
… |
class TransactionTestCase(unittest.TestCase):
|
469 | 497 | def assertQuerysetEqual(self, qs, values, transform=repr): |
470 | 498 | return self.assertEqual(map(transform, qs), values) |
471 | 499 | |
| 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 | |
472 | 516 | def connections_support_transactions(): |
473 | 517 | """ |
474 | 518 | Returns True if all connections support transactions. This is messy |
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.
|
1372 | 1372 | implicit ordering, you will need to apply a ``order_by()`` clause to your |
1373 | 1373 | queryset to ensure that the test will pass reliably. |
1374 | 1374 | |
| 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 | |
1375 | 1401 | .. _topics-testing-email: |
1376 | 1402 | |
1377 | 1403 | E-mail services |
diff --git a/tests/modeltests/select_related/tests.py b/tests/modeltests/select_related/tests.py
index a211102..301ce93 100644
a
|
b
|
|
1 | 1 | from django.test import TestCase |
2 | | from django.conf import settings |
3 | | from django import db |
4 | 2 | |
5 | 3 | from models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species |
6 | 4 | |
… |
… |
class SelectRelatedTests(TestCase):
|
36 | 34 | # queries so we'll set it to True here and reset it at the end of the |
37 | 35 | # test case. |
38 | 36 | self.create_base_data() |
39 | | settings.DEBUG = True |
40 | | db.reset_queries() |
41 | | |
42 | | def tearDown(self): |
43 | | settings.DEBUG = False |
44 | 37 | |
45 | 38 | def test_access_fks_without_select_related(self): |
46 | 39 | """ |
47 | 40 | Normally, accessing FKs doesn't fill in related objects |
48 | 41 | """ |
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) |
53 | 47 | |
54 | 48 | def test_access_fks_with_select_related(self): |
55 | 49 | """ |
56 | 50 | A select_related() call will fill in those related objects without any |
57 | 51 | extra queries |
58 | 52 | """ |
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) |
63 | 58 | |
64 | 59 | def test_list_without_select_related(self): |
65 | 60 | """ |
66 | 61 | select_related() also of course applies to entire lists, not just |
67 | 62 | items. This test verifies the expected behavior without select_related. |
68 | 63 | """ |
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) |
78 | 74 | |
79 | 75 | def test_list_with_select_related(self): |
80 | 76 | """ |
81 | 77 | select_related() also of course applies to entire lists, not just |
82 | 78 | items. This test verifies the expected behavior with select_related. |
83 | 79 | """ |
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) |
93 | 90 | |
94 | 91 | def test_depth(self, depth=1, expected=7): |
95 | 92 | """ |
96 | 93 | The "depth" argument to select_related() will stop the descent at a |
97 | 94 | particular level. |
98 | 95 | """ |
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 | ) |
104 | 102 | # Notice: one fewer queries than above because of depth=1 |
105 | | self.assertEqual(len(db.connection.queries), expected) |
| 103 | self.assertNumQueries(expected, test) |
106 | 104 | |
107 | 105 | def test_larger_depth(self): |
108 | 106 | """ |
… |
… |
class SelectRelatedTests(TestCase):
|
116 | 114 | The "depth" argument to select_related() will stop the descent at a |
117 | 115 | particular level. This can be used on lists as well. |
118 | 116 | """ |
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) |
124 | 123 | |
125 | 124 | def test_select_related_with_extra(self): |
126 | 125 | s = Species.objects.all().select_related(depth=1)\ |
… |
… |
class SelectRelatedTests(TestCase):
|
136 | 135 | In this case, we explicitly say to select the 'genus' and |
137 | 136 | 'genus.family' models, leading to the same number of queries as before. |
138 | 137 | """ |
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) |
144 | 144 | |
145 | 145 | def test_more_certain_fields(self): |
146 | 146 | """ |
147 | 147 | In this case, we explicitly say to select the 'genus' and |
148 | 148 | 'genus.family' models, leading to the same number of queries as before. |
149 | 149 | """ |
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) |
155 | 156 | |
156 | 157 | 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) |
161 | 163 | |
162 | 164 | def test_depth_fields_fails(self): |
163 | 165 | self.assertRaises(TypeError, |
diff --git a/tests/modeltests/validation/test_unique.py b/tests/modeltests/validation/test_unique.py
index 2b824ae..d239b84 100644
a
|
b
|
import datetime
|
2 | 2 | |
3 | 3 | from django.conf import settings |
4 | 4 | from django.db import connection |
| 5 | from django.test import TestCase |
5 | 6 | from django.utils import unittest |
6 | 7 | |
7 | | from models import CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, UniqueForDateModel, ModelToValidate |
| 8 | from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, |
| 9 | UniqueForDateModel, ModelToValidate) |
8 | 10 | |
9 | 11 | |
10 | 12 | class GetUniqueCheckTests(unittest.TestCase): |
… |
… |
class GetUniqueCheckTests(unittest.TestCase):
|
51 | 53 | ), m._get_unique_checks(exclude='start_date') |
52 | 54 | ) |
53 | 55 | |
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 | | |
| 56 | class PerformUniqueChecksTest(TestCase): |
65 | 57 | def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self): |
66 | 58 | # 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) |
72 | 64 | |
73 | 65 | def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self): |
74 | 66 | # 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) |
80 | 72 | |
81 | 73 | def test_primary_key_unique_check_not_performed_when_not_adding(self): |
82 | 74 | # 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) |
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
|
6 | 6 | |
7 | 7 | # Import other tests for this package. |
8 | 8 | from modeltests.validation.validators import TestModelsWithValidators |
9 | | from modeltests.validation.test_unique import GetUniqueCheckTests, PerformUniqueChecksTest |
| 9 | from modeltests.validation.test_unique import (GetUniqueCheckTests, |
| 10 | PerformUniqueChecksTest) |
10 | 11 | from modeltests.validation.test_custom_messages import CustomMessagesTest |
11 | 12 | |
12 | 13 | |
… |
… |
class ModelFormsTests(TestCase):
|
111 | 112 | article = Article(author_id=self.author.id) |
112 | 113 | form = ArticleForm(data, instance=article) |
113 | 114 | self.assertEqual(form.errors.keys(), ['pub_date']) |
114 | | |
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
|
11 | 11 | |
12 | 12 | |
13 | 13 | class 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 | | |
25 | 14 | def test_basic(self): |
26 | 15 | # Deferred fields should really be deferred and not accidentally use |
27 | 16 | # the field's default value just because they aren't passed to __init__ |
… |
… |
class DeferRegressionTest(TestCase):
|
33 | 22 | def test(): |
34 | 23 | self.assertEqual(obj.name, "first") |
35 | 24 | self.assertEqual(obj.other_value, 0) |
36 | | self.assert_num_queries(0, test) |
| 25 | self.assertNumQueries(0, test) |
37 | 26 | |
38 | 27 | def test(): |
39 | 28 | self.assertEqual(obj.value, 42) |
40 | | self.assert_num_queries(1, test) |
| 29 | self.assertNumQueries(1, test) |
41 | 30 | |
42 | 31 | def test(): |
43 | 32 | self.assertEqual(obj.text, "xyzzy") |
44 | | self.assert_num_queries(1, test) |
| 33 | self.assertNumQueries(1, test) |
45 | 34 | |
46 | 35 | def test(): |
47 | 36 | self.assertEqual(obj.text, "xyzzy") |
48 | | self.assert_num_queries(0, test) |
| 37 | self.assertNumQueries(0, test) |
49 | 38 | |
50 | 39 | # Regression test for #10695. Make sure different instances don't |
51 | 40 | # inadvertently share data in the deferred descriptor objects. |
diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
index 028ff9b..a4891df 100644
a
|
b
|
|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import datetime |
3 | | import tempfile |
4 | 3 | import shutil |
| 4 | import tempfile |
5 | 5 | |
6 | | from django.db import models, connection |
7 | | from django.conf import settings |
| 6 | from django.db import models |
8 | 7 | # Can't import as "forms" due to implementation details in the test suite (the |
9 | 8 | # current file is called "forms" and is already imported). |
10 | 9 | from django import forms as django_forms |
… |
… |
class TestTicket12510(TestCase):
|
77 | 76 | ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). ''' |
78 | 77 | def setUp(self): |
79 | 78 | 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 |
86 | 79 | |
87 | 80 | 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) |
91 | 84 | # 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) |
93 | 86 | |
94 | 87 | class ModelFormCallableModelDefault(TestCase): |
95 | 88 | def test_no_empty_option(self): |
diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
index 397651a..d695104 100644
a
|
b
|
|
1 | 1 | import unittest |
2 | 2 | from datetime import date |
3 | 3 | |
4 | | from django import db |
5 | 4 | from django import forms |
6 | 5 | from django.forms.models import modelform_factory, ModelChoiceField |
7 | | from django.conf import settings |
8 | 6 | from django.test import TestCase |
9 | 7 | from django.core.exceptions import FieldError, ValidationError |
10 | 8 | from django.core.files.uploadedfile import SimpleUploadedFile |
… |
… |
from models import Person, RealPerson, Triple, FilePathModel, Article, \
|
14 | 12 | |
15 | 13 | |
16 | 14 | class 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 | | |
25 | 15 | def test_model_multiple_choice_number_of_queries(self): |
26 | 16 | """ |
27 | 17 | Test that ModelMultipleChoiceField does O(1) queries instead of |
… |
… |
class ModelMultipleChoiceFieldTests(TestCase):
|
30 | 20 | for i in range(30): |
31 | 21 | Person.objects.create(name="Person %s" % i) |
32 | 22 | |
33 | | db.reset_queries() |
34 | 23 | 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]) |
37 | 25 | |
38 | 26 | class TripleForm(forms.ModelForm): |
39 | 27 | class Meta: |
… |
… |
class InvalidFieldAndFactory(TestCase):
|
312 | 300 | model = Person |
313 | 301 | fields = ('name', 'no-field') |
314 | 302 | except FieldError, e: |
315 | | # Make sure the exception contains some reference to the |
| 303 | # Make sure the exception contains some reference to the |
316 | 304 | # field responsible for the problem. |
317 | 305 | self.assertTrue('no-field' in e.args[0]) |
318 | 306 | else: |
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,
|
7 | 7 | |
8 | 8 | class ReverseSelectRelatedTestCase(TestCase): |
9 | 9 | 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 | | |
15 | 10 | user = User.objects.create(username="test") |
16 | 11 | userprofile = UserProfile.objects.create(user=user, state="KS", |
17 | 12 | city="Lawrence") |
… |
… |
class ReverseSelectRelatedTestCase(TestCase):
|
26 | 21 | results=results2) |
27 | 22 | StatDetails.objects.create(base_stats=advstat, comments=250) |
28 | 23 | |
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 | | |
37 | 24 | 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) |
41 | 29 | |
42 | 30 | 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) |
47 | 36 | |
48 | 37 | 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) |
53 | 43 | |
54 | 44 | 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) |
59 | 50 | |
60 | 51 | 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) |
65 | 57 | |
66 | 58 | 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) |
70 | 63 | |
71 | 64 | 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) |
75 | 69 | |
76 | 70 | 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) |
81 | 76 | |
82 | 77 | 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) |
88 | 84 | |
89 | 85 | def test_nullable_relation(self): |
90 | 86 | im = Image.objects.create(name="imag1") |
diff --git a/tests/regressiontests/test_utils/models.py b/tests/regressiontests/test_utils/models.py
index e69de29..4da7a07 100644
a
|
b
|
|
| 1 | from django.db import models |
| 2 | |
| 3 | |
| 4 | class Person(models.Model): |
| 5 | name = models.CharField(max_length=100) |
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
-
|
+
|
|
| 1 | from __future__ import with_statement |
| 2 | |
| 3 | from django.test import TestCase |
| 4 | |
| 5 | from models import Person |
| 6 | |
| 7 | |
| 8 | class 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 |
diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py
index a2539bf..4f92a40 100644
a
|
b
|
|
1 | | r""" |
| 1 | import sys |
| 2 | |
| 3 | if sys.version_info >= (2, 5): |
| 4 | from python_25 import AssertNumQueriesTests |
| 5 | |
| 6 | |
| 7 | __test__ = {"API_TEST": r""" |
2 | 8 | # Some checks of the doctest output normalizer. |
3 | | # Standard doctests do fairly |
| 9 | # Standard doctests do fairly |
4 | 10 | >>> from django.utils import simplejson |
5 | 11 | >>> from django.utils.xmlutils import SimplerXMLGenerator |
6 | 12 | >>> from StringIO import StringIO |
… |
… |
r"""
|
55 | 61 | >>> produce_json() |
56 | 62 | '["foo", {"whiz": 42, "bar": ["baz", null, 1.0, 2]}]' |
57 | 63 | |
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 |
59 | 65 | # which order XML element attributes are listed in output |
60 | 66 | >>> produce_xml() |
61 | 67 | '<?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"""
|
69 | 75 | >>> produce_xml_fragment() |
70 | 76 | '<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>' |
71 | 77 | |
72 | | """ |
73 | | No newline at end of file |
| 78 | """} |