1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
"""Tests which check the various ways you can set DJANGO_SETTINGS_MODULE
If these tests fail you probably forgot to install django-configurations.
"""
import pytest
pytest.importorskip("configurations")
BARE_SETTINGS = """
from configurations import Configuration
class MySettings(Configuration):
# At least one database must be configured
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
},
}
SECRET_KEY = 'foobar'
"""
def test_dc_env(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env")
monkeypatch.setenv("DJANGO_CONFIGURATION", "MySettings")
pkg = pytester.mkpydir("tpkg")
settings = pkg.joinpath("settings_env.py")
settings.write_text(BARE_SETTINGS)
pytester.makepyfile(
"""
import os
def test_settings():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_env'
assert os.environ['DJANGO_CONFIGURATION'] == 'MySettings'
"""
)
result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines(
[
"django: version: *, settings: tpkg.settings_env (from env), "
"configuration: MySettings (from env)",
"* 1 passed*",
]
)
assert result.ret == 0
def test_dc_env_overrides_ini(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env")
monkeypatch.setenv("DJANGO_CONFIGURATION", "MySettings")
pytester.makeini(
"""
[pytest]
DJANGO_SETTINGS_MODULE = DO_NOT_USE_ini
DJANGO_CONFIGURATION = DO_NOT_USE_ini
"""
)
pkg = pytester.mkpydir("tpkg")
settings = pkg.joinpath("settings_env.py")
settings.write_text(BARE_SETTINGS)
pytester.makepyfile(
"""
import os
def test_ds():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_env'
assert os.environ['DJANGO_CONFIGURATION'] == 'MySettings'
"""
)
result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines(
[
"django: version: *, settings: tpkg.settings_env (from env), "
"configuration: MySettings (from env)",
"* 1 passed*",
]
)
assert result.ret == 0
def test_dc_ini(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
pytester.makeini(
"""
[pytest]
DJANGO_SETTINGS_MODULE = tpkg.settings_ini
DJANGO_CONFIGURATION = MySettings
"""
)
pkg = pytester.mkpydir("tpkg")
settings = pkg.joinpath("settings_ini.py")
settings.write_text(BARE_SETTINGS)
pytester.makepyfile(
"""
import os
def test_ds():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_ini'
assert os.environ['DJANGO_CONFIGURATION'] == 'MySettings'
"""
)
result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines(
[
"django: version: *, settings: tpkg.settings_ini (from ini), "
"configuration: MySettings (from ini)",
"* 1 passed*",
]
)
assert result.ret == 0
def test_dc_option(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DO_NOT_USE_env")
monkeypatch.setenv("DJANGO_CONFIGURATION", "DO_NOT_USE_env")
pytester.makeini(
"""
[pytest]
DJANGO_SETTINGS_MODULE = DO_NOT_USE_ini
DJANGO_CONFIGURATION = DO_NOT_USE_ini
"""
)
pkg = pytester.mkpydir("tpkg")
settings = pkg.joinpath("settings_opt.py")
settings.write_text(BARE_SETTINGS)
pytester.makepyfile(
"""
import os
def test_ds():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_opt'
assert os.environ['DJANGO_CONFIGURATION'] == 'MySettings'
"""
)
result = pytester.runpytest_subprocess("--ds=tpkg.settings_opt", "--dc=MySettings")
result.stdout.fnmatch_lines(
[
"django: version: *, settings: tpkg.settings_opt (from option),"
" configuration: MySettings (from option)",
"* 1 passed*",
]
)
assert result.ret == 0
|