|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import pyya |
| 6 | + |
| 7 | + |
| 8 | +config_path = Path(__file__).parent / 'testdata/config.yaml' |
| 9 | +default_config_path = Path(__file__).parent / 'testdata/default.config.yaml' |
| 10 | +config_wrong_ext = Path(__file__).parent / 'testdata/config.txt' |
| 11 | +config_corrupted = Path(__file__).parent / 'testdata/corrupted.yaml' |
| 12 | + |
| 13 | +config_extra = Path(__file__).parent / 'testdata/extra.yaml' |
| 14 | +default_config_extra = Path(__file__).parent / 'testdata/default_extra.yaml' |
| 15 | + |
| 16 | + |
| 17 | +def test_raise_err_default_file_not_found() -> None: |
| 18 | + with pytest.raises(pyya.PyyaError, match=r'missing.yaml file is missing or empty'): |
| 19 | + _ = pyya.init_config( |
| 20 | + config=config_path, |
| 21 | + default_config='missing.yaml', |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def test_raise_err_wrong_ext() -> None: |
| 26 | + with pytest.raises(pyya.PyyaError, match=r'config.txt file format is not supported'): |
| 27 | + _ = pyya.init_config( |
| 28 | + config=config_wrong_ext, |
| 29 | + default_config=default_config_path, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def test_raise_err_default_wrong_ext() -> None: |
| 34 | + with pytest.raises(pyya.PyyaError, match=r'config.txt file format is not supported'): |
| 35 | + _ = pyya.init_config( |
| 36 | + config=config_path, |
| 37 | + default_config=config_wrong_ext, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_raise_err_corrupted() -> None: |
| 42 | + with pytest.raises(pyya.PyyaError, match=r'corrupted.yaml file is corrupted'): |
| 43 | + _ = pyya.init_config( |
| 44 | + config=config_corrupted, |
| 45 | + default_config=default_config_path, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_raise_err_default_corrupted() -> None: |
| 50 | + with pytest.raises(pyya.PyyaError, match=r'corrupted.yaml file is corrupted'): |
| 51 | + _ = pyya.init_config( |
| 52 | + config=config_path, |
| 53 | + default_config=config_corrupted, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_raise_err_sections_ignored_on_merge() -> None: |
| 58 | + with pytest.raises(pyya.PyyaError, match=r'Failed parsing `sections_ignored_on_merge`'): |
| 59 | + _ = pyya.init_config( |
| 60 | + config=config_path, |
| 61 | + default_config=default_config_path, |
| 62 | + sections_ignored_on_merge=[0], # type: ignore |
| 63 | + validate_data_types=False, |
| 64 | + replace_dashes_with_underscores=True, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def test_no_error_config_not_found() -> None: |
| 69 | + _ = pyya.init_config(config='missing.yaml', default_config=default_config_path) |
| 70 | + |
| 71 | + |
| 72 | +def test_raise_err_extra_sections() -> None: |
| 73 | + with pytest.raises(pyya.PyyaError, match=r'Extra inputs are not permitted'): |
| 74 | + _ = pyya.init_config( |
| 75 | + config=config_extra, |
| 76 | + default_config=default_config_extra, |
| 77 | + allow_extra_sections=False, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def test_raise_err_extra_sections_access() -> None: |
| 82 | + with pytest.raises(AttributeError, match=r'garbage'): |
| 83 | + config = pyya.init_config( |
| 84 | + config=config_extra, |
| 85 | + default_config=default_config_extra, |
| 86 | + ) |
| 87 | + _ = config.database.garbage |
0 commit comments