tests.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. from __future__ import unicode_literals
  2. from django.db import connection
  3. from django.test import TestCase
  4. from .models import (Bar, Favorites, HiddenPointer, Place, Restaurant, Target,
  5. UndergroundBar)
  6. class OneToOneRegressionTests(TestCase):
  7. def setUp(self):
  8. self.p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
  9. self.p1.save()
  10. self.r1 = Restaurant(place=self.p1, serves_hot_dogs=True, serves_pizza=False)
  11. self.r1.save()
  12. self.b1 = Bar(place=self.p1, serves_cocktails=False)
  13. self.b1.save()
  14. def test_reverse_relationship_cache_cascade(self):
  15. """
  16. Regression test for #9023: accessing the reverse relationship shouldn't
  17. result in a cascading delete().
  18. """
  19. bar = UndergroundBar.objects.create(place=self.p1, serves_cocktails=False)
  20. # The bug in #9023: if you access the one-to-one relation *before*
  21. # setting to None and deleting, the cascade happens anyway.
  22. self.p1.undergroundbar
  23. bar.place.name = 'foo'
  24. bar.place = None
  25. bar.save()
  26. self.p1.delete()
  27. self.assertEqual(Place.objects.all().count(), 0)
  28. self.assertEqual(UndergroundBar.objects.all().count(), 1)
  29. def test_create_models_m2m(self):
  30. """
  31. Regression test for #1064 and #1506
  32. Check that we create models via the m2m relation if the remote model
  33. has a OneToOneField.
  34. """
  35. f = Favorites(name='Fred')
  36. f.save()
  37. f.restaurants = [self.r1]
  38. self.assertQuerysetEqual(
  39. f.restaurants.all(),
  40. ['<Restaurant: Demon Dogs the restaurant>']
  41. )
  42. def test_reverse_object_cache(self):
  43. """
  44. Regression test for #7173
  45. Check that the name of the cache for the reverse object is correct.
  46. """
  47. self.assertEqual(self.p1.restaurant, self.r1)
  48. self.assertEqual(self.p1.bar, self.b1)
  49. def test_related_object_cache(self):
  50. """ Regression test for #6886 (the related-object cache) """
  51. # Look up the objects again so that we get "fresh" objects
  52. p = Place.objects.get(name="Demon Dogs")
  53. r = p.restaurant
  54. # Accessing the related object again returns the exactly same object
  55. self.assertTrue(p.restaurant is r)
  56. # But if we kill the cache, we get a new object
  57. del p._restaurant_cache
  58. self.assertFalse(p.restaurant is r)
  59. # Reassigning the Restaurant object results in an immediate cache update
  60. # We can't use a new Restaurant because that'll violate one-to-one, but
  61. # with a new *instance* the is test below will fail if #6886 regresses.
  62. r2 = Restaurant.objects.get(pk=r.pk)
  63. p.restaurant = r2
  64. self.assertTrue(p.restaurant is r2)
  65. # Assigning None succeeds if field is null=True.
  66. ug_bar = UndergroundBar.objects.create(place=p, serves_cocktails=False)
  67. ug_bar.place = None
  68. self.assertTrue(ug_bar.place is None)
  69. # Assigning None fails: Place.restaurant is null=False
  70. self.assertRaises(ValueError, setattr, p, 'restaurant', None)
  71. # You also can't assign an object of the wrong type here
  72. self.assertRaises(ValueError, setattr, p, 'restaurant', p)
  73. # Creation using keyword argument should cache the related object.
  74. p = Place.objects.get(name="Demon Dogs")
  75. r = Restaurant(place=p)
  76. self.assertTrue(r.place is p)
  77. # Creation using attname keyword argument and an id will cause the related
  78. # object to be fetched.
  79. p = Place.objects.get(name="Demon Dogs")
  80. r = Restaurant(place_id=p.id)
  81. self.assertFalse(r.place is p)
  82. self.assertEqual(r.place, p)
  83. def test_filter_one_to_one_relations(self):
  84. """
  85. Regression test for #9968
  86. filtering reverse one-to-one relations with primary_key=True was
  87. misbehaving. We test both (primary_key=True & False) cases here to
  88. prevent any reappearance of the problem.
  89. """
  90. Target.objects.create()
  91. self.assertQuerysetEqual(
  92. Target.objects.filter(pointer=None),
  93. ['<Target: Target object>']
  94. )
  95. self.assertQuerysetEqual(
  96. Target.objects.exclude(pointer=None),
  97. []
  98. )
  99. self.assertQuerysetEqual(
  100. Target.objects.filter(second_pointer=None),
  101. ['<Target: Target object>']
  102. )
  103. self.assertQuerysetEqual(
  104. Target.objects.exclude(second_pointer=None),
  105. []
  106. )
  107. def test_reverse_object_does_not_exist_cache(self):
  108. """
  109. Regression for #13839 and #17439.
  110. DoesNotExist on a reverse one-to-one relation is cached.
  111. """
  112. p = Place(name='Zombie Cats', address='Not sure')
  113. p.save()
  114. with self.assertNumQueries(1):
  115. with self.assertRaises(Restaurant.DoesNotExist):
  116. p.restaurant
  117. with self.assertNumQueries(0):
  118. with self.assertRaises(Restaurant.DoesNotExist):
  119. p.restaurant
  120. def test_reverse_object_cached_when_related_is_accessed(self):
  121. """
  122. Regression for #13839 and #17439.
  123. The target of a one-to-one relation is cached
  124. when the origin is accessed through the reverse relation.
  125. """
  126. # Use a fresh object without caches
  127. r = Restaurant.objects.get(pk=self.r1.pk)
  128. p = r.place
  129. with self.assertNumQueries(0):
  130. self.assertEqual(p.restaurant, r)
  131. def test_related_object_cached_when_reverse_is_accessed(self):
  132. """
  133. Regression for #13839 and #17439.
  134. The origin of a one-to-one relation is cached
  135. when the target is accessed through the reverse relation.
  136. """
  137. # Use a fresh object without caches
  138. p = Place.objects.get(pk=self.p1.pk)
  139. r = p.restaurant
  140. with self.assertNumQueries(0):
  141. self.assertEqual(r.place, p)
  142. def test_reverse_object_cached_when_related_is_set(self):
  143. """
  144. Regression for #13839 and #17439.
  145. The target of a one-to-one relation is always cached.
  146. """
  147. p = Place(name='Zombie Cats', address='Not sure')
  148. p.save()
  149. self.r1.place = p
  150. self.r1.save()
  151. with self.assertNumQueries(0):
  152. self.assertEqual(p.restaurant, self.r1)
  153. def test_reverse_object_cached_when_related_is_unset(self):
  154. """
  155. Regression for #13839 and #17439.
  156. The target of a one-to-one relation is always cached.
  157. """
  158. b = UndergroundBar(place=self.p1, serves_cocktails=True)
  159. b.save()
  160. with self.assertNumQueries(0):
  161. self.assertEqual(self.p1.undergroundbar, b)
  162. b.place = None
  163. b.save()
  164. with self.assertNumQueries(0):
  165. with self.assertRaises(UndergroundBar.DoesNotExist):
  166. self.p1.undergroundbar
  167. def test_get_reverse_on_unsaved_object(self):
  168. """
  169. Regression for #18153 and #19089.
  170. Accessing the reverse relation on an unsaved object
  171. always raises an exception.
  172. """
  173. p = Place()
  174. # When there's no instance of the origin of the one-to-one
  175. with self.assertNumQueries(0):
  176. with self.assertRaises(UndergroundBar.DoesNotExist):
  177. p.undergroundbar
  178. UndergroundBar.objects.create()
  179. # When there's one instance of the origin
  180. # (p.undergroundbar used to return that instance)
  181. with self.assertNumQueries(0):
  182. with self.assertRaises(UndergroundBar.DoesNotExist):
  183. p.undergroundbar
  184. # Several instances of the origin are only possible if database allows
  185. # inserting multiple NULL rows for a unique constraint
  186. if connection.features.supports_nullable_unique_constraints:
  187. UndergroundBar.objects.create()
  188. # When there are several instances of the origin
  189. with self.assertNumQueries(0):
  190. with self.assertRaises(UndergroundBar.DoesNotExist):
  191. p.undergroundbar
  192. def test_set_reverse_on_unsaved_object(self):
  193. """
  194. Writing to the reverse relation on an unsaved object
  195. is impossible too.
  196. """
  197. p = Place()
  198. b = UndergroundBar.objects.create()
  199. with self.assertNumQueries(0):
  200. with self.assertRaises(ValueError):
  201. p.undergroundbar = b
  202. def test_nullable_o2o_delete(self):
  203. u = UndergroundBar.objects.create(place=self.p1)
  204. u.place_id = None
  205. u.save()
  206. self.p1.delete()
  207. self.assertTrue(UndergroundBar.objects.filter(pk=u.pk).exists())
  208. self.assertIsNone(UndergroundBar.objects.get(pk=u.pk).place)
  209. def test_hidden_accessor(self):
  210. """
  211. When a '+' ending related name is specified no reverse accessor should
  212. be added to the related model.
  213. """
  214. self.assertFalse(
  215. hasattr(Target, HiddenPointer._meta.get_field('target').related.get_accessor_name())
  216. )