Ticket #12247: 12247-jkocherhans.diff

File 12247-jkocherhans.diff, 3.2 KB (added by jkocherhans, 9 years ago)

Moved the models and tests from matiasb's patch into the existing update test and changed the formatting to follow pep8.

  • django/db/models/sql/subqueries.py

    diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py
    index ef6cec5..3df3a17 100644
    a b class UpdateQuery(Query): 
    174174        for model, values in self.related_updates.iteritems():
    175175            query = UpdateQuery(model)
    176176            query.values = values
    177             if self.related_ids:
     177            if self.related_ids is not None:
    178178                query.add_filter(('pk__in', self.related_ids))
    179179            result.append(query)
    180180        return result
  • tests/modeltests/update/models.py

    diff --git a/tests/modeltests/update/models.py b/tests/modeltests/update/models.py
    index 0ffd029..9ce672f 100644
    a b class RelatedPoint(models.Model): 
    2121        return unicode(self.name)
    2222
    2323
     24class A(models.Model):
     25    x = models.IntegerField(default=10)
     26
     27class B(models.Model):
     28    a = models.ForeignKey(A)
     29    y = models.IntegerField(default=10)
     30
     31class C(models.Model):
     32    y = models.IntegerField(default=10)
     33
     34class D(C):
     35    a = models.ForeignKey(A)
     36
    2437__test__ = {'API_TESTS': """
    2538>>> DataPoint(name="d0", value="apple").save()
    2639>>> DataPoint(name="d2", value="banana").save()
  • new file tests/modeltests/update/tests.py

    diff --git a/tests/modeltests/update/tests.py b/tests/modeltests/update/tests.py
    new file mode 100644
    index 0000000..8102075
    - +  
     1from django.test import TestCase
     2
     3from models import A, B, D
     4
     5class SimpleTest(TestCase):
     6    def setUp(self):
     7        self.a1 = A.objects.create()
     8        self.a2 = A.objects.create()
     9        for x in range(20):
     10            B.objects.create(a=self.a1)
     11            D.objects.create(a=self.a1)
     12
     13    def tearDown(self):
     14        B.objects.all().delete()
     15        A.objects.all().delete()
     16
     17    def test_nonempty_update(self):
     18        """
     19        Test that update changes the right number of rows for a nonempty queryset
     20        """
     21        num_updated = self.a1.b_set.update(y=100)
     22        self.failUnlessEqual(num_updated, 20)
     23        cnt = B.objects.filter(y=100).count()
     24        self.failUnlessEqual(cnt, 20)
     25
     26    def test_empty_update(self):
     27        """
     28        Test that update changes the right number of rows for an empty queryset
     29        """
     30        num_updated = self.a2.b_set.update(y=100)
     31        self.failUnlessEqual(num_updated, 0)
     32        cnt = B.objects.filter(y=100).count()
     33        self.failUnlessEqual(cnt, 0)
     34
     35    def test_nonempty_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.a1.d_set.update(y=100)
     41        self.failUnlessEqual(num_updated, 20)
     42        cnt = D.objects.filter(y=100).count()
     43        self.failUnlessEqual(cnt, 20)
     44
     45    def test_empty_update_with_inheritance(self):
     46        """
     47        Test that update changes the right number of rows for an empty queryset
     48        when the update affects only a base table
     49        """
     50        num_updated = self.a2.d_set.update(y=100)
     51        self.failUnlessEqual(num_updated, 0)
     52        cnt = D.objects.filter(y=100).count()
     53        self.failUnlessEqual(cnt, 0)
Back to Top