Created
May 5, 2017 18:29
-
-
Save greenkey/531f80679802bf9bbcea8887a83496e3 to your computer and use it in GitHub Desktop.
how to avoid django-memoize during test
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
# Supposing you're using the django-memoize package, you could have the | |
# following view memoized: | |
# file: your_module/views.py | |
# ... | |
@memoize(timeout=60) | |
def very_heavy_view(request): | |
time_consuming_function() | |
# ... | |
# ... | |
# But during the tests you don't want to wait a minute to execute, and | |
# usually you mock the external functions like `time_consuming_function()` | |
# So here it is, put this in your settings.py | |
# file: your_app/settings.py | |
# ... | |
IS_TESTING = lambda: len(sys.argv) > 1 and sys.argv[1] == 'test' | |
# ... | |
# Then change the memoize decorator as follows | |
# file: your_module/views.py | |
# ... | |
from django.conf import settings | |
# ... | |
@memoize(timeout=60, unless=settings.IS_TESTING) | |
def very_heavy_view(request): | |
time_consuming_function() | |
# ... | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Tobia for the like #19
http://stackoverflow.com/questions/4088253/django-how-to-detect-test-environment