Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrated the update doctests. Thanks to Eric Florenzano.

Backport of r13824 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13833 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f7bdebf2fec57a58e374161cd9522333e806c8db 1 parent 62585b7
Russell Keith-Magee authored September 13, 2010
56  tests/modeltests/update/models.py
@@ -33,59 +33,3 @@ class C(models.Model):
33 33
 
34 34
 class D(C):
35 35
     a = models.ForeignKey(A)
36  
-
37  
-__test__ = {'API_TESTS': """
38  
->>> DataPoint(name="d0", value="apple").save()
39  
->>> DataPoint(name="d2", value="banana").save()
40  
->>> d3 = DataPoint.objects.create(name="d3", value="banana")
41  
->>> RelatedPoint(name="r1", data=d3).save()
42  
-
43  
-Objects are updated by first filtering the candidates into a queryset and then
44  
-calling the update() method. It executes immediately and returns nothing.
45  
-
46  
->>> DataPoint.objects.filter(value="apple").update(name="d1")
47  
-1
48  
->>> DataPoint.objects.filter(value="apple")
49  
-[<DataPoint: d1>]
50  
-
51  
-We can update multiple objects at once.
52  
-
53  
->>> DataPoint.objects.filter(value="banana").update(value="pineapple")
54  
-2
55  
->>> DataPoint.objects.get(name="d2").value
56  
-u'pineapple'
57  
-
58  
-Foreign key fields can also be updated, although you can only update the object
59  
-referred to, not anything inside the related object.
60  
-
61  
->>> d = DataPoint.objects.get(name="d1")
62  
->>> RelatedPoint.objects.filter(name="r1").update(data=d)
63  
-1
64  
->>> RelatedPoint.objects.filter(data__name="d1")
65  
-[<RelatedPoint: r1>]
66  
-
67  
-Multiple fields can be updated at once
68  
-
69  
->>> DataPoint.objects.filter(value="pineapple").update(value="fruit", another_value="peaches")
70  
-2
71  
->>> d = DataPoint.objects.get(name="d2")
72  
->>> d.value, d.another_value
73  
-(u'fruit', u'peaches')
74  
-
75  
-In the rare case you want to update every instance of a model, update() is also
76  
-a manager method.
77  
-
78  
->>> DataPoint.objects.update(value='thing')
79  
-3
80  
->>> DataPoint.objects.values('value').distinct()
81  
-[{'value': u'thing'}]
82  
-
83  
-We do not support update on already sliced query sets.
84  
-
85  
->>> DataPoint.objects.all()[:2].update(another_value='another thing')
86  
-Traceback (most recent call last):
87  
-    ...
88  
-AssertionError: Cannot update a query once a slice has been taken.
89  
-
90  
-"""
91  
-}
68  tests/modeltests/update/tests.py
... ...
@@ -1,6 +1,6 @@
1 1
 from django.test import TestCase
2 2
 
3  
-from models import A, B, D
  3
+from models import A, B, C, D, DataPoint, RelatedPoint
4 4
 
5 5
 class SimpleTest(TestCase):
6 6
     def setUp(self):
@@ -47,3 +47,69 @@ def test_empty_update_with_inheritance(self):
47 47
         self.failUnlessEqual(num_updated, 0)
48 48
         cnt = D.objects.filter(y=100).count()
49 49
         self.failUnlessEqual(cnt, 0)
  50
+
  51
+class AdvancedTests(TestCase):
  52
+
  53
+    def setUp(self):
  54
+        self.d0 = DataPoint.objects.create(name="d0", value="apple")
  55
+        self.d2 = DataPoint.objects.create(name="d2", value="banana")
  56
+        self.d3 = DataPoint.objects.create(name="d3", value="banana")
  57
+        self.r1 = RelatedPoint.objects.create(name="r1", data=self.d3)
  58
+
  59
+    def test_update(self):
  60
+        """
  61
+        Objects are updated by first filtering the candidates into a queryset
  62
+        and then calling the update() method. It executes immediately and
  63
+        returns nothing.
  64
+        """
  65
+        resp = DataPoint.objects.filter(value="apple").update(name="d1")
  66
+        self.assertEqual(resp, 1)
  67
+        resp = DataPoint.objects.filter(value="apple")
  68
+        self.assertEqual(list(resp), [self.d0])
  69
+
  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, u'pineapple')
  78
+
  79
+    def test_update_fk(self):
  80
+        """
  81
+        Foreign key fields can also be updated, although you can only update
  82
+        the object referred to, not anything inside the related object.
  83
+        """
  84
+        resp = RelatedPoint.objects.filter(name="r1").update(data=self.d0)
  85
+        self.assertEqual(resp, 1)
  86
+        resp = RelatedPoint.objects.filter(data__name="d0")
  87
+        self.assertEqual(list(resp), [self.r1])
  88
+
  89
+    def test_update_multiple_fields(self):
  90
+        """
  91
+        Multiple fields can be updated at once
  92
+        """
  93
+        resp = DataPoint.objects.filter(value="apple").update(
  94
+            value="fruit", another_value="peach")
  95
+        self.assertEqual(resp, 1)
  96
+        d = DataPoint.objects.get(name="d0")
  97
+        self.assertEqual(d.value, u'fruit')
  98
+        self.assertEqual(d.another_value, u'peach')
  99
+
  100
+    def test_update_all(self):
  101
+        """
  102
+        In the rare case you want to update every instance of a model, update()
  103
+        is also a manager method.
  104
+        """
  105
+        self.assertEqual(DataPoint.objects.update(value='thing'), 3)
  106
+        resp = DataPoint.objects.values('value').distinct()
  107
+        self.assertEqual(list(resp), [{'value': u'thing'}])
  108
+
  109
+    def test_update_slice_fail(self):
  110
+        """
  111
+        We do not support update on already sliced query sets.
  112
+        """
  113
+        method = DataPoint.objects.all()[:2].update
  114
+        self.assertRaises(AssertionError, method,
  115
+            another_value='another thing')

0 notes on commit f7bdebf

Please sign in to comment.
Something went wrong with that request. Please try again.