Skip to content

Instantly share code, notes, and snippets.

@greenkey
Created May 5, 2017 18:29
Show Gist options
  • Save greenkey/531f80679802bf9bbcea8887a83496e3 to your computer and use it in GitHub Desktop.
Save greenkey/531f80679802bf9bbcea8887a83496e3 to your computer and use it in GitHub Desktop.
how to avoid django-memoize during test
# 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()
# ...
# ...
@greenkey
Copy link
Author

greenkey commented May 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment