1
|
|
|
#!/usr/bin/env python |
2
|
|
|
import unittest |
3
|
|
|
import os |
4
|
|
|
import sys |
5
|
|
|
|
6
|
|
|
from impulsare_config import Reader, utils |
7
|
|
|
|
8
|
|
|
base_path = os.path.abspath(os.path.dirname(__file__)) |
9
|
|
|
sys.path.insert(0, base_path + '/../') |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
# https://docs.python.org/3/library/unittest.html#assert-methods |
13
|
|
|
class TestReader(unittest.TestCase): |
14
|
|
|
def test_default(self): |
15
|
|
|
if os.path.isfile(utils.get_venv_basedir() + '/config/app.yml'): |
16
|
|
|
return |
17
|
|
|
|
18
|
|
|
with self.assertRaisesRegex(IOError, 'Missing config file: "(.+)config/app\.yml" does not exist'): |
19
|
|
|
Reader().parse() |
20
|
|
|
|
21
|
|
|
def test_overriden(self): |
22
|
|
|
with self.assertRaisesRegex(IOError, 'Missing config file: "/does/not/exist" does not exist'): |
23
|
|
|
Reader().parse('/does/not/exist') |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def test_exists(self): |
27
|
|
|
cf = Reader() |
28
|
|
|
self.assertIs(type(cf), Reader) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def test_valid_config(self): |
32
|
|
|
config_file = base_path + '/static/config_valid.yml' |
33
|
|
|
specs_file = base_path + '/static/specs.yml' |
34
|
|
|
|
35
|
|
|
cf = Reader() |
36
|
|
|
config_data = cf.parse(config_file, specs_file) |
37
|
|
|
self.assertIsInstance(config_data, dict) |
38
|
|
|
|
39
|
|
|
self.assertIn('debug', config_data) |
40
|
|
|
self.assertTrue(config_data['debug']) |
41
|
|
|
|
42
|
|
|
self.assertIn('logger', config_data) |
43
|
|
|
self.assertEqual('syslog', config_data['logger']) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_valid_config_bad_default(self): |
47
|
|
|
config_file = base_path + '/static/config_empty.yml' |
48
|
|
|
specs_file = base_path + '/static/specs.yml' |
49
|
|
|
|
50
|
|
|
with self.assertRaisesRegex(IOError, 'Your default .+ does not exist'): |
51
|
|
|
Reader().parse(config_file=config_file, specs=specs_file, default_file='/does/not/exists') |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def test_valid_config_bad_specs(self): |
55
|
|
|
config_file = base_path + '/static/config_empty.yml' |
56
|
|
|
|
57
|
|
|
with self.assertRaisesRegex(IOError, 'Your specs .+ does not exist'): |
58
|
|
|
Reader().parse(config_file=config_file, specs='/does/not/exists') |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def test_empty_config(self): |
62
|
|
|
config_file = base_path + '/static/config_empty.yml' |
63
|
|
|
specs_file = base_path + '/static/specs.yml' |
64
|
|
|
default_config_file = base_path + '/static/default.yml' |
65
|
|
|
|
66
|
|
|
cf = Reader() |
67
|
|
|
config_data = cf.parse(config_file, specs_file, default_config_file) |
68
|
|
|
self.assertIsInstance(config_data, dict) |
69
|
|
|
|
70
|
|
|
self.assertIn('debug', config_data) |
71
|
|
|
self.assertFalse(config_data['debug']) |
72
|
|
|
|
73
|
|
|
self.assertIn('logger', config_data) |
74
|
|
|
self.assertEqual('monolog', config_data['logger']) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def test_invalid_config(self): |
78
|
|
|
config_file = base_path + '/static/config_invalid.yml' |
79
|
|
|
specs_file = base_path + '/static/specs.yml' |
80
|
|
|
|
81
|
|
|
with self.assertRaisesRegex(ValueError, "'abc' is not of type 'boolean'"): |
82
|
|
|
Reader().parse(config_file, specs_file) |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def test_invalid_config_no_specs(self): |
86
|
|
|
config_file = base_path + '/static/config_invalid.yml' |
87
|
|
|
|
88
|
|
|
cf = Reader() |
89
|
|
|
config_data = cf.parse(config_file) |
90
|
|
|
self.assertIsInstance(config_data, dict) |
91
|
|
|
|
92
|
|
|
self.assertIn('debug', config_data) |
93
|
|
|
self.assertEqual('abc', config_data['debug']) |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
if __name__ == "__main__": |
97
|
|
|
unittest.main() |
98
|
|
|
|