PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/update/tests.py

https://code.google.com/p/mango-py/
Python | 116 lines | 65 code | 15 blank | 36 comment | 1 complexity | 6502e606efe91796c30b8db3da3393af MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.test import TestCase
  2. from models import A, B, C, D, DataPoint, RelatedPoint
  3. class SimpleTest(TestCase):
  4. def setUp(self):
  5. self.a1 = A.objects.create()
  6. self.a2 = A.objects.create()
  7. for x in range(20):
  8. B.objects.create(a=self.a1)
  9. D.objects.create(a=self.a1)
  10. def test_nonempty_update(self):
  11. """
  12. Test that update changes the right number of rows for a nonempty queryset
  13. """
  14. num_updated = self.a1.b_set.update(y=100)
  15. self.assertEqual(num_updated, 20)
  16. cnt = B.objects.filter(y=100).count()
  17. self.assertEqual(cnt, 20)
  18. def test_empty_update(self):
  19. """
  20. Test that update changes the right number of rows for an empty queryset
  21. """
  22. num_updated = self.a2.b_set.update(y=100)
  23. self.assertEqual(num_updated, 0)
  24. cnt = B.objects.filter(y=100).count()
  25. self.assertEqual(cnt, 0)
  26. def test_nonempty_update_with_inheritance(self):
  27. """
  28. Test that update changes the right number of rows for an empty queryset
  29. when the update affects only a base table
  30. """
  31. num_updated = self.a1.d_set.update(y=100)
  32. self.assertEqual(num_updated, 20)
  33. cnt = D.objects.filter(y=100).count()
  34. self.assertEqual(cnt, 20)
  35. def test_empty_update_with_inheritance(self):
  36. """
  37. Test that update changes the right number of rows for an empty queryset
  38. when the update affects only a base table
  39. """
  40. num_updated = self.a2.d_set.update(y=100)
  41. self.assertEqual(num_updated, 0)
  42. cnt = D.objects.filter(y=100).count()
  43. self.assertEqual(cnt, 0)
  44. class AdvancedTests(TestCase):
  45. def setUp(self):
  46. self.d0 = DataPoint.objects.create(name="d0", value="apple")
  47. self.d2 = DataPoint.objects.create(name="d2", value="banana")
  48. self.d3 = DataPoint.objects.create(name="d3", value="banana")
  49. self.r1 = RelatedPoint.objects.create(name="r1", data=self.d3)
  50. def test_update(self):
  51. """
  52. Objects are updated by first filtering the candidates into a queryset
  53. and then calling the update() method. It executes immediately and
  54. returns nothing.
  55. """
  56. resp = DataPoint.objects.filter(value="apple").update(name="d1")
  57. self.assertEqual(resp, 1)
  58. resp = DataPoint.objects.filter(value="apple")
  59. self.assertEqual(list(resp), [self.d0])
  60. def test_update_multiple_objects(self):
  61. """
  62. We can update multiple objects at once.
  63. """
  64. resp = DataPoint.objects.filter(value="banana").update(
  65. value="pineapple")
  66. self.assertEqual(resp, 2)
  67. self.assertEqual(DataPoint.objects.get(name="d2").value, u'pineapple')
  68. def test_update_fk(self):
  69. """
  70. Foreign key fields can also be updated, although you can only update
  71. the object referred to, not anything inside the related object.
  72. """
  73. resp = RelatedPoint.objects.filter(name="r1").update(data=self.d0)
  74. self.assertEqual(resp, 1)
  75. resp = RelatedPoint.objects.filter(data__name="d0")
  76. self.assertEqual(list(resp), [self.r1])
  77. def test_update_multiple_fields(self):
  78. """
  79. Multiple fields can be updated at once
  80. """
  81. resp = DataPoint.objects.filter(value="apple").update(
  82. value="fruit", another_value="peach")
  83. self.assertEqual(resp, 1)
  84. d = DataPoint.objects.get(name="d0")
  85. self.assertEqual(d.value, u'fruit')
  86. self.assertEqual(d.another_value, u'peach')
  87. def test_update_all(self):
  88. """
  89. In the rare case you want to update every instance of a model, update()
  90. is also a manager method.
  91. """
  92. self.assertEqual(DataPoint.objects.update(value='thing'), 3)
  93. resp = DataPoint.objects.values('value').distinct()
  94. self.assertEqual(list(resp), [{'value': u'thing'}])
  95. def test_update_slice_fail(self):
  96. """
  97. We do not support update on already sliced query sets.
  98. """
  99. method = DataPoint.objects.all()[:2].update
  100. self.assertRaises(AssertionError, method,
  101. another_value='another thing')