/tests/regressiontests/select_related_regress/tests.py

https://code.google.com/p/mango-py/ · Python · 134 lines · 81 code · 24 blank · 29 comment · 2 complexity · 6dcbdeeb15d9dc46d10961c1579830b6 MD5 · raw file

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