-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_configparser.py
97 lines (76 loc) · 3.95 KB
/
test_configparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from pyconfigparser import ConfigParser, ConfigError, ConfigFileNotFoundError
from config.schemas import SIMPLE_SCHEMA_CONFIG, UNSUPPORTED_OBJECT_KEYS_SCHEMA
import unittest
import os
DT_FMT_TEST = '%Y-%m-%dT%H:%M:%SZ'
VAR_LOG_LEVEL_INFO = 'INFO'
class ConfigTestCase(unittest.TestCase):
def setUp(self) -> None:
os.environ['DATE_FORMAT_TEST'] = DT_FMT_TEST
os.environ['LOG_LEVEL_TEST'] = VAR_LOG_LEVEL_INFO
def test_schema_checking(self):
configparser = ConfigParser()
self.assertRaises(ConfigError, configparser.get_config, 1)
def test_config_without_file(self):
configparser = ConfigParser()
self.assertRaises(ConfigFileNotFoundError, configparser.get_config, SIMPLE_SCHEMA_CONFIG,
'config',
'some_non_exists_file.json')
def test_undefined_env_var(self):
try:
configparser = ConfigParser()
configparser.get_config(file_name='config.yaml')
except Exception as e:
self.assertIn('Environment', str(e))
configparser = ConfigParser()
configparser.ignore_unset_env_vars = True
configparser.get_config(file_name='config.yaml')
def test_to_access_attr_from_config(self):
configparser = ConfigParser()
config = configparser.get_config(SIMPLE_SCHEMA_CONFIG)
self.assertEqual(VAR_LOG_LEVEL_INFO, config.core.logging.level)
self.assertEqual(DT_FMT_TEST, config.core.logging.datefmt)
self.assertEqual('format', config.core.logging.format)
self.assertEqual(24, config.core.obj_list[0].age)
self.assertEqual('Mike', config.core.obj_list[0]['name']) # <- using subscriptable access
def test_access_fake_attr(self):
configparser = ConfigParser()
config = configparser.get_config(SIMPLE_SCHEMA_CONFIG)
self.assertRaises(AttributeError, lambda: config.fake_attr)
def test_unsupported_object_key(self):
configparser = ConfigParser()
self.assertRaises(ConfigError, configparser.get_config, UNSUPPORTED_OBJECT_KEYS_SCHEMA,
file_name='unsupported_object_key.json')
def test_config_with_wrong_json_model(self):
configparser = ConfigParser()
self.assertRaises(ConfigError, configparser.get_config, SIMPLE_SCHEMA_CONFIG, file_name='wrong_model.json')
def test_config_file_with_unsupported_extension(self):
configparser = ConfigParser()
self.assertRaises(ConfigError, configparser.get_config, SIMPLE_SCHEMA_CONFIG, file_name='config.bad_extension')
def test_bad_decoder_error(self):
configparser = ConfigParser()
self.assertRaises(ConfigError, configparser.get_config, SIMPLE_SCHEMA_CONFIG, file_name='bad_content.json')
self.assertRaises(ConfigError, configparser.get_config, SIMPLE_SCHEMA_CONFIG, file_name='bad_content.yaml')
def test_caching_instance(self):
configparser = ConfigParser()
config1 = configparser.get_config()
config2 = configparser.get_config()
self.assertIs(config1, config2)
configparser.hold_an_instance = False
config2 = configparser.get_config()
self.assertIsNot(config1, config2)
def test_configparser_config_switches(self):
configparser = ConfigParser()
def assign_a_bad_type_hold_an_instance():
configparser.hold_an_instance = []
def assign_a_bad_type_ignore_unsetted_env_vars():
configparser.ignore_unset_env_vars = []
self.assertRaises(ValueError, assign_a_bad_type_hold_an_instance)
self.assertRaises(ValueError, assign_a_bad_type_ignore_unsetted_env_vars)
configparser.hold_an_instance = False
configparser.ignore_unset_env_vars = True
self.assertIs(configparser.hold_an_instance, False)
self.assertIs(configparser.ignore_unset_env_vars, True)
self.assertIsInstance(configparser.ignore_unset_env_vars, bool)
if __name__ == '__main__':
unittest.main()