|
| 1 | +"""Module testing ConfigurationReader""" |
| 2 | +import json |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +import yaml |
| 9 | + |
| 10 | +from cdevents.cli.configuration_reader import ConfigurationReader |
| 11 | + |
| 12 | +# pylint: disable=missing-function-docstring, protected-access, missing-class-docstring |
| 13 | +TEST_DIR = Path(__file__).parent |
| 14 | +CONFIG_DIR: Path = Path(TEST_DIR, "configurations") |
| 15 | + |
| 16 | +YAML_CONFIG_1 = """--- |
| 17 | +configuration: |
| 18 | + source: |
| 19 | + name: cde-cli |
| 20 | + client: |
| 21 | + host: http://localhost:8080 |
| 22 | +""" |
| 23 | +DICT_CONFIG_1 = yaml.safe_load(YAML_CONFIG_1) |
| 24 | + |
| 25 | +JSON_CONFIG_1 = json.dumps(DICT_CONFIG_1) |
| 26 | +YAML_CONFIG_2 = """--- |
| 27 | +configuration: |
| 28 | + source: |
| 29 | + name: mysource |
| 30 | + client: |
| 31 | + host: http://myhost:5000 |
| 32 | +""" |
| 33 | +DICT_CONFIG_2 = { |
| 34 | + "configuration": { |
| 35 | + "source": {"name": "mysource"}, |
| 36 | + "client": {"host": "http://myhost:5000"}, |
| 37 | + }, |
| 38 | +} |
| 39 | + |
| 40 | +DUMMY_PATH = "my/dummy/path/dummy.yaml" |
| 41 | + |
| 42 | + |
| 43 | +class TestConfigurationReader(unittest.TestCase): |
| 44 | + # pylint: disable=unused-argument |
| 45 | + def setUp(self) -> None: |
| 46 | + self.reader = ConfigurationReader() |
| 47 | + |
| 48 | + @pytest.mark.unit |
| 49 | + @patch.object(Path, "exists", return_value=False) |
| 50 | + def test_read_configuration_with_invalid_file_path_then_exception(self, mocked_exists): |
| 51 | + with self.assertRaises(FileNotFoundError): |
| 52 | + self.reader.read_configuration(Path(DUMMY_PATH)) |
| 53 | + |
| 54 | + @pytest.mark.unit |
| 55 | + @patch.object(Path, "read_text", return_value=YAML_CONFIG_1) |
| 56 | + @patch.object(Path, "exists", return_value=True) |
| 57 | + def test_read_configuration_with_valid_yaml_file(self, mocked_exists, mocked_read_text): |
| 58 | + self.reader.read_configuration(Path(DUMMY_PATH)) |
| 59 | + actual = self.reader.configuration |
| 60 | + self.assertDictEqual(actual, DICT_CONFIG_1) |
| 61 | + |
| 62 | + @pytest.mark.unit |
| 63 | + @patch.object(Path, "read_text", return_value=JSON_CONFIG_1) |
| 64 | + @patch.object(Path, "exists", return_value=True) |
| 65 | + def test_read_configuration_with_valid_json_file(self, mocked_exists, mocked_read_text): |
| 66 | + self.reader.read_configuration(Path("path/to/my/config.json")) |
| 67 | + actual = self.reader.configuration |
| 68 | + self.assertDictEqual(actual, DICT_CONFIG_1) |
| 69 | + |
| 70 | + @pytest.mark.unit |
| 71 | + @patch.object(Path, "exists", return_value=True) |
| 72 | + def test_read_configuration_with_invalid_filetype_then_value_error( |
| 73 | + self, |
| 74 | + mocked_exists, |
| 75 | + ): |
| 76 | + with self.assertRaises(ValueError): |
| 77 | + self.reader.read_configuration(Path("path/to/my/invalid_config.jsonX")) |
| 78 | + |
| 79 | + @pytest.mark.unit |
| 80 | + @patch.object(Path, "read_text", side_effect=[YAML_CONFIG_1, YAML_CONFIG_2]) |
| 81 | + @patch.object(Path, "exists", return_value=True) |
| 82 | + def test_read_configuration_with_2_configs(self, mocked_exists, mocked_read_text): |
| 83 | + """Second configuration overwrites previous.""" |
| 84 | + self.reader.read_configuration(Path(DUMMY_PATH)) |
| 85 | + actual = self.reader.configuration |
| 86 | + self.assertDictEqual(actual, DICT_CONFIG_1) |
| 87 | + self.reader.read_configuration(Path("dummy_config2.yml")) |
| 88 | + actual_2 = self.reader.configuration |
| 89 | + self.assertDictEqual(actual_2, DICT_CONFIG_2) |
| 90 | + |
| 91 | + @pytest.mark.unit |
| 92 | + def test_is_supported_suffix_with_valid_suffix(self): |
| 93 | + self.assertTrue(ConfigurationReader._is_supported_json_suffix(Path("valid_suffix.json"))) |
| 94 | + self.assertTrue(ConfigurationReader._is_supported_yaml_suffix(Path("valid_suffix.yml"))) |
| 95 | + self.assertTrue(ConfigurationReader._is_supported_yaml_suffix(Path("valid_suffix.yAMl"))) |
| 96 | + |
| 97 | + @pytest.mark.unit |
| 98 | + def test_is_supported_suffix_with_invalid_suffix(self): |
| 99 | + self.assertFalse(ConfigurationReader._is_supported_yaml_suffix(Path("valid_suffix.berra"))) |
0 commit comments