tests.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. from django.test import TestCase
  2. from .models import (
  3. A, B, Building, C, Chick, Child, Class, Client, ClientStatus, Connection,
  4. Country, Device, Enrollment, Hen, Item, Organizer, Person, Port,
  5. SpecialClient, State, Student, TUser,
  6. )
  7. class SelectRelatedRegressTests(TestCase):
  8. def test_regression_7110(self):
  9. """
  10. Regression test for bug #7110.
  11. When using select_related(), we must query the
  12. Device and Building tables using two different aliases (each) in order to
  13. differentiate the start and end Connection fields. The net result is that
  14. both the "connections = ..." queries here should give the same results
  15. without pulling in more than the absolute minimum number of tables
  16. (history has shown that it's easy to make a mistake in the implementation
  17. and include some unnecessary bonus joins).
  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. connections = Connection.objects.filter(start__device__building=b, end__device__building=b).order_by('id')
  29. self.assertEqual(
  30. [(c.id, str(c.start), str(c.end)) for c in connections],
  31. [(c1.id, 'router/4', 'switch/7'), (c2.id, 'switch/7', 'server/1')]
  32. )
  33. connections = (
  34. Connection.objects
  35. .filter(start__device__building=b, end__device__building=b)
  36. .select_related()
  37. .order_by('id')
  38. )
  39. self.assertEqual(
  40. [(c.id, str(c.start), str(c.end)) for c in connections],
  41. [(c1.id, 'router/4', 'switch/7'), (c2.id, 'switch/7', 'server/1')]
  42. )
  43. # This final query should only have seven tables (port, device and building
  44. # twice each, plus connection once). Thus, 6 joins plus the FROM table.
  45. self.assertEqual(str(connections.query).count(" JOIN "), 6)
  46. def test_regression_8106(self):
  47. """
  48. Regression test for bug #8106.
  49. Same sort of problem as the previous test, but this time there are
  50. more extra tables to pull in as part of the select_related() and some
  51. of them could potentially clash (so need to be kept separate).
  52. """
  53. us = TUser.objects.create(name="std")
  54. usp = Person.objects.create(user=us)
  55. uo = TUser.objects.create(name="org")
  56. uop = Person.objects.create(user=uo)
  57. s = Student.objects.create(person=usp)
  58. o = Organizer.objects.create(person=uop)
  59. c = Class.objects.create(org=o)
  60. Enrollment.objects.create(std=s, cls=c)
  61. e_related = Enrollment.objects.all().select_related()[0]
  62. self.assertEqual(e_related.std.person.user.name, "std")
  63. self.assertEqual(e_related.cls.org.person.user.name, "org")
  64. def test_regression_8036(self):
  65. """
  66. Regression test for bug #8036
  67. the first related model in the tests below
  68. ("state") is empty and we try to select the more remotely related
  69. state__country. The regression here was not skipping the empty column results
  70. for country before getting status.
  71. """
  72. Country.objects.create(name='Australia')
  73. active = ClientStatus.objects.create(name='active')
  74. client = Client.objects.create(name='client', status=active)
  75. self.assertEqual(client.status, active)
  76. self.assertEqual(Client.objects.select_related()[0].status, active)
  77. self.assertEqual(Client.objects.select_related('state')[0].status, active)
  78. self.assertEqual(Client.objects.select_related('state', 'status')[0].status, active)
  79. self.assertEqual(Client.objects.select_related('state__country')[0].status, active)
  80. self.assertEqual(Client.objects.select_related('state__country', 'status')[0].status, active)
  81. self.assertEqual(Client.objects.select_related('status')[0].status, active)
  82. def test_multi_table_inheritance(self):
  83. """ Exercising select_related() with multi-table model inheritance. """
  84. c1 = Child.objects.create(name="child1", value=42)
  85. i1 = Item.objects.create(name="item1", child=c1)
  86. i2 = Item.objects.create(name="item2")
  87. self.assertSequenceEqual(
  88. Item.objects.select_related("child").order_by("name"),
  89. [i1, i2],
  90. )
  91. def test_regression_12851(self):
  92. """
  93. Regression for #12851
  94. Deferred fields are used correctly if you select_related a subset
  95. of fields.
  96. """
  97. australia = Country.objects.create(name='Australia')
  98. active = ClientStatus.objects.create(name='active')
  99. wa = State.objects.create(name="Western Australia", country=australia)
  100. Client.objects.create(name='Brian Burke', state=wa, status=active)
  101. burke = Client.objects.select_related('state').defer('state__name').get(name='Brian Burke')
  102. self.assertEqual(burke.name, 'Brian Burke')
  103. self.assertEqual(burke.state.name, 'Western Australia')
  104. # Still works if we're dealing with an inherited class
  105. SpecialClient.objects.create(name='Troy Buswell', state=wa, status=active, value=42)
  106. troy = SpecialClient.objects.select_related('state').defer('state__name').get(name='Troy Buswell')
  107. self.assertEqual(troy.name, 'Troy Buswell')
  108. self.assertEqual(troy.value, 42)
  109. self.assertEqual(troy.state.name, 'Western Australia')
  110. # Still works if we defer an attribute on the inherited class
  111. troy = SpecialClient.objects.select_related('state').defer('value', 'state__name').get(name='Troy Buswell')
  112. self.assertEqual(troy.name, 'Troy Buswell')
  113. self.assertEqual(troy.value, 42)
  114. self.assertEqual(troy.state.name, 'Western Australia')
  115. # Also works if you use only, rather than defer
  116. troy = SpecialClient.objects.select_related('state').only('name', 'state').get(name='Troy Buswell')
  117. self.assertEqual(troy.name, 'Troy Buswell')
  118. self.assertEqual(troy.value, 42)
  119. self.assertEqual(troy.state.name, 'Western Australia')
  120. def test_null_join_promotion(self):
  121. australia = Country.objects.create(name='Australia')
  122. active = ClientStatus.objects.create(name='active')
  123. wa = State.objects.create(name="Western Australia", country=australia)
  124. bob = Client.objects.create(name='Bob', status=active)
  125. jack = Client.objects.create(name='Jack', status=active, state=wa)
  126. qs = Client.objects.filter(state=wa).select_related('state')
  127. with self.assertNumQueries(1):
  128. self.assertEqual(list(qs), [jack])
  129. self.assertEqual(qs[0].state, wa)
  130. # The select_related join wasn't promoted as there was already an
  131. # existing (even if trimmed) inner join to state.
  132. self.assertNotIn('LEFT OUTER', str(qs.query))
  133. qs = Client.objects.select_related('state').order_by('name')
  134. with self.assertNumQueries(1):
  135. self.assertEqual(list(qs), [bob, jack])
  136. self.assertIs(qs[0].state, None)
  137. self.assertEqual(qs[1].state, wa)
  138. # The select_related join was promoted as there is already an
  139. # existing join.
  140. self.assertIn('LEFT OUTER', str(qs.query))
  141. def test_regression_19870(self):
  142. hen = Hen.objects.create(name='Hen')
  143. Chick.objects.create(name='Chick', mother=hen)
  144. self.assertEqual(Chick.objects.all()[0].mother.name, 'Hen')
  145. self.assertEqual(Chick.objects.select_related()[0].mother.name, 'Hen')
  146. def test_regression_10733(self):
  147. a = A.objects.create(name='a', lots_of_text='lots_of_text_a', a_field='a_field')
  148. b = B.objects.create(name='b', lots_of_text='lots_of_text_b', b_field='b_field')
  149. c = C.objects.create(name='c', lots_of_text='lots_of_text_c', is_published=True,
  150. c_a=a, c_b=b)
  151. results = C.objects.all().only('name', 'lots_of_text', 'c_a', 'c_b', 'c_b__lots_of_text',
  152. 'c_a__name', 'c_b__name').select_related()
  153. self.assertSequenceEqual(results, [c])
  154. with self.assertNumQueries(0):
  155. qs_c = results[0]
  156. self.assertEqual(qs_c.name, 'c')
  157. self.assertEqual(qs_c.lots_of_text, 'lots_of_text_c')
  158. self.assertEqual(qs_c.c_b.lots_of_text, 'lots_of_text_b')
  159. self.assertEqual(qs_c.c_a.name, 'a')
  160. self.assertEqual(qs_c.c_b.name, 'b')
  161. def test_regression_22508(self):
  162. building = Building.objects.create(name='101')
  163. device = Device.objects.create(name="router", building=building)
  164. Port.objects.create(port_number='1', device=device)
  165. device = Device.objects.get()
  166. port = device.port_set.select_related('device__building').get()
  167. with self.assertNumQueries(0):
  168. port.device.building