tests.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. from __future__ import unicode_literals
  2. from django.core.exceptions import FieldError
  3. from django.db.models import Count, F, Max
  4. from django.test import TestCase
  5. from .models import A, B, Bar, D, DataPoint, Foo, RelatedPoint
  6. class SimpleTest(TestCase):
  7. def setUp(self):
  8. self.a1 = A.objects.create()
  9. self.a2 = A.objects.create()
  10. for x in range(20):
  11. B.objects.create(a=self.a1)
  12. D.objects.create(a=self.a1)
  13. def test_nonempty_update(self):
  14. """
  15. Update changes the right number of rows for a nonempty queryset
  16. """
  17. num_updated = self.a1.b_set.update(y=100)
  18. self.assertEqual(num_updated, 20)
  19. cnt = B.objects.filter(y=100).count()
  20. self.assertEqual(cnt, 20)
  21. def test_empty_update(self):
  22. """
  23. Update changes the right number of rows for an empty queryset
  24. """
  25. num_updated = self.a2.b_set.update(y=100)
  26. self.assertEqual(num_updated, 0)
  27. cnt = B.objects.filter(y=100).count()
  28. self.assertEqual(cnt, 0)
  29. def test_nonempty_update_with_inheritance(self):
  30. """
  31. Update changes the right number of rows for an empty queryset
  32. when the update affects only a base table
  33. """
  34. num_updated = self.a1.d_set.update(y=100)
  35. self.assertEqual(num_updated, 20)
  36. cnt = D.objects.filter(y=100).count()
  37. self.assertEqual(cnt, 20)
  38. def test_empty_update_with_inheritance(self):
  39. """
  40. Update changes the right number of rows for an empty queryset
  41. when the update affects only a base table
  42. """
  43. num_updated = self.a2.d_set.update(y=100)
  44. self.assertEqual(num_updated, 0)
  45. cnt = D.objects.filter(y=100).count()
  46. self.assertEqual(cnt, 0)
  47. def test_foreign_key_update_with_id(self):
  48. """
  49. Update works using <field>_id for foreign keys
  50. """
  51. num_updated = self.a1.d_set.update(a_id=self.a2)
  52. self.assertEqual(num_updated, 20)
  53. self.assertEqual(self.a2.d_set.count(), 20)
  54. class AdvancedTests(TestCase):
  55. def setUp(self):
  56. self.d0 = DataPoint.objects.create(name="d0", value="apple")
  57. self.d2 = DataPoint.objects.create(name="d2", value="banana")
  58. self.d3 = DataPoint.objects.create(name="d3", value="banana")
  59. self.r1 = RelatedPoint.objects.create(name="r1", data=self.d3)
  60. def test_update(self):
  61. """
  62. Objects are updated by first filtering the candidates into a queryset
  63. and then calling the update() method. It executes immediately and
  64. returns nothing.
  65. """
  66. resp = DataPoint.objects.filter(value="apple").update(name="d1")
  67. self.assertEqual(resp, 1)
  68. resp = DataPoint.objects.filter(value="apple")
  69. self.assertEqual(list(resp), [self.d0])
  70. def test_update_multiple_objects(self):
  71. """
  72. We can update multiple objects at once.
  73. """
  74. resp = DataPoint.objects.filter(value="banana").update(
  75. value="pineapple")
  76. self.assertEqual(resp, 2)
  77. self.assertEqual(DataPoint.objects.get(name="d2").value, 'pineapple')
  78. def test_update_fk(self):
  79. """
  80. Foreign key fields can also be updated, although you can only update
  81. the object referred to, not anything inside the related object.
  82. """
  83. resp = RelatedPoint.objects.filter(name="r1").update(data=self.d0)
  84. self.assertEqual(resp, 1)
  85. resp = RelatedPoint.objects.filter(data__name="d0")
  86. self.assertEqual(list(resp), [self.r1])
  87. def test_update_multiple_fields(self):
  88. """
  89. Multiple fields can be updated at once
  90. """
  91. resp = DataPoint.objects.filter(value="apple").update(
  92. value="fruit", another_value="peach")
  93. self.assertEqual(resp, 1)
  94. d = DataPoint.objects.get(name="d0")
  95. self.assertEqual(d.value, 'fruit')
  96. self.assertEqual(d.another_value, 'peach')
  97. def test_update_all(self):
  98. """
  99. In the rare case you want to update every instance of a model, update()
  100. is also a manager method.
  101. """
  102. self.assertEqual(DataPoint.objects.update(value='thing'), 3)
  103. resp = DataPoint.objects.values('value').distinct()
  104. self.assertEqual(list(resp), [{'value': 'thing'}])
  105. def test_update_slice_fail(self):
  106. """
  107. We do not support update on already sliced query sets.
  108. """
  109. method = DataPoint.objects.all()[:2].update
  110. with self.assertRaises(AssertionError):
  111. method(another_value='another thing')
  112. def test_update_respects_to_field(self):
  113. """
  114. Update of an FK field which specifies a to_field works.
  115. """
  116. a_foo = Foo.objects.create(target='aaa')
  117. b_foo = Foo.objects.create(target='bbb')
  118. bar = Bar.objects.create(foo=a_foo)
  119. self.assertEqual(bar.foo_id, a_foo.target)
  120. bar_qs = Bar.objects.filter(pk=bar.pk)
  121. self.assertEqual(bar_qs[0].foo_id, a_foo.target)
  122. bar_qs.update(foo=b_foo)
  123. self.assertEqual(bar_qs[0].foo_id, b_foo.target)
  124. def test_update_annotated_queryset(self):
  125. """
  126. Update of a queryset that's been annotated.
  127. """
  128. # Trivial annotated update
  129. qs = DataPoint.objects.annotate(alias=F('value'))
  130. self.assertEqual(qs.update(another_value='foo'), 3)
  131. # Update where annotation is used for filtering
  132. qs = DataPoint.objects.annotate(alias=F('value')).filter(alias='apple')
  133. self.assertEqual(qs.update(another_value='foo'), 1)
  134. # Update where annotation is used in update parameters
  135. qs = DataPoint.objects.annotate(alias=F('value'))
  136. self.assertEqual(qs.update(another_value=F('alias')), 3)
  137. # Update where aggregation annotation is used in update parameters
  138. qs = DataPoint.objects.annotate(max=Max('value'))
  139. with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'):
  140. qs.update(another_value=F('max'))
  141. def test_update_annotated_multi_table_queryset(self):
  142. """
  143. Update of a queryset that's been annotated and involves multiple tables.
  144. """
  145. # Trivial annotated update
  146. qs = DataPoint.objects.annotate(related_count=Count('relatedpoint'))
  147. self.assertEqual(qs.update(value='Foo'), 3)
  148. # Update where annotation is used for filtering
  149. qs = DataPoint.objects.annotate(related_count=Count('relatedpoint'))
  150. self.assertEqual(qs.filter(related_count=1).update(value='Foo'), 1)
  151. # Update where annotation is used in update parameters
  152. # #26539 - This isn't forbidden but also doesn't generate proper SQL
  153. # qs = RelatedPoint.objects.annotate(data_name=F('data__name'))
  154. # updated = qs.update(name=F('data_name'))
  155. # self.assertEqual(updated, 1)
  156. # Update where aggregation annotation is used in update parameters
  157. qs = RelatedPoint.objects.annotate(max=Max('data__value'))
  158. with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'):
  159. qs.update(name=F('max'))