/google_appengine/lib/django-1.5/tests/regressiontests/managers_regress/tests.py

https://bitbucket.org/Liosan/gizapi
Python | 194 lines | 133 code | 30 blank | 31 comment | 18 complexity | f0b5eb79ea925d9472636d794f48fad6 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. )
  58. self.assertQuerysetEqual(Child5._default_manager.all(), ["<Child5: fred>"])
  59. self.assertQuerysetEqual(Child6._default_manager.all(), ["<Child6: f1>"])
  60. self.assertQuerysetEqual(Child7._default_manager.order_by('name'), [
  61. "<Child7: barney>",
  62. "<Child7: fred>"
  63. ]
  64. )
  65. def test_abstract_manager(self):
  66. # Accessing the manager on an abstract model should
  67. # raise an attribute error with an appropriate message.
  68. try:
  69. AbstractBase3.objects.all()
  70. self.fail('Should raise an AttributeError')
  71. except AttributeError as e:
  72. # This error message isn't ideal, but if the model is abstract and
  73. # a lot of the class instantiation logic isn't invoked; if the
  74. # manager is implied, then we don't get a hook to install the
  75. # error-raising manager.
  76. self.assertEqual(str(e), "type object 'AbstractBase3' has no attribute 'objects'")
  77. def test_custom_abstract_manager(self):
  78. # Accessing the manager on an abstract model with an custom
  79. # manager should raise an attribute error with an appropriate
  80. # message.
  81. try:
  82. AbstractBase2.restricted.all()
  83. self.fail('Should raise an AttributeError')
  84. except AttributeError as e:
  85. self.assertEqual(str(e), "Manager isn't available; AbstractBase2 is abstract")
  86. def test_explicit_abstract_manager(self):
  87. # Accessing the manager on an abstract model with an explicit
  88. # manager should raise an attribute error with an appropriate
  89. # message.
  90. try:
  91. AbstractBase1.objects.all()
  92. self.fail('Should raise an AttributeError')
  93. except AttributeError as e:
  94. self.assertEqual(str(e), "Manager isn't available; AbstractBase1 is abstract")
  95. def test_swappable_manager(self):
  96. try:
  97. # This test adds dummy models to the app cache. These
  98. # need to be removed in order to prevent bad interactions
  99. # with the flush operation in other tests.
  100. old_app_models = copy.deepcopy(cache.app_models)
  101. old_app_store = copy.deepcopy(cache.app_store)
  102. settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
  103. class SwappableModel(models.Model):
  104. class Meta:
  105. swappable = 'TEST_SWAPPABLE_MODEL'
  106. # Accessing the manager on a swappable model should
  107. # raise an attribute error with a helpful message
  108. try:
  109. SwappableModel.objects.all()
  110. self.fail('Should raise an AttributeError')
  111. except AttributeError as e:
  112. self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
  113. finally:
  114. del settings.TEST_SWAPPABLE_MODEL
  115. cache.app_models = old_app_models
  116. cache.app_store = old_app_store
  117. def test_custom_swappable_manager(self):
  118. try:
  119. # This test adds dummy models to the app cache. These
  120. # need to be removed in order to prevent bad interactions
  121. # with the flush operation in other tests.
  122. old_app_models = copy.deepcopy(cache.app_models)
  123. old_app_store = copy.deepcopy(cache.app_store)
  124. settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
  125. class SwappableModel(models.Model):
  126. stuff = models.Manager()
  127. class Meta:
  128. swappable = 'TEST_SWAPPABLE_MODEL'
  129. # Accessing the manager on a swappable model with an
  130. # explicit manager should raise an attribute error with a
  131. # helpful message
  132. try:
  133. SwappableModel.stuff.all()
  134. self.fail('Should raise an AttributeError')
  135. except AttributeError as e:
  136. self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
  137. finally:
  138. del settings.TEST_SWAPPABLE_MODEL
  139. cache.app_models = old_app_models
  140. cache.app_store = old_app_store
  141. def test_explicit_swappable_manager(self):
  142. try:
  143. # This test adds dummy models to the app cache. These
  144. # need to be removed in order to prevent bad interactions
  145. # with the flush operation in other tests.
  146. old_app_models = copy.deepcopy(cache.app_models)
  147. old_app_store = copy.deepcopy(cache.app_store)
  148. settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
  149. class SwappableModel(models.Model):
  150. objects = models.Manager()
  151. class Meta:
  152. swappable = 'TEST_SWAPPABLE_MODEL'
  153. # Accessing the manager on a swappable model with an
  154. # explicit manager should raise an attribute error with a
  155. # helpful message
  156. try:
  157. SwappableModel.objects.all()
  158. self.fail('Should raise an AttributeError')
  159. except AttributeError as e:
  160. self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
  161. finally:
  162. del settings.TEST_SWAPPABLE_MODEL
  163. cache.app_models = old_app_models
  164. cache.app_store = old_app_store