diff --git a/django/test/testcases.py b/django/test/testcases.py
index 40b3c82..ed73288 100644
a
|
b
|
class TransactionTestCase(unittest.TestCase):
|
467 | 467 | def assertQuerysetEqual(self, qs, values, transform=repr): |
468 | 468 | return self.assertEqual(map(transform, qs), values) |
469 | 469 | |
| 470 | def assertNumQueries(self, num, func, *args, **kwargs): |
| 471 | using = kwargs.pop("using", DEFAULT_DB_ALIAS) |
| 472 | connection = connections[using] |
| 473 | |
| 474 | at_start = len(connection.queries) |
| 475 | old_DEBUG = settings.DEBUG |
| 476 | settings.DEBUG = True |
| 477 | try: |
| 478 | func(*args, **kwargs) |
| 479 | finally: |
| 480 | settings.DEBUG = old_DEBUG |
| 481 | |
| 482 | executed = len(connection.queries) - at_start |
| 483 | self.assertEqual(num, executed, "%d queries executed, %d expected" % ( |
| 484 | executed, num |
| 485 | )) |
| 486 | |
470 | 487 | def connections_support_transactions(): |
471 | 488 | """ |
472 | 489 | Returns True if all connections support transactions. This is messy |
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 fb77c4d..41cfcfb 100644
a
|
b
|
|
1 | | import unittest |
2 | 1 | import datetime |
3 | | from django.conf import settings |
4 | | from django.db import connection |
5 | | from models import CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, UniqueForDateModel, ModelToValidate |
| 2 | import unittest |
| 3 | |
| 4 | from django.test import TestCase |
| 5 | |
| 6 | from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, |
| 7 | UniqueForDateModel, ModelToValidate) |
6 | 8 | |
7 | 9 | |
8 | 10 | class GetUniqueCheckTests(unittest.TestCase): |
… |
… |
class GetUniqueCheckTests(unittest.TestCase):
|
49 | 51 | ), m._get_unique_checks(exclude='start_date') |
50 | 52 | ) |
51 | 53 | |
52 | | class PerformUniqueChecksTest(unittest.TestCase): |
53 | | def setUp(self): |
54 | | # Set debug to True to gain access to connection.queries. |
55 | | self._old_debug, settings.DEBUG = settings.DEBUG, True |
56 | | super(PerformUniqueChecksTest, self).setUp() |
57 | | |
58 | | def tearDown(self): |
59 | | # Restore old debug value. |
60 | | settings.DEBUG = self._old_debug |
61 | | super(PerformUniqueChecksTest, self).tearDown() |
62 | | |
| 54 | class PerformUniqueChecksTest(TestCase): |
63 | 55 | def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self): |
64 | 56 | # Regression test for #12560 |
65 | | query_count = len(connection.queries) |
66 | | mtv = ModelToValidate(number=10, name='Some Name') |
67 | | setattr(mtv, '_adding', True) |
68 | | mtv.full_clean() |
69 | | self.assertEqual(query_count, len(connection.queries)) |
| 57 | def test(): |
| 58 | mtv = ModelToValidate(number=10, name='Some Name') |
| 59 | setattr(mtv, '_adding', True) |
| 60 | mtv.full_clean() |
| 61 | self.assertNumQueries(0, test) |
70 | 62 | |
71 | 63 | def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self): |
72 | 64 | # Regression test for #12560 |
73 | | query_count = len(connection.queries) |
74 | | mtv = ModelToValidate(number=10, name='Some Name', id=123) |
75 | | setattr(mtv, '_adding', True) |
76 | | mtv.full_clean() |
77 | | self.assertEqual(query_count + 1, len(connection.queries)) |
| 65 | def test(): |
| 66 | mtv = ModelToValidate(number=10, name='Some Name', id=123) |
| 67 | setattr(mtv, '_adding', True) |
| 68 | mtv.full_clean() |
| 69 | self.assertNumQueries(1, test) |
78 | 70 | |
79 | 71 | def test_primary_key_unique_check_not_performed_when_not_adding(self): |
80 | 72 | # Regression test for #12132 |
81 | | query_count= len(connection.queries) |
82 | | mtv = ModelToValidate(number=10, name='Some Name') |
83 | | mtv.full_clean() |
84 | | self.assertEqual(query_count, len(connection.queries)) |
85 | | |
| 73 | def test(): |
| 74 | mtv = ModelToValidate(number=10, name='Some Name') |
| 75 | mtv.full_clean() |
| 76 | 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") |