/tests/regressiontests/managers_regress/tests.py

https://bitbucket.org/bluezoo/django · Python · 195 lines · 134 code · 30 blank · 31 comment · 18 complexity · d4aa099080c3dc78bd21bf768e6a1ff6 MD5 · raw file

  1. from __future__ import absolute_import
  2. import copy
  3. from django.conf import settings
  4. from django.db import models
  5. from django.db.models.loading import cache
  6. from django.test import TestCase
  7. from django.test.utils import override_settings
  8. from .models import (
  9. Child1,
  10. Child2,
  11. Child3,
  12. Child4,
  13. Child5,
  14. Child6,
  15. Child7,
  16. AbstractBase1,
  17. AbstractBase2,
  18. AbstractBase3,
  19. )
  20. class ManagersRegressionTests(TestCase):
  21. def test_managers(self):
  22. Child1.objects.create(name='fred', data='a1')
  23. Child1.objects.create(name='barney', data='a2')
  24. Child2.objects.create(name='fred', data='b1', value=1)
  25. Child2.objects.create(name='barney', data='b2', value=42)
  26. Child3.objects.create(name='fred', data='c1', comment='yes')
  27. Child3.objects.create(name='barney', data='c2', comment='no')
  28. Child4.objects.create(name='fred', data='d1')
  29. Child4.objects.create(name='barney', data='d2')
  30. Child5.objects.create(name='fred', comment='yes')
  31. Child5.objects.create(name='barney', comment='no')
  32. Child6.objects.create(name='fred', data='f1', value=42)
  33. Child6.objects.create(name='barney', data='f2', value=42)
  34. Child7.objects.create(name='fred')
  35. Child7.objects.create(name='barney')
  36. self.assertQuerysetEqual(Child1.manager1.all(), ["<Child1: a1>"])
  37. self.assertQuerysetEqual(Child1.manager2.all(), ["<Child1: a2>"])
  38. self.assertQuerysetEqual(Child1._default_manager.all(), ["<Child1: a1>"])
  39. self.assertQuerysetEqual(Child2._default_manager.all(), ["<Child2: b1>"])
  40. self.assertQuerysetEqual(Child2.restricted.all(), ["<Child2: b2>"])
  41. self.assertQuerysetEqual(Child3._default_manager.all(), ["<Child3: c1>"])
  42. self.assertQuerysetEqual(Child3.manager1.all(), ["<Child3: c1>"])
  43. self.assertQuerysetEqual(Child3.manager2.all(), ["<Child3: c2>"])
  44. # Since Child6 inherits from Child4, the corresponding rows from f1 and
  45. # f2 also appear here. This is the expected result.
  46. self.assertQuerysetEqual(Child4._default_manager.order_by('data'), [
  47. "<Child4: d1>",
  48. "<Child4: d2>",
  49. "<Child4: f1>",
  50. "<Child4: f2>"
  51. ]
  52. )
  53. self.assertQuerysetEqual(Child4.manager1.all(), [
  54. "<Child4: d1>",
  55. "<Child4: f1>"
  56. ],
  57. ordered=False
  58. )
  59. self.assertQuerysetEqual(Child5._default_manager.all(), ["<Child5: fred>"])
  60. self.assertQuerysetEqual(Child6._default_manager.all(), ["<Child6: f1>"])
  61. self.assertQuerysetEqual(Child7._default_manager.order_by('name'), [
  62. "<Child7: barney>",
  63. "<Child7: fred>"
  64. ]
  65. )
  66. def test_abstract_manager(self):
  67. # Accessing the manager on an abstract model should
  68. # raise an attribute error with an appropriate message.
  69. try:
  70. AbstractBase3.objects.all()
  71. self.fail('Should raise an AttributeError')
  72. except AttributeError as e:
  73. # This error message isn't ideal, but if the model is abstract and
  74. # a lot of the class instantiation logic isn't invoked; if the
  75. # manager is implied, then we don't get a hook to install the
  76. # error-raising manager.
  77. self.assertEqual(str(e), "type object 'AbstractBase3' has no attribute 'objects'")
  78. def test_custom_abstract_manager(self):
  79. # Accessing the manager on an abstract model with an custom
  80. # manager should raise an attribute error with an appropriate
  81. # message.
  82. try:
  83. AbstractBase2.restricted.all()
  84. self.fail('Should raise an AttributeError')
  85. except AttributeError as e:
  86. self.assertEqual(str(e), "Manager isn't available; AbstractBase2 is abstract")
  87. def test_explicit_abstract_manager(self):
  88. # Accessing the manager on an abstract model with an explicit
  89. # manager should raise an attribute error with an appropriate
  90. # message.
  91. try:
  92. AbstractBase1.objects.all()
  93. self.fail('Should raise an AttributeError')
  94. except AttributeError as e:
  95. self.assertEqual(str(e), "Manager isn't available; AbstractBase1 is abstract")
  96. def test_swappable_manager(self):
  97. try:
  98. # This test adds dummy models to the app cache. These
  99. # need to be removed in order to prevent bad interactions
  100. # with the flush operation in other tests.
  101. old_app_models = copy.deepcopy(cache.app_models)
  102. old_app_store = copy.deepcopy(cache.app_store)
  103. settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
  104. class SwappableModel(models.Model):
  105. class Meta:
  106. swappable = 'TEST_SWAPPABLE_MODEL'
  107. # Accessing the manager on a swappable model should
  108. # raise an attribute error with a helpful message
  109. try:
  110. SwappableModel.objects.all()
  111. self.fail('Should raise an AttributeError')
  112. except AttributeError as e:
  113. self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
  114. finally:
  115. del settings.TEST_SWAPPABLE_MODEL
  116. cache.app_models = old_app_models
  117. cache.app_store = old_app_store
  118. def test_custom_swappable_manager(self):
  119. try:
  120. # This test adds dummy models to the app cache. These
  121. # need to be removed in order to prevent bad interactions
  122. # with the flush operation in other tests.
  123. old_app_models = copy.deepcopy(cache.app_models)
  124. old_app_store = copy.deepcopy(cache.app_store)
  125. settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
  126. class SwappableModel(models.Model):
  127. stuff = models.Manager()
  128. class Meta:
  129. swappable = 'TEST_SWAPPABLE_MODEL'
  130. # Accessing the manager on a swappable model with an
  131. # explicit manager should raise an attribute error with a
  132. # helpful message
  133. try:
  134. SwappableModel.stuff.all()
  135. self.fail('Should raise an AttributeError')
  136. except AttributeError as e:
  137. self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
  138. finally:
  139. del settings.TEST_SWAPPABLE_MODEL
  140. cache.app_models = old_app_models
  141. cache.app_store = old_app_store
  142. def test_explicit_swappable_manager(self):
  143. try:
  144. # This test adds dummy models to the app cache. These
  145. # need to be removed in order to prevent bad interactions
  146. # with the flush operation in other tests.
  147. old_app_models = copy.deepcopy(cache.app_models)
  148. old_app_store = copy.deepcopy(cache.app_store)
  149. settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
  150. class SwappableModel(models.Model):
  151. objects = models.Manager()
  152. class Meta:
  153. swappable = 'TEST_SWAPPABLE_MODEL'
  154. # Accessing the manager on a swappable model with an
  155. # explicit manager should raise an attribute error with a
  156. # helpful message
  157. try:
  158. SwappableModel.objects.all()
  159. self.fail('Should raise an AttributeError')
  160. except AttributeError as e:
  161. self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
  162. finally:
  163. del settings.TEST_SWAPPABLE_MODEL
  164. cache.app_models = old_app_models
  165. cache.app_store = old_app_store