mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-13 20:33:51 +03:00
Add provider changes and tests
This commit is contained in:
parent
b97862cb9f
commit
93001c2627
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -114,6 +114,7 @@ cdef class Configuration(Object):
|
|||
cdef str __name
|
||||
cdef bint __strict
|
||||
cdef dict __children
|
||||
cdef list __yaml_files
|
||||
cdef object __weakref__
|
||||
|
||||
|
||||
|
|
|
@ -216,7 +216,7 @@ class TypedConfigurationOption(Callable[T]):
|
|||
|
||||
class Configuration(Object[Any]):
|
||||
DEFAULT_NAME: str = 'config'
|
||||
def __init__(self, name: str = DEFAULT_NAME, default: Optional[Any] = None, *, strict: bool = False) -> None: ...
|
||||
def __init__(self, name: str = DEFAULT_NAME, default: Optional[Any] = None, *, strict: bool = False, yaml_files: Optional[_Iterable[Union[Path, str]]] = None) -> None: ...
|
||||
def __enter__(self) -> Configuration : ...
|
||||
def __exit__(self, *exc_info: Any) -> None: ...
|
||||
def __getattr__(self, item: str) -> ConfigurationOption: ...
|
||||
|
@ -234,6 +234,11 @@ class Configuration(Object[Any]):
|
|||
def get_children(self) -> _Dict[str, ConfigurationOption]: ...
|
||||
def set_children(self, children: _Dict[str, ConfigurationOption]) -> Configuration: ...
|
||||
|
||||
def get_yaml_files(self) -> _List[Union[Path, str]]: ...
|
||||
def set_yaml_files(self, files: _Iterable[Union[Path, str]]) -> Configuration: ...
|
||||
|
||||
def load(self, required: bool = False, envs_required: bool = False) -> None: ...
|
||||
|
||||
def get(self, selector: str) -> Any: ...
|
||||
def set(self, selector: str, value: Any) -> OverridingContext[P]: ...
|
||||
def reset_cache(self) -> None: ...
|
||||
|
|
|
@ -1755,14 +1755,19 @@ cdef class Configuration(Object):
|
|||
|
||||
DEFAULT_NAME = 'config'
|
||||
|
||||
def __init__(self, name=DEFAULT_NAME, default=None, strict=False):
|
||||
def __init__(self, name=DEFAULT_NAME, default=None, strict=False, yaml_files=None):
|
||||
self.__name = name
|
||||
self.__strict = strict
|
||||
self.__children = {}
|
||||
self.__yaml_files = []
|
||||
|
||||
super().__init__(provides={})
|
||||
self.set_default(default)
|
||||
|
||||
if yaml_files is None:
|
||||
yaml_files = []
|
||||
self.set_yaml_files(yaml_files)
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
copied = memo.get(id(self))
|
||||
if copied is not None:
|
||||
|
@ -1773,6 +1778,7 @@ cdef class Configuration(Object):
|
|||
copied.set_default(self.get_default())
|
||||
copied.set_strict(self.get_strict())
|
||||
copied.set_children(deepcopy(self.get_children(), memo))
|
||||
copied.set_yaml_files(self.get_yaml_files())
|
||||
|
||||
self._copy_overridings(copied, memo)
|
||||
return copied
|
||||
|
@ -1846,6 +1852,35 @@ cdef class Configuration(Object):
|
|||
self.__children = children
|
||||
return self
|
||||
|
||||
def get_yaml_files(self):
|
||||
"""Return list of YAML files."""
|
||||
return self.__yaml_files
|
||||
|
||||
def set_yaml_files(self, files):
|
||||
"""Set list of YAML files."""
|
||||
self.__yaml_files = list(files)
|
||||
return self
|
||||
|
||||
def load(self, required=UNDEFINED, envs_required=False):
|
||||
"""Load configuration.
|
||||
|
||||
This method loads configuration from configuration files or pydantic settings that
|
||||
were set earlier with set_*() methods or provided to the __init__(), e.g.:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
config = providers.Configuration(yaml_files=[file1, file2])
|
||||
config.load()
|
||||
|
||||
:param required: When required is True, raise an exception if file does not exist.
|
||||
:type required: bool
|
||||
|
||||
:param envs_required: When True, raises an error on undefined environment variable.
|
||||
:type envs_required: bool
|
||||
"""
|
||||
for file in self.get_yaml_files():
|
||||
self.from_yaml(file, required=required, envs_required=envs_required)
|
||||
|
||||
def get(self, selector, required=False):
|
||||
"""Return configuration option.
|
||||
|
||||
|
|
|
@ -17,3 +17,43 @@ def config(config_type):
|
|||
return providers.Configuration()
|
||||
else:
|
||||
raise ValueError("Undefined config type \"{0}\"".format(config_type))
|
||||
|
||||
|
||||
@fixture
|
||||
def yaml_config_file_1(tmp_path):
|
||||
config_file = str(tmp_path / "config_1.yml")
|
||||
with open(config_file, "w") as file:
|
||||
file.write(
|
||||
"section1:\n"
|
||||
" value1: 1\n"
|
||||
"\n"
|
||||
"section2:\n"
|
||||
" value2: 2\n"
|
||||
)
|
||||
return config_file
|
||||
|
||||
|
||||
@fixture
|
||||
def yaml_config_file_2(tmp_path):
|
||||
config_file = str(tmp_path / "config_2.yml")
|
||||
with open(config_file, "w") as file:
|
||||
file.write(
|
||||
"section1:\n"
|
||||
" value1: 11\n"
|
||||
" value11: 11\n"
|
||||
"section3:\n"
|
||||
" value3: 3\n"
|
||||
)
|
||||
return config_file
|
||||
|
||||
|
||||
@fixture
|
||||
def yaml_config_file_3(tmp_path):
|
||||
yaml_config_file_3 = str(tmp_path / "config_1.yml")
|
||||
with open(yaml_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"section1:\n"
|
||||
" value1: ${CONFIG_TEST_ENV}\n"
|
||||
" value2: ${CONFIG_TEST_PATH}/path\n"
|
||||
)
|
||||
return yaml_config_file_3
|
||||
|
|
|
@ -4,34 +4,6 @@ from dependency_injector import providers, errors
|
|||
from pytest import fixture, mark, raises
|
||||
|
||||
|
||||
@fixture
|
||||
def config_file_1(tmp_path):
|
||||
config_file = str(tmp_path / "config_1.ini")
|
||||
with open(config_file, "w") as file:
|
||||
file.write(
|
||||
"section1:\n"
|
||||
" value1: 1\n"
|
||||
"\n"
|
||||
"section2:\n"
|
||||
" value2: 2\n"
|
||||
)
|
||||
return config_file
|
||||
|
||||
|
||||
@fixture
|
||||
def config_file_2(tmp_path):
|
||||
config_file = str(tmp_path / "config_2.ini")
|
||||
with open(config_file, "w") as file:
|
||||
file.write(
|
||||
"section1:\n"
|
||||
" value1: 11\n"
|
||||
" value11: 11\n"
|
||||
"section3:\n"
|
||||
" value3: 3\n"
|
||||
)
|
||||
return config_file
|
||||
|
||||
|
||||
@fixture
|
||||
def no_yaml_module_installed():
|
||||
yaml = providers.yaml
|
||||
|
@ -40,8 +12,8 @@ def no_yaml_module_installed():
|
|||
providers.yaml = yaml
|
||||
|
||||
|
||||
def test(config, config_file_1):
|
||||
config.from_yaml(config_file_1)
|
||||
def test(config, yaml_config_file_1):
|
||||
config.from_yaml(yaml_config_file_1)
|
||||
|
||||
assert config() == {"section1": {"value1": 1}, "section2": {"value2": 2}}
|
||||
assert config.section1() == {"value1": 1}
|
||||
|
@ -50,9 +22,9 @@ def test(config, config_file_1):
|
|||
assert config.section2.value2() == 2
|
||||
|
||||
|
||||
def test_merge(config, config_file_1, config_file_2):
|
||||
config.from_yaml(config_file_1)
|
||||
config.from_yaml(config_file_2)
|
||||
def test_merge(config, yaml_config_file_1, yaml_config_file_2):
|
||||
config.from_yaml(yaml_config_file_1)
|
||||
config.from_yaml(yaml_config_file_2)
|
||||
|
||||
assert config() == {
|
||||
"section1": {
|
||||
|
@ -121,9 +93,9 @@ def test_not_required_option_file_does_not_exist_strict_mode(config):
|
|||
|
||||
|
||||
@mark.usefixtures("no_yaml_module_installed")
|
||||
def test_no_yaml_installed(config, config_file_1):
|
||||
def test_no_yaml_installed(config, yaml_config_file_1):
|
||||
with raises(errors.Error) as error:
|
||||
config.from_yaml(config_file_1)
|
||||
config.from_yaml(yaml_config_file_1)
|
||||
assert error.value.args[0] == (
|
||||
"Unable to load yaml configuration - PyYAML is not installed. "
|
||||
"Install PyYAML or install Dependency Injector with yaml extras: "
|
||||
|
@ -132,9 +104,9 @@ def test_no_yaml_installed(config, config_file_1):
|
|||
|
||||
|
||||
@mark.usefixtures("no_yaml_module_installed")
|
||||
def test_option_no_yaml_installed(config, config_file_1):
|
||||
def test_option_no_yaml_installed(config, yaml_config_file_1):
|
||||
with raises(errors.Error) as error:
|
||||
config.option.from_yaml(config_file_1)
|
||||
config.option.from_yaml(yaml_config_file_1)
|
||||
assert error.value.args[0] == (
|
||||
"Unable to load yaml configuration - PyYAML is not installed. "
|
||||
"Install PyYAML or install Dependency Injector with yaml extras: "
|
||||
|
|
|
@ -6,18 +6,6 @@ import yaml
|
|||
from pytest import fixture, mark, raises
|
||||
|
||||
|
||||
@fixture
|
||||
def config_file(tmp_path):
|
||||
config_file = str(tmp_path / "config_1.ini")
|
||||
with open(config_file, "w") as file:
|
||||
file.write(
|
||||
"section1:\n"
|
||||
" value1: ${CONFIG_TEST_ENV}\n"
|
||||
" value2: ${CONFIG_TEST_PATH}/path\n"
|
||||
)
|
||||
return config_file
|
||||
|
||||
|
||||
@fixture(autouse=True)
|
||||
def environment_variables():
|
||||
os.environ["CONFIG_TEST_ENV"] = "test-value"
|
||||
|
@ -29,8 +17,8 @@ def environment_variables():
|
|||
os.environ.pop("DEFINED", None)
|
||||
|
||||
|
||||
def test_env_variable_interpolation(config, config_file):
|
||||
config.from_yaml(config_file)
|
||||
def test_env_variable_interpolation(config, yaml_config_file_3):
|
||||
config.from_yaml(yaml_config_file_3)
|
||||
|
||||
assert config() == {
|
||||
"section1": {
|
||||
|
@ -46,11 +34,11 @@ def test_env_variable_interpolation(config, config_file):
|
|||
assert config.section1.value2() == "test-path/path"
|
||||
|
||||
|
||||
def test_missing_envs_not_required(config, config_file):
|
||||
def test_missing_envs_not_required(config, yaml_config_file_3):
|
||||
del os.environ["CONFIG_TEST_ENV"]
|
||||
del os.environ["CONFIG_TEST_PATH"]
|
||||
|
||||
config.from_yaml(config_file)
|
||||
config.from_yaml(yaml_config_file_3)
|
||||
|
||||
assert config() == {
|
||||
"section1": {
|
||||
|
@ -66,32 +54,32 @@ def test_missing_envs_not_required(config, config_file):
|
|||
assert config.section1.value2() == "/path"
|
||||
|
||||
|
||||
def test_missing_envs_required(config, config_file):
|
||||
with open(config_file, "w") as file:
|
||||
def test_missing_envs_required(config, yaml_config_file_3):
|
||||
with open(yaml_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"section:\n"
|
||||
" undefined: ${UNDEFINED}\n"
|
||||
)
|
||||
with raises(ValueError, match="Missing required environment variable \"UNDEFINED\""):
|
||||
config.from_yaml(config_file, envs_required=True)
|
||||
config.from_yaml(yaml_config_file_3, envs_required=True)
|
||||
|
||||
|
||||
@mark.parametrize("config_type", ["strict"])
|
||||
def test_missing_envs_strict_mode(config, config_file):
|
||||
with open(config_file, "w") as file:
|
||||
def test_missing_envs_strict_mode(config, yaml_config_file_3):
|
||||
with open(yaml_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"section:\n"
|
||||
" undefined: ${UNDEFINED}\n"
|
||||
)
|
||||
with raises(ValueError, match="Missing required environment variable \"UNDEFINED\""):
|
||||
config.from_yaml(config_file)
|
||||
config.from_yaml(yaml_config_file_3)
|
||||
|
||||
|
||||
def test_option_missing_envs_not_required(config, config_file):
|
||||
def test_option_missing_envs_not_required(config, yaml_config_file_3):
|
||||
del os.environ["CONFIG_TEST_ENV"]
|
||||
del os.environ["CONFIG_TEST_PATH"]
|
||||
|
||||
config.option.from_yaml(config_file)
|
||||
config.option.from_yaml(yaml_config_file_3)
|
||||
|
||||
assert config.option() == {
|
||||
"section1": {
|
||||
|
@ -107,29 +95,29 @@ def test_option_missing_envs_not_required(config, config_file):
|
|||
assert config.option.section1.value2() == "/path"
|
||||
|
||||
|
||||
def test_option_missing_envs_required(config, config_file):
|
||||
with open(config_file, "w") as file:
|
||||
def test_option_missing_envs_required(config, yaml_config_file_3):
|
||||
with open(yaml_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"section:\n"
|
||||
" undefined: ${UNDEFINED}\n"
|
||||
)
|
||||
with raises(ValueError, match="Missing required environment variable \"UNDEFINED\""):
|
||||
config.option.from_yaml(config_file, envs_required=True)
|
||||
config.option.from_yaml(yaml_config_file_3, envs_required=True)
|
||||
|
||||
|
||||
@mark.parametrize("config_type", ["strict"])
|
||||
def test_option_missing_envs_strict_mode(config, config_file):
|
||||
with open(config_file, "w") as file:
|
||||
def test_option_missing_envs_strict_mode(config, yaml_config_file_3):
|
||||
with open(yaml_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"section:\n"
|
||||
" undefined: ${UNDEFINED}\n"
|
||||
)
|
||||
with raises(ValueError, match="Missing required environment variable \"UNDEFINED\""):
|
||||
config.option.from_yaml(config_file)
|
||||
config.option.from_yaml(yaml_config_file_3)
|
||||
|
||||
|
||||
def test_default_values(config, config_file):
|
||||
with open(config_file, "w") as file:
|
||||
def test_default_values(config, yaml_config_file_3):
|
||||
with open(yaml_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"section:\n"
|
||||
" defined_with_default: ${DEFINED:default}\n"
|
||||
|
@ -137,7 +125,7 @@ def test_default_values(config, config_file):
|
|||
" complex: ${DEFINED}/path/${DEFINED:default}/${UNDEFINED}/${UNDEFINED:default}\n"
|
||||
)
|
||||
|
||||
config.from_yaml(config_file)
|
||||
config.from_yaml(yaml_config_file_3)
|
||||
|
||||
assert config.section() == {
|
||||
"defined_with_default": "defined",
|
||||
|
@ -146,8 +134,8 @@ def test_default_values(config, config_file):
|
|||
}
|
||||
|
||||
|
||||
def test_option_env_variable_interpolation(config, config_file):
|
||||
config.option.from_yaml(config_file)
|
||||
def test_option_env_variable_interpolation(config, yaml_config_file_3):
|
||||
config.option.from_yaml(yaml_config_file_3)
|
||||
|
||||
assert config.option() == {
|
||||
"section1": {
|
||||
|
@ -163,8 +151,8 @@ def test_option_env_variable_interpolation(config, config_file):
|
|||
assert config.option.section1.value2() == "test-path/path"
|
||||
|
||||
|
||||
def test_env_variable_interpolation_custom_loader(config, config_file):
|
||||
config.from_yaml(config_file, loader=yaml.UnsafeLoader)
|
||||
def test_env_variable_interpolation_custom_loader(config, yaml_config_file_3):
|
||||
config.from_yaml(yaml_config_file_3, loader=yaml.UnsafeLoader)
|
||||
|
||||
assert config.section1() == {
|
||||
"value1": "test-value",
|
||||
|
@ -174,8 +162,8 @@ def test_env_variable_interpolation_custom_loader(config, config_file):
|
|||
assert config.section1.value2() == "test-path/path"
|
||||
|
||||
|
||||
def test_option_env_variable_interpolation_custom_loader(config, config_file):
|
||||
config.option.from_yaml(config_file, loader=yaml.UnsafeLoader)
|
||||
def test_option_env_variable_interpolation_custom_loader(config, yaml_config_file_3):
|
||||
config.option.from_yaml(yaml_config_file_3, loader=yaml.UnsafeLoader)
|
||||
|
||||
assert config.option.section1() == {
|
||||
"value1": "test-value",
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
"""Configuration(yaml_files=[...]) tests."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from pytest import fixture, mark, raises
|
||||
|
||||
|
||||
@fixture
|
||||
def config(config_type, yaml_config_file_1, yaml_config_file_2):
|
||||
if config_type == "strict":
|
||||
return providers.Configuration(strict=True)
|
||||
elif config_type == "default":
|
||||
return providers.Configuration(yaml_files=[yaml_config_file_1, yaml_config_file_2])
|
||||
else:
|
||||
raise ValueError("Undefined config type \"{0}\"".format(config_type))
|
||||
|
||||
|
||||
def test_load(config):
|
||||
config.load()
|
||||
|
||||
assert config() == {
|
||||
"section1": {
|
||||
"value1": 11,
|
||||
"value11": 11,
|
||||
},
|
||||
"section2": {
|
||||
"value2": 2,
|
||||
},
|
||||
"section3": {
|
||||
"value3": 3,
|
||||
},
|
||||
}
|
||||
assert config.section1() == {"value1": 11, "value11": 11}
|
||||
assert config.section1.value1() == 11
|
||||
assert config.section1.value11() == 11
|
||||
assert config.section2() == {"value2": 2}
|
||||
assert config.section2.value2() == 2
|
||||
assert config.section3() == {"value3": 3}
|
||||
assert config.section3.value3() == 3
|
||||
|
||||
|
||||
def test_get_files(config, yaml_config_file_1, yaml_config_file_2):
|
||||
assert config.get_yaml_files() == [yaml_config_file_1, yaml_config_file_2]
|
||||
|
||||
|
||||
def test_set_files(config):
|
||||
config.set_yaml_files(["file1.yml", "file2.yml"])
|
||||
assert config.get_yaml_files() == ["file1.yml", "file2.yml"]
|
||||
|
||||
|
||||
def test_file_does_not_exist(config):
|
||||
config.set_yaml_files(["./does_not_exist.yml"])
|
||||
config.load()
|
||||
assert config() == {}
|
||||
|
||||
|
||||
@mark.parametrize("config_type", ["strict"])
|
||||
def test_file_does_not_exist_strict_mode(config):
|
||||
config.set_yaml_files(["./does_not_exist.yml"])
|
||||
with raises(IOError):
|
||||
config.load()
|
||||
assert config() == {}
|
||||
|
||||
|
||||
def test_required_file_does_not_exist(config):
|
||||
config.set_yaml_files(["./does_not_exist.yml"])
|
||||
with raises(IOError):
|
||||
config.load(required=True)
|
||||
|
||||
|
||||
@mark.parametrize("config_type", ["strict"])
|
||||
def test_not_required_file_does_not_exist_strict_mode(config):
|
||||
config.set_yaml_files(["./does_not_exist.yml"])
|
||||
config.load(required=False)
|
||||
assert config() == {}
|
Loading…
Reference in New Issue
Block a user