From 2b8ccff3b6278f862a2d4840f55a7062747a452f Mon Sep 17 00:00:00 2001 From: Mathieu Pillard Date: Wed, 21 Sep 2016 12:32:24 +0200 Subject: [PATCH] Tested model signals "sender" parameter when defer() is used. This was fixed when dynamic classes for deferred instance loading was removed in 7f51876f99851fdc3fef63aecdfbcffa199c26b9. --- tests/signals/tests.py | 46 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/tests/signals/tests.py b/tests/signals/tests.py index b52cf1d11c..3907bf93ed 100644 --- a/tests/signals/tests.py +++ b/tests/signals/tests.py @@ -53,12 +53,12 @@ class SignalTests(BaseSignalTest): def pre_save_handler(signal, sender, instance, **kwargs): data.append( - (instance, kwargs.get("raw", False)) + (instance, sender, kwargs.get("raw", False)) ) def post_save_handler(signal, sender, instance, **kwargs): data.append( - (instance, kwargs.get("created"), kwargs.get("raw", False)) + (instance, sender, kwargs.get("created"), kwargs.get("raw", False)) ) signals.pre_save.connect(pre_save_handler, weak=False) @@ -67,24 +67,24 @@ class SignalTests(BaseSignalTest): p1 = Person.objects.create(first_name="John", last_name="Smith") self.assertEqual(data, [ - (p1, False), - (p1, True, False), + (p1, Person, False), + (p1, Person, True, False), ]) data[:] = [] p1.first_name = "Tom" p1.save() self.assertEqual(data, [ - (p1, False), - (p1, False, False), + (p1, Person, False), + (p1, Person, False, False), ]) data[:] = [] # Calling an internal method purely so that we can trigger a "raw" save. p1.save_base(raw=True) self.assertEqual(data, [ - (p1, True), - (p1, False, True), + (p1, Person, True), + (p1, Person, False, True), ]) data[:] = [] @@ -92,15 +92,25 @@ class SignalTests(BaseSignalTest): p2.id = 99999 p2.save() self.assertEqual(data, [ - (p2, False), - (p2, True, False), + (p2, Person, False), + (p2, Person, True, False), ]) data[:] = [] p2.id = 99998 p2.save() self.assertEqual(data, [ - (p2, False), - (p2, True, False), + (p2, Person, False), + (p2, Person, True, False), + ]) + + # The sender should stay the same when using defer(). + data[:] = [] + p3 = Person.objects.defer('first_name').get(pk=p1.pk) + p3.last_name = 'Reese' + p3.save() + self.assertEqual(data, [ + (p3, Person, False), + (p3, Person, False, False), ]) finally: signals.pre_save.disconnect(pre_save_handler) @@ -111,7 +121,7 @@ class SignalTests(BaseSignalTest): def pre_delete_handler(signal, sender, instance, **kwargs): data.append( - (instance, instance.id is None) + (instance, sender, instance.id is None) ) # #8285: signals can be any callable @@ -121,7 +131,7 @@ class SignalTests(BaseSignalTest): def __call__(self, signal, sender, instance, **kwargs): self.data.append( - (instance, instance.id is None) + (instance, sender, instance.id is None) ) post_delete_handler = PostDeleteHandler(data) @@ -131,8 +141,8 @@ class SignalTests(BaseSignalTest): p1 = Person.objects.create(first_name="John", last_name="Smith") p1.delete() self.assertEqual(data, [ - (p1, False), - (p1, False), + (p1, Person, False), + (p1, Person, False), ]) data[:] = [] @@ -143,8 +153,8 @@ class SignalTests(BaseSignalTest): p2.save() p2.delete() self.assertEqual(data, [ - (p2, False), - (p2, False) + (p2, Person, False), + (p2, Person, False), ]) data[:] = []