|
| 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