Skip to content

Commit 45f2f35

Browse files
authored
Merge pull request #14 from eadwinCode/configuration_test
Configuration Module Test
2 parents 7e8ab42 + 300d835 commit 45f2f35

6 files changed

Lines changed: 151 additions & 4 deletions

File tree

ellar/core/conf/config.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from .app_settings_models import ConfigValidationSchema
1212

1313

14+
class ConfigRuntimeError(RuntimeError):
15+
pass
16+
17+
1418
class Config(DataMutableMapper, AttributeDictAccessMixin):
1519
__slots__ = ("config_module", "_data")
1620

@@ -31,10 +35,13 @@ def __init__(
3135
self._data[setting] = getattr(default_settings, setting)
3236

3337
if self.config_module:
34-
mod = importlib.import_module(self.config_module)
35-
for setting in dir(mod):
36-
if setting.isupper():
37-
self._data[setting] = getattr(mod, setting)
38+
try:
39+
mod = importlib.import_module(self.config_module)
40+
for setting in dir(mod):
41+
if setting.isupper():
42+
self._data[setting] = getattr(mod, setting)
43+
except Exception as ex:
44+
raise ConfigRuntimeError(str(ex))
3845

3946
self._data.update(**mapping)
4047

File renamed without changes.

tests/notstarted/test_testclient_factory.py

Whitespace-only changes.

tests/test_conf/__init__.py

Whitespace-only changes.

tests/test_conf/settings.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from ellar.core.versioning import UrlPathAPIVersioning
2+
3+
DEBUG: bool = True
4+
5+
SECRET_KEY: str = "your-secret-key-changed"
6+
7+
INJECTOR_AUTO_BIND = True
8+
9+
TEMPLATES_AUTO_RELOAD = DEBUG
10+
11+
VERSIONING_SCHEME = UrlPathAPIVersioning()
12+
REDIRECT_SLASHES: bool = True
13+
STATIC_MOUNT_PATH: str = "/static-changed"
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import os
2+
3+
import pytest
4+
from pydantic.json import ENCODERS_BY_TYPE
5+
from starlette.responses import JSONResponse
6+
7+
from ellar.constants import ELLAR_CONFIG_MODULE
8+
from ellar.core.conf import Config
9+
from ellar.core.conf.config import ConfigRuntimeError
10+
from ellar.core.versioning import DefaultAPIVersioning, UrlPathAPIVersioning
11+
from ellar.exceptions import APIException, RequestValidationError
12+
13+
overriding_settings_path = "tests.test_conf.settings"
14+
15+
16+
def test_default_configurations():
17+
config = Config()
18+
19+
assert config.DEBUG is False
20+
assert config.DEFAULT_JSON_CLASS == JSONResponse
21+
assert config.SECRET_KEY == "your-secret-key"
22+
assert config.INJECTOR_AUTO_BIND is False
23+
24+
assert config.TEMPLATES_AUTO_RELOAD is None
25+
26+
assert isinstance(config.VERSIONING_SCHEME, DefaultAPIVersioning)
27+
assert config.REDIRECT_SLASHES is False
28+
29+
assert config.STATIC_FOLDER_PACKAGES == []
30+
assert config.STATIC_MOUNT_PATH == "/static"
31+
32+
assert config.MIDDLEWARE == []
33+
34+
assert RequestValidationError in config.APP_EXCEPTION_HANDLERS
35+
assert APIException in config.APP_EXCEPTION_HANDLERS
36+
37+
assert config.USER_CUSTOM_EXCEPTION_HANDLERS == {}
38+
39+
assert callable(config.DEFAULT_NOT_FOUND_HANDLER)
40+
assert config.DEFAULT_LIFESPAN_HANDLER is None
41+
42+
assert config.SERIALIZER_CUSTOM_ENCODER == ENCODERS_BY_TYPE
43+
44+
45+
def test_configuration_export_to_os_environment():
46+
os.environ.setdefault(ELLAR_CONFIG_MODULE, overriding_settings_path)
47+
config = Config()
48+
49+
assert config.DEBUG
50+
assert config.SECRET_KEY == "your-secret-key-changed"
51+
assert config.INJECTOR_AUTO_BIND is True
52+
assert config.TEMPLATES_AUTO_RELOAD is True
53+
assert isinstance(config.VERSIONING_SCHEME, UrlPathAPIVersioning)
54+
assert config.REDIRECT_SLASHES is True
55+
assert config.STATIC_MOUNT_PATH == "/static-changed"
56+
57+
del os.environ[ELLAR_CONFIG_MODULE]
58+
59+
60+
def test_configuration_raise_runtime_error_for_invalid_settings_module_path():
61+
os.environ.setdefault(ELLAR_CONFIG_MODULE, "tests.somewrongpath.settings")
62+
with pytest.raises(ConfigRuntimeError):
63+
Config()
64+
65+
del os.environ[ELLAR_CONFIG_MODULE]
66+
67+
with pytest.raises(ConfigRuntimeError):
68+
Config(config_module="tests.somewrongpath.settings")
69+
70+
71+
def test_configuration_settings_can_be_loaded_through_constructor():
72+
config = Config(config_module=overriding_settings_path)
73+
assert config.DEBUG
74+
assert config.SECRET_KEY == "your-secret-key-changed"
75+
assert config.INJECTOR_AUTO_BIND is True
76+
assert config.TEMPLATES_AUTO_RELOAD is True
77+
assert isinstance(config.VERSIONING_SCHEME, UrlPathAPIVersioning)
78+
assert config.REDIRECT_SLASHES is True
79+
assert config.STATIC_MOUNT_PATH == "/static-changed"
80+
81+
82+
def test_configuration_can_be_changed_during_instantiation():
83+
config = Config(
84+
config_module=overriding_settings_path,
85+
DEBUG=False,
86+
SOME_NEW_CONFIGS="some new configuration values",
87+
TEMPLATES_AUTO_RELOAD=False,
88+
)
89+
90+
assert config.DEBUG is False
91+
assert config.TEMPLATES_AUTO_RELOAD is False
92+
assert config.SOME_NEW_CONFIGS == "some new configuration values"
93+
94+
95+
def test_can_set_defaults_a_configuration_instance_once():
96+
config = Config(config_module=overriding_settings_path)
97+
with pytest.raises(Exception):
98+
config.SOME_NEW_CONFIGS
99+
config.setdefault("SOME_NEW_CONFIGS", "some new configuration values")
100+
assert config.SOME_NEW_CONFIGS == "some new configuration values"
101+
102+
config.setdefault("SOME_NEW_CONFIGS", "some new configuration values changed")
103+
assert config.SOME_NEW_CONFIGS == "some new configuration values"
104+
105+
106+
def test_can_change_configuration_values_after_instantiation():
107+
config = Config(config_module=overriding_settings_path)
108+
assert config.DEBUG
109+
110+
config["DEBUG"] = False
111+
assert config.DEBUG is False
112+
113+
config["SOME_NEW_CONFIGS"] = "some new configuration values"
114+
assert config.SOME_NEW_CONFIGS == "some new configuration values"
115+
116+
config["SOME_NEW_CONFIGS"] = "some new configuration values changed"
117+
assert config.SOME_NEW_CONFIGS == "some new configuration values changed"
118+
119+
config.config_module = "somethings"
120+
assert config.config_module == "somethings"
121+
122+
123+
def test_can_export_configuration_values():
124+
config = Config()
125+
values = list(config.values)
126+
127+
assert len(values) > 7

0 commit comments

Comments
 (0)