Ticket #10399: 0002-Fixed-10399-Tested-that-o2o-field-updates-are-not-co.patch

File 0002-Fixed-10399-Tested-that-o2o-field-updates-are-not-co.patch, 2.1 KB (added by Simon Charette, 11 years ago)

Testcase for this ticket using CaptureQueriesContext

  • tests/model_inheritance/tests.py

    From ed166c918c9a14673d33e071488189c37d24b877 Mon Sep 17 00:00:00 2001
    From: Simon Charette <charette.s@gmail.com>
    Date: Fri, 1 Mar 2013 15:32:39 -0500
    Subject: [PATCH 2/2] Fixed #10399 -- Tested that o2o field updates are not
     constrained by an inner query.
    
    ---
     tests/model_inheritance/tests.py |   26 +++++++++++++++++++++++++-
     1 file changed, 25 insertions(+), 1 deletion(-)
    
    diff --git a/tests/model_inheritance/tests.py b/tests/model_inheritance/tests.py
    index 16d2242..46cef3a 100644
    a b  
    1 from __future__ import absolute_import
     1from __future__ import absolute_import, unicode_literals
    22
    33from operator import attrgetter
    44
    55from django.core.exceptions import FieldError
     6from django.db import connection
    67from django.test import TestCase
     8from django.test.testcases import CaptureQueriesContext
    79from django.utils import six
    810
    911from .models import (Chef, CommonInfo, ItalianRestaurant, ParkingLot, Place,
    class ModelInheritanceTests(TestCase):  
    294296        )
    295297        with self.assertNumQueries(6):
    296298            ir.save()
     299
     300    def test_update_parent_filtering(self):
     301        """
     302        Test that updating a field of a model subclass doesn't issue an UPDATE
     303        query constrained by an inner query.
     304        Refs #10399
     305        """
     306        supplier = Supplier.objects.create(
     307            name='Central market',
     308            address='610 some street'
     309        )
     310        # Capture the expected query in a database agnostic way
     311        with CaptureQueriesContext(connection) as captured_queries:
     312            Place.objects.filter(pk=supplier.pk).update(name=supplier.name)
     313        expected_sql = captured_queries[0]['sql']
     314        # Capture the queries executed when a subclassed model instance is saved.
     315        with CaptureQueriesContext(connection) as captured_queries:
     316            supplier.save(update_fields=('name',))
     317        for query in captured_queries:
     318            sql = query['sql']
     319            if 'UPDATE' in sql:
     320                self.assertEqual(expected_sql, sql)
Back to Top