-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def pre_save_test(instance, *args, **kwargs): | ||
instance.pre_save_runned = True | ||
|
||
def post_save_test(instance, created, *args, **kwargs): | ||
instance.post_save_runned = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.test import TestCase | ||
|
||
from tests.models import SaveSignalHandlingTestModel | ||
from tests.signals import pre_save_test, post_save_test | ||
from django.db.models.signals import pre_save, post_save | ||
|
||
|
||
class SaveSignalHandlingModelTests(TestCase): | ||
|
||
def test_pre_save(self): | ||
pre_save.connect(pre_save_test, sender=SaveSignalHandlingTestModel) | ||
|
||
obj = SaveSignalHandlingTestModel.objects.create(name='Test') | ||
delattr(obj, 'pre_save_runned') | ||
obj.name = 'Test A' | ||
obj.save() | ||
self.assertEqual(obj.name, 'Test A') | ||
self.assertTrue(hasattr(obj, 'pre_save_runned')) | ||
|
||
obj = SaveSignalHandlingTestModel.objects.create(name='Test') | ||
delattr(obj, 'pre_save_runned') | ||
obj.name = 'Test B' | ||
obj.save(signals_to_disable=['pre_save']) | ||
self.assertEqual(obj.name, 'Test B') | ||
self.assertFalse(hasattr(obj, 'pre_save_runned')) | ||
|
||
|
||
def test_post_save(self): | ||
post_save.connect(post_save_test, sender=SaveSignalHandlingTestModel) | ||
|
||
obj = SaveSignalHandlingTestModel.objects.create(name='Test') | ||
delattr(obj, 'post_save_runned') | ||
obj.name = 'Test A' | ||
obj.save() | ||
self.assertEqual(obj.name, 'Test A') | ||
self.assertTrue(hasattr(obj, 'post_save_runned')) | ||
|
||
obj = SaveSignalHandlingTestModel.objects.create(name='Test') | ||
delattr(obj, 'post_save_runned') | ||
obj.name = 'Test B' | ||
obj.save(signals_to_disable=['post_save']) | ||
self.assertEqual(obj.name, 'Test B') | ||
self.assertFalse(hasattr(obj, 'post_save_runned')) |