Ticket #15696: with-tests.patch

File with-tests.patch, 2.7 KB (added by Jonas H., 14 years ago)

Fixed version with tests

  • django/core/management/commands/loaddata.py

    commit 6767cafc03f7a0a9e766ccc7a6e4ee3ce6494de8
    Author: Jonas Haag <jonas@lophus.org>
    Date:   Fri Apr 8 03:52:59 2011 -0700
    
        v2 with tests
    
    diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py
    index 34f3543..6f523c5 100644
    a b from optparse import make_option  
    66
    77from django.conf import settings
    88from django.core import serializers
     9from django.core.serializers.signals import pre_insert
    910from django.core.management.base import BaseCommand
    1011from django.core.management.color import no_style
    1112from django.db import connections, router, transaction, DEFAULT_DB_ALIAS
    class Command(BaseCommand):  
    171172                                    if router.allow_syncdb(using, obj.object.__class__):
    172173                                        loaded_objects_in_fixture += 1
    173174                                        models.add(obj.object.__class__)
     175                                        pre_insert.send(self, instance=obj.object)
    174176                                        obj.save(using=using)
    175177                                loaded_object_count += loaded_objects_in_fixture
    176178                                fixture_object_count += objects_in_fixture
  • new file django/core/serializers/signals.py

    diff --git a/django/core/serializers/signals.py b/django/core/serializers/signals.py
    new file mode 100644
    index 0000000..c447e54
    - +  
     1from django.dispatch import Signal
     2
     3pre_insert = Signal(providing_args=['instance'])
  • tests/regressiontests/fixtures_regress/tests.py

    diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
    index 3dc4ede..ef1508f 100644
    a b class TestFixtures(TestCase):  
    363363            % widget.pk
    364364            )
    365365
     366    def test_pre_insert_signal(self):
     367        """
     368        Test for ticket #15696 - deserialized objects may be manipulated before
     369        committing them to database using the pre_insert signal
     370        """
     371        from django.core.serializers.signals import pre_insert
     372        def callback(signal, sender, instance):
     373            instance.name = 'hello'
     374        pre_insert.connect(callback)
     375        try:
     376            management.call_command(
     377                'loaddata',
     378                'thingy.json',
     379                verbosity=0,
     380                commit=False
     381            )
     382            self.assertEqual(Thingy.objects.get().name, u'hello')
     383        finally:
     384            pre_insert.disconnect(callback)
     385
    366386
    367387class NaturalKeyFixtureTests(TestCase):
    368388    def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):
Back to Top