Skip to content

Commit

Permalink
Fix loading setting with pytest-django and django 3.1 (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky authored Dec 9, 2020
1 parent 25931cc commit 9d349f5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
23 changes: 20 additions & 3 deletions app_helper/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,26 @@ def setup(app, helper_module, extra_args=None, use_cms=False):
"""

def _pytest_setup(settings, module):
if not getattr(settings, "SECRET_KEY", None):
settings.SECRET_KEY = "SECRET"
excluded_settings = {"PASSWORD_RESET_TIMEOUT": "PASSWORD_RESET_TIMEOUT_DAYS"}
default_settings = {"SECRET_KEY": "secret"}
for setting in dir(settings):
if setting.isupper():
setting_value = getattr(settings, setting)

# Empty settings value in the original value which must have a default are checked on
# default_settings dictionary and default is set, if available
default_value = default_settings.get(setting, None)
if default_value and not setting_value:
setting_value = default_value

# If two settings exclude each other, we check if the alternate setting is already defined
# in the settings module and if not, we set the current setting
alternate_setting = excluded_settings.get(setting, None)
alternate_setting_value = None
if alternate_setting:
alternate_setting_value = getattr(settings, alternate_setting, None)
if not alternate_setting_value:
setattr(module, setting, setting_value)

helper = helper_module.__file__
argv = [os.path.basename(helper), app, "setup", "--extra-settings={}".format(helper)]
Expand All @@ -73,7 +91,6 @@ def _pytest_setup(settings, module):
settings = runner(argv)
if "pytest_django" in sys.modules:
_pytest_setup(settings, helper_module)
_pytest_setup(settings, helper_module)
return settings


Expand Down
1 change: 1 addition & 0 deletions changes/202.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix loading setting with pytest-django and django 3.1
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ passenv =
PYTEST_*

[testenv:nocms]
basepython = python3.7
alwayscopy = True

[testenv:pep8]
commands =
Expand Down

0 comments on commit 9d349f5

Please sign in to comment.