Author: russellm Date: 2010-09-13 00:46:46 -0500 (Mon, 13 Sep 2010) New Revision: 13833
Modified: django/branches/releases/1.2.X/tests/modeltests/update/models.py django/branches/releases/1.2.X/tests/modeltests/update/tests.py Log: [1.2.X] Migrated the update doctests. Thanks to Eric Florenzano. Backport of r13824 from trunk. Modified: django/branches/releases/1.2.X/tests/modeltests/update/models.py =================================================================== --- django/branches/releases/1.2.X/tests/modeltests/update/models.py 2010-09-13 05:46:34 UTC (rev 13832) +++ django/branches/releases/1.2.X/tests/modeltests/update/models.py 2010-09-13 05:46:46 UTC (rev 13833) @@ -33,59 +33,3 @@ class D(C): a = models.ForeignKey(A) - -__test__ = {'API_TESTS': """ ->>> DataPoint(name="d0", value="apple").save() ->>> DataPoint(name="d2", value="banana").save() ->>> d3 = DataPoint.objects.create(name="d3", value="banana") ->>> RelatedPoint(name="r1", data=d3).save() - -Objects are updated by first filtering the candidates into a queryset and then -calling the update() method. It executes immediately and returns nothing. - ->>> DataPoint.objects.filter(value="apple").update(name="d1") -1 ->>> DataPoint.objects.filter(value="apple") -[<DataPoint: d1>] - -We can update multiple objects at once. - ->>> DataPoint.objects.filter(value="banana").update(value="pineapple") -2 ->>> DataPoint.objects.get(name="d2").value -u'pineapple' - -Foreign key fields can also be updated, although you can only update the object -referred to, not anything inside the related object. - ->>> d = DataPoint.objects.get(name="d1") ->>> RelatedPoint.objects.filter(name="r1").update(data=d) -1 ->>> RelatedPoint.objects.filter(data__name="d1") -[<RelatedPoint: r1>] - -Multiple fields can be updated at once - ->>> DataPoint.objects.filter(value="pineapple").update(value="fruit", another_value="peaches") -2 ->>> d = DataPoint.objects.get(name="d2") ->>> d.value, d.another_value -(u'fruit', u'peaches') - -In the rare case you want to update every instance of a model, update() is also -a manager method. - ->>> DataPoint.objects.update(value='thing') -3 ->>> DataPoint.objects.values('value').distinct() -[{'value': u'thing'}] - -We do not support update on already sliced query sets. - ->>> DataPoint.objects.all()[:2].update(another_value='another thing') -Traceback (most recent call last): - ... -AssertionError: Cannot update a query once a slice has been taken. - -""" -} Modified: django/branches/releases/1.2.X/tests/modeltests/update/tests.py =================================================================== --- django/branches/releases/1.2.X/tests/modeltests/update/tests.py 2010-09-13 05:46:34 UTC (rev 13832) +++ django/branches/releases/1.2.X/tests/modeltests/update/tests.py 2010-09-13 05:46:46 UTC (rev 13833) @@ -1,6 +1,6 @@ from django.test import TestCase -from models import A, B, D +from models import A, B, C, D, DataPoint, RelatedPoint class SimpleTest(TestCase): def setUp(self): @@ -47,3 +47,69 @@ self.failUnlessEqual(num_updated, 0) cnt = D.objects.filter(y=100).count() self.failUnlessEqual(cnt, 0) + +class AdvancedTests(TestCase): + + def setUp(self): + self.d0 = DataPoint.objects.create(name="d0", value="apple") + self.d2 = DataPoint.objects.create(name="d2", value="banana") + self.d3 = DataPoint.objects.create(name="d3", value="banana") + self.r1 = RelatedPoint.objects.create(name="r1", data=self.d3) + + def test_update(self): + """ + Objects are updated by first filtering the candidates into a queryset + and then calling the update() method. It executes immediately and + returns nothing. + """ + resp = DataPoint.objects.filter(value="apple").update(name="d1") + self.assertEqual(resp, 1) + resp = DataPoint.objects.filter(value="apple") + self.assertEqual(list(resp), [self.d0]) + + def test_update_multiple_objects(self): + """ + We can update multiple objects at once. + """ + resp = DataPoint.objects.filter(value="banana").update( + value="pineapple") + self.assertEqual(resp, 2) + self.assertEqual(DataPoint.objects.get(name="d2").value, u'pineapple') + + def test_update_fk(self): + """ + Foreign key fields can also be updated, although you can only update + the object referred to, not anything inside the related object. + """ + resp = RelatedPoint.objects.filter(name="r1").update(data=self.d0) + self.assertEqual(resp, 1) + resp = RelatedPoint.objects.filter(data__name="d0") + self.assertEqual(list(resp), [self.r1]) + + def test_update_multiple_fields(self): + """ + Multiple fields can be updated at once + """ + resp = DataPoint.objects.filter(value="apple").update( + value="fruit", another_value="peach") + self.assertEqual(resp, 1) + d = DataPoint.objects.get(name="d0") + self.assertEqual(d.value, u'fruit') + self.assertEqual(d.another_value, u'peach') + + def test_update_all(self): + """ + In the rare case you want to update every instance of a model, update() + is also a manager method. + """ + self.assertEqual(DataPoint.objects.update(value='thing'), 3) + resp = DataPoint.objects.values('value').distinct() + self.assertEqual(list(resp), [{'value': u'thing'}]) + + def test_update_slice_fail(self): + """ + We do not support update on already sliced query sets. + """ + method = DataPoint.objects.all()[:2].update + self.assertRaises(AssertionError, method, + another_value='another thing') \ No newline at end of file -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.