|
1 from django.test import TestCase |
|
2 from regressiontests.select_related_regress.models import * |
|
3 |
|
4 class SelectRelatedRegressTests(TestCase): |
|
5 |
|
6 def test_regression_7110(self): |
|
7 """ |
|
8 Regression test for bug #7110. |
|
9 |
|
10 When using select_related(), we must query the |
|
11 Device and Building tables using two different aliases (each) in order to |
|
12 differentiate the start and end Connection fields. The net result is that |
|
13 both the "connections = ..." queries here should give the same results |
|
14 without pulling in more than the absolute minimum number of tables |
|
15 (history has shown that it's easy to make a mistake in the implementation |
|
16 and include some unnecessary bonus joins). |
|
17 """ |
|
18 |
|
19 b=Building.objects.create(name='101') |
|
20 dev1=Device.objects.create(name="router", building=b) |
|
21 dev2=Device.objects.create(name="switch", building=b) |
|
22 dev3=Device.objects.create(name="server", building=b) |
|
23 port1=Port.objects.create(port_number='4',device=dev1) |
|
24 port2=Port.objects.create(port_number='7',device=dev2) |
|
25 port3=Port.objects.create(port_number='1',device=dev3) |
|
26 c1=Connection.objects.create(start=port1, end=port2) |
|
27 c2=Connection.objects.create(start=port2, end=port3) |
|
28 |
|
29 connections=Connection.objects.filter(start__device__building=b, end__device__building=b).order_by('id') |
|
30 self.assertEquals([(c.id, unicode(c.start), unicode(c.end)) for c in connections], |
|
31 [(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]) |
|
32 |
|
33 connections=Connection.objects.filter(start__device__building=b, end__device__building=b).select_related().order_by('id') |
|
34 self.assertEquals([(c.id, unicode(c.start), unicode(c.end)) for c in connections], |
|
35 [(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]) |
|
36 |
|
37 # This final query should only join seven tables (port, device and building |
|
38 # twice each, plus connection once). |
|
39 self.assertEquals(connections.query.count_active_tables(), 7) |
|
40 |
|
41 |
|
42 def test_regression_8106(self): |
|
43 """ |
|
44 Regression test for bug #8106. |
|
45 |
|
46 Same sort of problem as the previous test, but this time there are |
|
47 more extra tables to pull in as part of the select_related() and some |
|
48 of them could potentially clash (so need to be kept separate). |
|
49 """ |
|
50 |
|
51 us = TUser.objects.create(name="std") |
|
52 usp = Person.objects.create(user=us) |
|
53 uo = TUser.objects.create(name="org") |
|
54 uop = Person.objects.create(user=uo) |
|
55 s = Student.objects.create(person = usp) |
|
56 o = Organizer.objects.create(person = uop) |
|
57 c = Class.objects.create(org=o) |
|
58 e = Enrollment.objects.create(std=s, cls=c) |
|
59 |
|
60 e_related = Enrollment.objects.all().select_related()[0] |
|
61 self.assertEquals(e_related.std.person.user.name, u"std") |
|
62 self.assertEquals(e_related.cls.org.person.user.name, u"org") |
|
63 |
|
64 def test_regression_8036(self): |
|
65 """ |
|
66 Regression test for bug #8036 |
|
67 |
|
68 the first related model in the tests below |
|
69 ("state") is empty and we try to select the more remotely related |
|
70 state__country. The regression here was not skipping the empty column results |
|
71 for country before getting status. |
|
72 """ |
|
73 |
|
74 australia = Country.objects.create(name='Australia') |
|
75 active = ClientStatus.objects.create(name='active') |
|
76 client = Client.objects.create(name='client', status=active) |
|
77 |
|
78 self.assertEquals(client.status, active) |
|
79 self.assertEquals(Client.objects.select_related()[0].status, active) |
|
80 self.assertEquals(Client.objects.select_related('state')[0].status, active) |
|
81 self.assertEquals(Client.objects.select_related('state', 'status')[0].status, active) |
|
82 self.assertEquals(Client.objects.select_related('state__country')[0].status, active) |
|
83 self.assertEquals(Client.objects.select_related('state__country', 'status')[0].status, active) |
|
84 self.assertEquals(Client.objects.select_related('status')[0].status, active) |
|
85 |
|
86 def test_multi_table_inheritance(self): |
|
87 """ Exercising select_related() with multi-table model inheritance. """ |
|
88 c1 = Child.objects.create(name="child1", value=42) |
|
89 i1 = Item.objects.create(name="item1", child=c1) |
|
90 i2 = Item.objects.create(name="item2") |
|
91 |
|
92 self.assertQuerysetEqual( |
|
93 Item.objects.select_related("child").order_by("name"), |
|
94 ["<Item: item1>", "<Item: item2>"] |
|
95 ) |
|
96 |
|
97 def test_regression_12851(self): |
|
98 """ |
|
99 Regression for #12851 |
|
100 |
|
101 Deferred fields are used correctly if you select_related a subset |
|
102 of fields. |
|
103 """ |
|
104 australia = Country.objects.create(name='Australia') |
|
105 active = ClientStatus.objects.create(name='active') |
|
106 |
|
107 wa = State.objects.create(name="Western Australia", country=australia) |
|
108 c1 = Client.objects.create(name='Brian Burke', state=wa, status=active) |
|
109 burke = Client.objects.select_related('state').defer('state__name').get(name='Brian Burke') |
|
110 |
|
111 self.assertEquals(burke.name, u'Brian Burke') |
|
112 self.assertEquals(burke.state.name, u'Western Australia') |
|
113 |
|
114 # Still works if we're dealing with an inherited class |
|
115 sc1 = SpecialClient.objects.create(name='Troy Buswell', state=wa, status=active, value=42) |
|
116 troy = SpecialClient.objects.select_related('state').defer('state__name').get(name='Troy Buswell') |
|
117 |
|
118 self.assertEquals(troy.name, u'Troy Buswell') |
|
119 self.assertEquals(troy.value, 42) |
|
120 self.assertEquals(troy.state.name, u'Western Australia') |
|
121 |
|
122 # Still works if we defer an attribute on the inherited class |
|
123 troy = SpecialClient.objects.select_related('state').defer('value', 'state__name').get(name='Troy Buswell') |
|
124 |
|
125 self.assertEquals(troy.name, u'Troy Buswell') |
|
126 self.assertEquals(troy.value, 42) |
|
127 self.assertEquals(troy.state.name, u'Western Australia') |
|
128 |
|
129 # Also works if you use only, rather than defer |
|
130 troy = SpecialClient.objects.select_related('state').only('name').get(name='Troy Buswell') |
|
131 |
|
132 self.assertEquals(troy.name, u'Troy Buswell') |
|
133 self.assertEquals(troy.value, 42) |
|
134 self.assertEquals(troy.state.name, u'Western Australia') |