|
1 | | -import os.path |
| 1 | +import sys |
2 | 2 | import unittest |
| 3 | +from argparse import ArgumentParser |
3 | 4 | from pathlib import Path |
4 | 5 | from unittest import TestCase |
| 6 | +from unittest.mock import patch |
5 | 7 |
|
6 | 8 | from context_logger import setup_logging |
7 | 9 |
|
8 | 10 | from common_utility import delete_file, ConfigLoader, copy_file |
9 | 11 | from tests import TEST_RESOURCE_ROOT, TEST_FILE_SYSTEM_ROOT |
10 | 12 |
|
| 13 | +DEFAULT_CONFIG_FILE = f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf.default' |
| 14 | + |
11 | 15 |
|
12 | 16 | class ConfigLoaderTest(TestCase): |
13 | 17 |
|
14 | 18 | @classmethod |
15 | 19 | def setUpClass(cls): |
16 | 20 | setup_logging('python-common-utility', 'DEBUG', warn_on_overwrite=False) |
| 21 | + copy_file(f'{TEST_RESOURCE_ROOT}/config/example.conf.default', DEFAULT_CONFIG_FILE) |
17 | 22 |
|
18 | 23 | def setUp(self): |
19 | 24 | print() |
20 | 25 | delete_file(f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf') |
| 26 | + delete_file(f'{TEST_FILE_SYSTEM_ROOT}/etc/example.types.conf') |
| 27 | + delete_file(f'{TEST_FILE_SYSTEM_ROOT}/etc/example.types.invalid.conf') |
| 28 | + |
| 29 | + def _create_argument_parser(self) -> ArgumentParser: |
| 30 | + argument_parser = ArgumentParser() |
| 31 | + argument_parser.add_argument('--config', default=None) |
| 32 | + argument_parser.add_argument('--config-key1', default=None) |
| 33 | + argument_parser.add_argument('--config-key2', default=None) |
| 34 | + argument_parser.add_argument('--example-key1', default=None) |
| 35 | + argument_parser.add_argument('--example-key2', default=None) |
| 36 | + return argument_parser |
| 37 | + |
| 38 | + def test_load_config_when_default_config_file_could_not_be_loaded(self): |
| 39 | + # Given |
| 40 | + config_loader = ConfigLoader(Path('invalid/path/example.conf.default')) |
| 41 | + argument_parser = self._create_argument_parser() |
| 42 | + |
| 43 | + # When |
| 44 | + with patch.object(sys, 'argv', ['test', '--config-key1', 'new_value1']): |
| 45 | + result = config_loader.load(argument_parser) |
| 46 | + |
| 47 | + # Then |
| 48 | + self.assertEqual('new_value1', result.config_key1) |
| 49 | + |
| 50 | + def test_load_config_when_no_custom_config_file_specified(self): |
| 51 | + # Given |
| 52 | + config_loader = ConfigLoader(Path(DEFAULT_CONFIG_FILE)) |
| 53 | + argument_parser = self._create_argument_parser() |
| 54 | + |
| 55 | + # When |
| 56 | + with patch.object(sys, 'argv', ['test', '--config-key1', 'new_value1']): |
| 57 | + result = config_loader.load(argument_parser) |
| 58 | + |
| 59 | + # Then |
| 60 | + self.assertEqual('new_value1', result.config_key1) |
| 61 | + self.assertEqual('value2', result.config_key2) |
| 62 | + self.assertEqual('example1', result.example_key1) |
| 63 | + self.assertEqual('example2', result.example_key2) |
| 64 | + |
| 65 | + def test_load_config_when_custom_config_file_specified(self): |
| 66 | + # Given |
| 67 | + config_loader = ConfigLoader(Path(DEFAULT_CONFIG_FILE)) |
| 68 | + argument_parser = self._create_argument_parser() |
| 69 | + config_file = f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf' |
| 70 | + |
| 71 | + copy_file(f'{TEST_RESOURCE_ROOT}/config/example.conf', config_file) |
| 72 | + |
| 73 | + # When |
| 74 | + with patch.object(sys, 'argv', ['test', '--config', config_file, '--example-key1', 'new_example1']): |
| 75 | + result = config_loader.load(argument_parser) |
21 | 76 |
|
22 | | - def test_load_config_when_custom_configuration_not_exists(self): |
| 77 | + # Then |
| 78 | + self.assertEqual('value1', result.config_key1) |
| 79 | + self.assertEqual('value3', result.config_key2) |
| 80 | + self.assertEqual('new_example1', result.example_key1) |
| 81 | + self.assertEqual('example4', result.example_key2) |
| 82 | + |
| 83 | + def test_load_config_when_custom_config_file_could_not_be_loaded(self): |
23 | 84 | # Given |
24 | | - config_loader = ConfigLoader(Path(TEST_RESOURCE_ROOT) / 'config' / 'example.default.conf') |
25 | | - arguments = { |
26 | | - 'config_file': f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf', |
27 | | - 'config_key1': 'new_value1', |
28 | | - } |
| 85 | + config_loader = ConfigLoader(Path(DEFAULT_CONFIG_FILE)) |
| 86 | + argument_parser = self._create_argument_parser() |
| 87 | + # When |
| 88 | + with patch.object(sys, 'argv', |
| 89 | + ['test', '--config', 'invalid/path/example.conf', '--example-key1', 'new_example1']): |
| 90 | + result = config_loader.load(argument_parser) |
| 91 | + |
| 92 | + # Then |
| 93 | + self.assertEqual('value2', result.config_key2) |
| 94 | + self.assertEqual('new_example1', result.example_key1) |
| 95 | + self.assertEqual('example2', result.example_key2) |
| 96 | + |
| 97 | + def test_load_config_when_parser_default_values_defined_but_not_passed(self): |
| 98 | + # Given |
| 99 | + config_loader = ConfigLoader(Path(TEST_RESOURCE_ROOT) / 'config' / 'example.conf.default') |
| 100 | + argument_parser = ArgumentParser() |
| 101 | + argument_parser.add_argument('--config', default=None) |
| 102 | + argument_parser.add_argument('--config-key1', default='cli_default_value1') |
| 103 | + argument_parser.add_argument('--example-key1', default='cli_default_example1') |
| 104 | + config_file = f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf' |
| 105 | + |
| 106 | + copy_file(f'{TEST_RESOURCE_ROOT}/config/example.conf', config_file) |
29 | 107 |
|
30 | 108 | # When |
31 | | - result = config_loader.load(arguments) |
| 109 | + with patch.object(sys, 'argv', ['test', '--config', config_file]): |
| 110 | + result = config_loader.load(argument_parser) |
32 | 111 |
|
33 | 112 | # Then |
34 | | - self.assertTrue(os.path.exists(f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf')) |
35 | | - self.assertEqual('new_value1', result['config_key1']) |
36 | | - self.assertEqual('value2', result['config_key2']) |
37 | | - self.assertEqual('example1', result['example_key1']) |
38 | | - self.assertEqual('example2', result['example_key2']) |
| 113 | + self.assertEqual('value1', result.config_key1) |
| 114 | + self.assertEqual('example3', result.example_key1) |
39 | 115 |
|
40 | | - def test_load_config_when_custom_configuration_exists(self): |
| 116 | + def test_load_config_when_short_option_cli_override_is_passed(self): |
41 | 117 | # Given |
42 | | - config_loader = ConfigLoader(Path(TEST_RESOURCE_ROOT) / 'config' / 'example.default.conf') |
43 | | - arguments = { |
44 | | - 'config_file': f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf', |
45 | | - 'example_key1': 'new_example1', |
46 | | - } |
| 118 | + config_loader = ConfigLoader(Path(TEST_RESOURCE_ROOT) / 'config' / 'example.conf.default') |
| 119 | + argument_parser = ArgumentParser() |
| 120 | + argument_parser.add_argument('--config', default=None) |
| 121 | + argument_parser.add_argument('--example-key2', '-e2', default=None) |
| 122 | + config_file = f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf' |
47 | 123 |
|
48 | | - copy_file(f'{TEST_RESOURCE_ROOT}/config/example.conf', f'{TEST_FILE_SYSTEM_ROOT}/etc/example.conf') |
| 124 | + copy_file(f'{TEST_RESOURCE_ROOT}/config/example.conf', config_file) |
49 | 125 |
|
50 | 126 | # When |
51 | | - result = config_loader.load(arguments) |
| 127 | + with patch.object(sys, 'argv', ['test', '--config', config_file, '-e2', 'new_example2']): |
| 128 | + result = config_loader.load(argument_parser) |
52 | 129 |
|
53 | 130 | # Then |
54 | | - self.assertEqual('value1', result['config_key1']) |
55 | | - self.assertEqual('value3', result['config_key2']) |
56 | | - self.assertEqual('new_example1', result['example_key1']) |
57 | | - self.assertEqual('example4', result['example_key2']) |
| 131 | + self.assertEqual('new_example2', result.example_key2) |
58 | 132 |
|
59 | | - def test_load_config_when_fail_to_create_custom_configuration(self): |
| 133 | + def test_load_config_when_long_option_cli_override_passed_using_equal_sign(self): |
60 | 134 | # Given |
61 | | - config_loader = ConfigLoader(Path(TEST_RESOURCE_ROOT) / 'config' / 'example.default.conf') |
62 | | - arguments = { |
63 | | - 'config_file': '/invalid/path/to/example.conf', |
64 | | - 'config_key1': 'new_value1', |
65 | | - } |
| 135 | + config_loader = ConfigLoader(Path(TEST_RESOURCE_ROOT) / 'config' / 'example.conf.default') |
| 136 | + argument_parser = self._create_argument_parser() |
| 137 | + |
| 138 | + # When |
| 139 | + with patch.object(sys, 'argv', ['test', '--config-key1=new_value1']): |
| 140 | + result = config_loader.load(argument_parser) |
| 141 | + |
| 142 | + # Then |
| 143 | + self.assertEqual('value1', result.config_key1) |
| 144 | + |
| 145 | + def test_get_cli_overrides_when_no_cli_arguments(self): |
| 146 | + # Given |
| 147 | + config_loader = ConfigLoader(Path(DEFAULT_CONFIG_FILE)) |
| 148 | + argument_parser = self._create_argument_parser() |
| 149 | + arguments = argument_parser.parse_args([]) |
| 150 | + |
| 151 | + # When |
| 152 | + with patch.object(sys, 'argv', ['test']): |
| 153 | + result = config_loader._get_cli_overrides(argument_parser, arguments) |
| 154 | + |
| 155 | + # Then |
| 156 | + self.assertEqual({}, result) |
| 157 | + |
| 158 | + def test_get_cli_overrides_when_argument_has_no_option_string(self): |
| 159 | + # Given |
| 160 | + config_loader = ConfigLoader(Path(DEFAULT_CONFIG_FILE)) |
| 161 | + argument_parser = ArgumentParser() |
| 162 | + argument_parser.add_argument('--config', default=None) |
| 163 | + argument_parser.add_argument('input_file') |
| 164 | + arguments = argument_parser.parse_args(['input.txt']) |
| 165 | + |
| 166 | + # When |
| 167 | + with patch.object(sys, 'argv', ['test', 'input.txt']): |
| 168 | + result = config_loader._get_cli_overrides(argument_parser, arguments) |
| 169 | + |
| 170 | + # Then |
| 171 | + self.assertEqual({}, result) |
| 172 | + |
| 173 | + def test_load_config_when_type_values_present_then_sanitize(self): |
| 174 | + # Given |
| 175 | + config_file = f'{TEST_FILE_SYSTEM_ROOT}/etc/example.types.conf' |
| 176 | + Path(config_file).write_text('[types]\nfeature_enabled = true\nretry_count = 7\ntimeout = 1.5\n', |
| 177 | + encoding='utf-8') |
| 178 | + |
| 179 | + config_loader = ConfigLoader(Path(DEFAULT_CONFIG_FILE)) |
| 180 | + argument_parser = ArgumentParser() |
| 181 | + argument_parser.add_argument('--config', default=None) |
| 182 | + argument_parser.add_argument('--feature-enabled', default=False) |
| 183 | + argument_parser.add_argument('--retry-count', default=0) |
| 184 | + argument_parser.add_argument('--timeout', default=0.0) |
| 185 | + |
| 186 | + # When |
| 187 | + with patch.object(sys, 'argv', ['test', '--config', config_file]): |
| 188 | + result = config_loader.load(argument_parser) |
| 189 | + |
| 190 | + # Then |
| 191 | + self.assertTrue(result.feature_enabled) |
| 192 | + self.assertEqual(7, result.retry_count) |
| 193 | + self.assertEqual(1.5, result.timeout) |
| 194 | + |
| 195 | + def test_load_config_when_invalid_numeric_values_present_then_keep_original(self): |
| 196 | + # Given |
| 197 | + config_file = f'{TEST_FILE_SYSTEM_ROOT}/etc/example.types.invalid.conf' |
| 198 | + Path(config_file).write_text('[types]\nretry_count = invalid\ntimeout = invalid\n', encoding='utf-8') |
| 199 | + |
| 200 | + config_loader = ConfigLoader(Path(DEFAULT_CONFIG_FILE)) |
| 201 | + argument_parser = ArgumentParser() |
| 202 | + argument_parser.add_argument('--config', default=None) |
| 203 | + argument_parser.add_argument('--retry-count', default=0) |
| 204 | + argument_parser.add_argument('--timeout', default=0.0) |
66 | 205 |
|
67 | 206 | # When |
68 | | - result = config_loader.load(arguments) |
| 207 | + with patch.object(sys, 'argv', ['test', '--config', config_file]): |
| 208 | + result = config_loader.load(argument_parser) |
69 | 209 |
|
70 | 210 | # Then |
71 | | - self.assertEqual('new_value1', result['config_key1']) |
72 | | - self.assertEqual('value2', result['config_key2']) |
73 | | - self.assertEqual('example1', result['example_key1']) |
74 | | - self.assertEqual('example2', result['example_key2']) |
| 211 | + self.assertEqual('invalid', result.retry_count) |
| 212 | + self.assertEqual('invalid', result.timeout) |
75 | 213 |
|
76 | 214 |
|
77 | 215 | if __name__ == '__main__': |
|
0 commit comments