mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-06 14:43:17 +03:00
Move config test fixtures
This commit is contained in:
parent
93001c2627
commit
f51a946973
|
@ -1,5 +1,7 @@
|
|||
"""Fixtures module."""
|
||||
|
||||
import os
|
||||
|
||||
from dependency_injector import providers
|
||||
from pytest import fixture
|
||||
|
||||
|
@ -19,6 +21,46 @@ def config(config_type):
|
|||
raise ValueError("Undefined config type \"{0}\"".format(config_type))
|
||||
|
||||
|
||||
@fixture
|
||||
def ini_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 ini_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 ini_config_file_3(tmp_path):
|
||||
ini_config_file_3 = str(tmp_path / "config_1.ini")
|
||||
with open(ini_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"[section1]\n"
|
||||
"value1=${CONFIG_TEST_ENV}\n"
|
||||
"value2=${CONFIG_TEST_PATH}/path\n"
|
||||
)
|
||||
return ini_config_file_3
|
||||
|
||||
|
||||
@fixture
|
||||
def yaml_config_file_1(tmp_path):
|
||||
config_file = str(tmp_path / "config_1.yml")
|
||||
|
@ -57,3 +99,14 @@ def yaml_config_file_3(tmp_path):
|
|||
" value2: ${CONFIG_TEST_PATH}/path\n"
|
||||
)
|
||||
return yaml_config_file_3
|
||||
|
||||
|
||||
@fixture(autouse=True)
|
||||
def environment_variables():
|
||||
os.environ["CONFIG_TEST_ENV"] = "test-value"
|
||||
os.environ["CONFIG_TEST_PATH"] = "test-path"
|
||||
os.environ["DEFINED"] = "defined"
|
||||
yield
|
||||
os.environ.pop("CONFIG_TEST_ENV", None)
|
||||
os.environ.pop("CONFIG_TEST_PATH", None)
|
||||
os.environ.pop("DEFINED", None)
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
"""Configuration.from_env() tests."""
|
||||
|
||||
import os
|
||||
|
||||
from pytest import fixture, mark, raises
|
||||
|
||||
|
||||
@fixture(autouse=True)
|
||||
def environment_variables():
|
||||
os.environ["CONFIG_TEST_ENV"] = "test-value"
|
||||
yield
|
||||
os.environ.pop("CONFIG_TEST_ENV", None)
|
||||
from pytest import mark, raises
|
||||
|
||||
|
||||
def test(config):
|
||||
|
|
|
@ -1,39 +1,11 @@
|
|||
"""Configuration.from_ini() tests."""
|
||||
|
||||
from dependency_injector import errors
|
||||
from pytest import fixture, mark, raises
|
||||
from pytest import 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
|
||||
|
||||
|
||||
def test(config, config_file_1):
|
||||
config.from_ini(config_file_1)
|
||||
def test(config, ini_config_file_1):
|
||||
config.from_ini(ini_config_file_1)
|
||||
|
||||
assert config() == {"section1": {"value1": "1"}, "section2": {"value2": "2"}}
|
||||
assert config.section1() == {"value1": "1"}
|
||||
|
@ -42,8 +14,8 @@ def test(config, config_file_1):
|
|||
assert config.section2.value2() == "2"
|
||||
|
||||
|
||||
def test_option(config, config_file_1):
|
||||
config.option.from_ini(config_file_1)
|
||||
def test_option(config, ini_config_file_1):
|
||||
config.option.from_ini(ini_config_file_1)
|
||||
|
||||
assert config() == {"option": {"section1": {"value1": "1"}, "section2": {"value2": "2"}}}
|
||||
assert config.option() == {"section1": {"value1": "1"}, "section2": {"value2": "2"}}
|
||||
|
@ -53,9 +25,9 @@ def test_option(config, config_file_1):
|
|||
assert config.option.section2.value2() == "2"
|
||||
|
||||
|
||||
def test_merge(config, config_file_1, config_file_2):
|
||||
config.from_ini(config_file_1)
|
||||
config.from_ini(config_file_2)
|
||||
def test_merge(config, ini_config_file_1, ini_config_file_2):
|
||||
config.from_ini(ini_config_file_1)
|
||||
config.from_ini(ini_config_file_2)
|
||||
|
||||
assert config() == {
|
||||
"section1": {
|
||||
|
|
|
@ -2,34 +2,11 @@
|
|||
|
||||
import os
|
||||
|
||||
from pytest import fixture, mark, raises
|
||||
from pytest import 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"
|
||||
os.environ["CONFIG_TEST_PATH"] = "test-path"
|
||||
os.environ["DEFINED"] = "defined"
|
||||
yield
|
||||
os.environ.pop("CONFIG_TEST_ENV", None)
|
||||
os.environ.pop("CONFIG_TEST_PATH", None)
|
||||
os.environ.pop("DEFINED", None)
|
||||
|
||||
|
||||
def test_env_variable_interpolation(config, config_file):
|
||||
config.from_ini(config_file)
|
||||
def test_env_variable_interpolation(config, ini_config_file_3):
|
||||
config.from_ini(ini_config_file_3)
|
||||
|
||||
assert config() == {
|
||||
"section1": {
|
||||
|
@ -45,11 +22,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, ini_config_file_3):
|
||||
del os.environ["CONFIG_TEST_ENV"]
|
||||
del os.environ["CONFIG_TEST_PATH"]
|
||||
|
||||
config.from_ini(config_file)
|
||||
config.from_ini(ini_config_file_3)
|
||||
|
||||
assert config() == {
|
||||
"section1": {
|
||||
|
@ -65,32 +42,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, ini_config_file_3):
|
||||
with open(ini_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"[section]\n"
|
||||
"undefined=${UNDEFINED}\n"
|
||||
)
|
||||
with raises(ValueError, match="Missing required environment variable \"UNDEFINED\""):
|
||||
config.from_ini(config_file, envs_required=True)
|
||||
config.from_ini(ini_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, ini_config_file_3):
|
||||
with open(ini_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"[section]\n"
|
||||
"undefined=${UNDEFINED}\n"
|
||||
)
|
||||
with raises(ValueError, match="Missing required environment variable \"UNDEFINED\""):
|
||||
config.from_ini(config_file)
|
||||
config.from_ini(ini_config_file_3)
|
||||
|
||||
|
||||
def test_option_missing_envs_not_required(config, config_file):
|
||||
def test_option_missing_envs_not_required(config, ini_config_file_3):
|
||||
del os.environ["CONFIG_TEST_ENV"]
|
||||
del os.environ["CONFIG_TEST_PATH"]
|
||||
|
||||
config.option.from_ini(config_file)
|
||||
config.option.from_ini(ini_config_file_3)
|
||||
|
||||
assert config.option() == {
|
||||
"section1": {
|
||||
|
@ -106,29 +83,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, ini_config_file_3):
|
||||
with open(ini_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_ini(config_file, envs_required=True)
|
||||
config.option.from_ini(ini_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, ini_config_file_3):
|
||||
with open(ini_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_ini(config_file)
|
||||
config.option.from_ini(ini_config_file_3)
|
||||
|
||||
|
||||
def test_default_values(config, config_file):
|
||||
with open(config_file, "w") as file:
|
||||
def test_default_values(config, ini_config_file_3):
|
||||
with open(ini_config_file_3, "w") as file:
|
||||
file.write(
|
||||
"[section]\n"
|
||||
"defined_with_default=${DEFINED:default}\n"
|
||||
|
@ -136,7 +113,7 @@ def test_default_values(config, config_file):
|
|||
"complex=${DEFINED}/path/${DEFINED:default}/${UNDEFINED}/${UNDEFINED:default}\n"
|
||||
)
|
||||
|
||||
config.from_ini(config_file)
|
||||
config.from_ini(ini_config_file_3)
|
||||
|
||||
assert config.section() == {
|
||||
"defined_with_default": "defined",
|
||||
|
|
|
@ -3,18 +3,7 @@
|
|||
import os
|
||||
|
||||
import yaml
|
||||
from pytest import fixture, mark, raises
|
||||
|
||||
|
||||
@fixture(autouse=True)
|
||||
def environment_variables():
|
||||
os.environ["CONFIG_TEST_ENV"] = "test-value"
|
||||
os.environ["CONFIG_TEST_PATH"] = "test-path"
|
||||
os.environ["DEFINED"] = "defined"
|
||||
yield
|
||||
os.environ.pop("CONFIG_TEST_ENV", None)
|
||||
os.environ.pop("CONFIG_TEST_PATH", None)
|
||||
os.environ.pop("DEFINED", None)
|
||||
from pytest import mark, raises
|
||||
|
||||
|
||||
def test_env_variable_interpolation(config, yaml_config_file_3):
|
||||
|
|
Loading…
Reference in New Issue
Block a user