PageRenderTime 125ms CodeModel.GetById 65ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/modeltests/update/tests.py

https://code.google.com/p/mango-py/
Python | 116 lines | 65 code | 15 blank | 36 comment | 2 complexity | 6502e606efe91796c30b8db3da3393af MD5 | raw file
  1from django.test import TestCase
  2
  3from models import A, B, C, D, DataPoint, RelatedPoint
  4
  5
  6class 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
 14    def test_nonempty_update(self):
 15        """
 16        Test that update changes the right number of rows for a nonempty queryset
 17        """
 18        num_updated = self.a1.b_set.update(y=100)
 19        self.assertEqual(num_updated, 20)
 20        cnt = B.objects.filter(y=100).count()
 21        self.assertEqual(cnt, 20)
 22
 23    def test_empty_update(self):
 24        """
 25        Test that update changes the right number of rows for an empty queryset
 26        """
 27        num_updated = self.a2.b_set.update(y=100)
 28        self.assertEqual(num_updated, 0)
 29        cnt = B.objects.filter(y=100).count()
 30        self.assertEqual(cnt, 0)
 31
 32    def test_nonempty_update_with_inheritance(self):
 33        """
 34        Test that update changes the right number of rows for an empty queryset
 35        when the update affects only a base table
 36        """
 37        num_updated = self.a1.d_set.update(y=100)
 38        self.assertEqual(num_updated, 20)
 39        cnt = D.objects.filter(y=100).count()
 40        self.assertEqual(cnt, 20)
 41
 42    def test_empty_update_with_inheritance(self):
 43        """
 44        Test that update changes the right number of rows for an empty queryset
 45        when the update affects only a base table
 46        """
 47        num_updated = self.a2.d_set.update(y=100)
 48        self.assertEqual(num_updated, 0)
 49        cnt = D.objects.filter(y=100).count()
 50        self.assertEqual(cnt, 0)
 51
 52class AdvancedTests(TestCase):
 53
 54    def setUp(self):
 55        self.d0 = DataPoint.objects.create(name="d0", value="apple")
 56        self.d2 = DataPoint.objects.create(name="d2", value="banana")
 57        self.d3 = DataPoint.objects.create(name="d3", value="banana")
 58        self.r1 = RelatedPoint.objects.create(name="r1", data=self.d3)
 59
 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
 71    def test_update_multiple_objects(self):
 72        """
 73        We can update multiple objects at once.
 74        """
 75        resp = DataPoint.objects.filter(value="banana").update(
 76            value="pineapple")
 77        self.assertEqual(resp, 2)
 78        self.assertEqual(DataPoint.objects.get(name="d2").value, u'pineapple')
 79
 80    def test_update_fk(self):
 81        """
 82        Foreign key fields can also be updated, although you can only update
 83        the object referred to, not anything inside the related object.
 84        """
 85        resp = RelatedPoint.objects.filter(name="r1").update(data=self.d0)
 86        self.assertEqual(resp, 1)
 87        resp = RelatedPoint.objects.filter(data__name="d0")
 88        self.assertEqual(list(resp), [self.r1])
 89
 90    def test_update_multiple_fields(self):
 91        """
 92        Multiple fields can be updated at once
 93        """
 94        resp = DataPoint.objects.filter(value="apple").update(
 95            value="fruit", another_value="peach")
 96        self.assertEqual(resp, 1)
 97        d = DataPoint.objects.get(name="d0")
 98        self.assertEqual(d.value, u'fruit')
 99        self.assertEqual(d.another_value, u'peach')
100
101    def test_update_all(self):
102        """
103        In the rare case you want to update every instance of a model, update()
104        is also a manager method.
105        """
106        self.assertEqual(DataPoint.objects.update(value='thing'), 3)
107        resp = DataPoint.objects.values('value').distinct()
108        self.assertEqual(list(resp), [{'value': u'thing'}])
109
110    def test_update_slice_fail(self):
111        """
112        We do not support update on already sliced query sets.
113        """
114        method = DataPoint.objects.all()[:2].update
115        self.assertRaises(AssertionError, method,
116            another_value='another thing')