|
| 1 | +"""Configuration(ini_files=[...]) tests.""" |
| 2 | + |
| 3 | +from dependency_injector import providers |
| 4 | +from pytest import fixture, mark, raises |
| 5 | + |
| 6 | + |
| 7 | +@fixture |
| 8 | +def config(config_type, ini_config_file_1, ini_config_file_2): |
| 9 | + if config_type == "strict": |
| 10 | + return providers.Configuration(strict=True) |
| 11 | + elif config_type == "default": |
| 12 | + return providers.Configuration(ini_files=[ini_config_file_1, ini_config_file_2]) |
| 13 | + else: |
| 14 | + raise ValueError("Undefined config type \"{0}\"".format(config_type)) |
| 15 | + |
| 16 | + |
| 17 | +def test_load(config): |
| 18 | + config.load() |
| 19 | + |
| 20 | + assert config() == { |
| 21 | + "section1": { |
| 22 | + "value1": "11", |
| 23 | + "value11": "11", |
| 24 | + }, |
| 25 | + "section2": { |
| 26 | + "value2": "2", |
| 27 | + }, |
| 28 | + "section3": { |
| 29 | + "value3": "3", |
| 30 | + }, |
| 31 | + } |
| 32 | + assert config.section1() == {"value1": "11", "value11": "11"} |
| 33 | + assert config.section1.value1() == "11" |
| 34 | + assert config.section1.value11() == "11" |
| 35 | + assert config.section2() == {"value2": "2"} |
| 36 | + assert config.section2.value2() == "2" |
| 37 | + assert config.section3() == {"value3": "3"} |
| 38 | + assert config.section3.value3() == "3" |
| 39 | + |
| 40 | + |
| 41 | +def test_get_files(config, ini_config_file_1, ini_config_file_2): |
| 42 | + assert config.get_ini_files() == [ini_config_file_1, ini_config_file_2] |
| 43 | + |
| 44 | + |
| 45 | +def test_set_files(config): |
| 46 | + config.set_ini_files(["file1.ini", "file2.ini"]) |
| 47 | + assert config.get_ini_files() == ["file1.ini", "file2.ini"] |
| 48 | + |
| 49 | + |
| 50 | +def test_file_does_not_exist(config): |
| 51 | + config.set_ini_files(["./does_not_exist.ini"]) |
| 52 | + config.load() |
| 53 | + assert config() == {} |
| 54 | + |
| 55 | + |
| 56 | +@mark.parametrize("config_type", ["strict"]) |
| 57 | +def test_file_does_not_exist_strict_mode(config): |
| 58 | + config.set_ini_files(["./does_not_exist.ini"]) |
| 59 | + with raises(IOError): |
| 60 | + config.load() |
| 61 | + assert config() == {} |
| 62 | + |
| 63 | + |
| 64 | +def test_required_file_does_not_exist(config): |
| 65 | + config.set_ini_files(["./does_not_exist.ini"]) |
| 66 | + with raises(IOError): |
| 67 | + config.load(required=True) |
| 68 | + |
| 69 | + |
| 70 | +@mark.parametrize("config_type", ["strict"]) |
| 71 | +def test_not_required_file_does_not_exist_strict_mode(config): |
| 72 | + config.set_ini_files(["./does_not_exist.ini"]) |
| 73 | + config.load(required=False) |
| 74 | + assert config() == {} |
| 75 | + |
| 76 | + |
| 77 | +def test_missing_envs_required(config, ini_config_file_3): |
| 78 | + with open(ini_config_file_3, "w") as file: |
| 79 | + file.write( |
| 80 | + "[section]\n" |
| 81 | + "undefined=${UNDEFINED}\n" |
| 82 | + ) |
| 83 | + config.set_ini_files([ini_config_file_3]) |
| 84 | + with raises(ValueError, match="Missing required environment variable \"UNDEFINED\""): |
| 85 | + config.load(envs_required=True) |
| 86 | + |
| 87 | + |
| 88 | +@mark.parametrize("config_type", ["strict"]) |
| 89 | +def test_missing_envs_not_required_in_strict_mode(config, ini_config_file_3): |
| 90 | + with open(ini_config_file_3, "w") as file: |
| 91 | + file.write( |
| 92 | + "[section]\n" |
| 93 | + "undefined=${UNDEFINED}\n" |
| 94 | + ) |
| 95 | + config.set_ini_files([ini_config_file_3]) |
| 96 | + config.load(envs_required=False) |
| 97 | + assert config.section.undefined() == "" |
0 commit comments