Created
August 23, 2018 12:04
-
-
Save jarshwah/5b234dbf9a0e429d71e060c247ad20ef to your computer and use it in GitHub Desktop.
Prevent receivers from running in test suites
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
import functools | |
from django.conf import settings | |
from django.dispatch import receiver | |
def suspendingreceiver(signal, **decorator_kwargs): | |
""" | |
A wrapper around the standard django receiver that prevents the receiver | |
from running if the setting `SUSPEND_SIGNALS` is `True`. | |
This is particularly useful if you want to prevent signals running during | |
unit testing. | |
@override_settings(SUSPEND_SIGNALS=True) | |
class MyTestCase(TestCase): | |
def test_method(self): | |
Model.objects.create() # post_save_receiver won't execute | |
@suspendingreceiver(post_save, sender=Model) | |
def post_save_receiver(sender, instance=None, **kwargs): | |
work(instance) | |
""" | |
def our_wrapper(func): | |
@receiver(signal, **decorator_kwargs) | |
@functools.wraps(func) | |
def fake_receiver(sender, **kwargs): | |
if settings.SUSPEND_SIGNALS: | |
return | |
return func(sender, **kwargs) | |
return fake_receiver | |
return our_wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment