|
| 1 | +import os |
| 2 | +from os.path import dirname |
| 3 | +from os.path import join |
| 4 | + |
| 5 | +import pytest |
| 6 | +from _pytest.tmpdir import TempPathFactory |
| 7 | + |
| 8 | +from dipdup.config import DipDupConfig |
| 9 | +from dipdup.exceptions import ConfigurationError |
| 10 | + |
| 11 | + |
| 12 | +class TestCustomConfig: |
| 13 | + @pytest.fixture(scope="session") |
| 14 | + def dummy_config_path(self) -> str: |
| 15 | + return join(dirname(dirname(__file__)), 'dipdup.yml') |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def appended_config_path(dummy_config_path: str, tmp_path_factory: TempPathFactory, append_raw: str) -> str: |
| 19 | + config_file = tmp_path_factory.mktemp('config') / 'dipdup.yml' |
| 20 | + with open(dummy_config_path, 'r') as f: |
| 21 | + config_raw = f.read() |
| 22 | + |
| 23 | + with open(config_file, 'a') as f: |
| 24 | + f.write(config_raw) |
| 25 | + f.write(append_raw) |
| 26 | + |
| 27 | + return str(config_file) |
| 28 | + |
| 29 | + @pytest.fixture( |
| 30 | + scope="session", |
| 31 | + params=( |
| 32 | + [ |
| 33 | + """ |
| 34 | +custom: |
| 35 | + foo: bar |
| 36 | + spam: |
| 37 | + - eggs |
| 38 | + - rice |
| 39 | +
|
| 40 | +""" |
| 41 | + ] |
| 42 | + ), |
| 43 | + ) |
| 44 | + def config_with_custom_section_path(self, dummy_config_path: str, tmp_path_factory: TempPathFactory, request) -> str: |
| 45 | + return self.appended_config_path(dummy_config_path, tmp_path_factory, request.param) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def test_empty_custom_section(dummy_config_path: str): |
| 49 | + config = DipDupConfig.load([dummy_config_path], False) |
| 50 | + config.initialize(True) |
| 51 | + assert hasattr(config, 'custom') |
| 52 | + assert config.custom == {} |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def test_custom_section_items(config_with_custom_section_path: str): |
| 56 | + config = DipDupConfig.load([config_with_custom_section_path], False) |
| 57 | + config.initialize(True) |
| 58 | + |
| 59 | + assert hasattr(config, 'custom') |
| 60 | + assert isinstance(config.custom, dict) |
| 61 | + |
| 62 | + assert config.custom['foo'] == 'bar' |
| 63 | + |
| 64 | + spam = config.custom['spam'] |
| 65 | + assert isinstance(spam, list) |
| 66 | + assert 'eggs' in spam |
| 67 | + assert 'rice' in spam |
| 68 | + |
| 69 | + @pytest.mark.parametrize( |
| 70 | + 'value, expected', |
| 71 | + ( |
| 72 | + ('${USER:-dipdup}', os.environ.get('USER')), |
| 73 | + ('${USER:-}', os.environ.get('USER')), |
| 74 | + ('${USER}', os.environ.get('USER')), |
| 75 | + ('${DEFINITELY_NOT_DEFINED:-default_value}', 'default_value'), |
| 76 | + ('${DEFINITELY_NOT_DEFINED:- some_spaces_is_ok }', 'some_spaces_is_ok'), |
| 77 | + ), |
| 78 | + ) |
| 79 | + def test_env_parsing_positive(self, value, expected, dummy_config_path, tmp_path_factory): |
| 80 | + append_raw = f""" |
| 81 | +custom: |
| 82 | + var_from_env: {value} |
| 83 | +""" |
| 84 | + config_path = self.appended_config_path(dummy_config_path, tmp_path_factory, append_raw) |
| 85 | + config = DipDupConfig.load([config_path], True) |
| 86 | + config.initialize(True) |
| 87 | + |
| 88 | + assert hasattr(config, 'custom') |
| 89 | + assert isinstance(config.custom, dict) |
| 90 | + |
| 91 | + assert config.custom['var_from_env'] == expected |
| 92 | + |
| 93 | + @pytest.mark.parametrize( |
| 94 | + 'value', |
| 95 | + ( |
| 96 | + '${DEFINITELY_NOT_DEFINED}', |
| 97 | + '${DEFINITELY_NOT_DEFINED:-}', |
| 98 | + ), |
| 99 | + ) |
| 100 | + def test_env_parsing_negative(self, value, dummy_config_path, tmp_path_factory): |
| 101 | + append_raw = f""" |
| 102 | +custom: |
| 103 | + var_from_env: {value} |
| 104 | +""" |
| 105 | + config_path = self.appended_config_path(dummy_config_path, tmp_path_factory, append_raw) |
| 106 | + with pytest.raises(ConfigurationError) as exception_info: |
| 107 | + DipDupConfig.load([config_path], True) |
| 108 | + |
| 109 | + assert str(exception_info.value) == 'Environment variable `DEFINITELY_NOT_DEFINED` is not set' |
0 commit comments