-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_declare_config.py
227 lines (160 loc) · 5.97 KB
/
test_declare_config.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""Test cases for declare_config.py."""
import os
import pathlib
import tempfile
import pytest
import yaml
from declare_config import Configuration, Setting, configuration_provider, \
enable_expanduser, enable_nested_settings
# ============================================================================
# Setting tests
# ============================================================================
def test_configured_value_simple():
class TestClass(Configuration):
test_value = Setting('x')
foo = TestClass({"x": "bar"})
assert foo.test_value == "bar"
def test_configured_value_type():
class TestClass(Configuration):
test_value = Setting('x', setting_type=int)
foo = TestClass({"x": "1"})
assert foo.test_value == 1
def test_no_config_given():
class TestClass(Configuration):
x = Setting("path.to.config")
foo = TestClass()
with pytest.raises(ValueError) as err:
print(foo.x)
assert "missing required config path.to.config" in str(err)
def test_configured_value_default():
class TestClass(Configuration):
test_value = Setting('x', 5000, int)
foo = TestClass()
assert foo.test_value == 5000
foo = TestClass({"x": "10000"})
assert foo.test_value == 10000
def test_require_default():
with pytest.raises(ValueError) as err:
Setting(config_path=None, default=None)
assert "must provide config_path or default" in str(err)
def test_require_callable_type():
with pytest.raises(ValueError) as err:
Setting("x", setting_type="not_callable")
assert "must provide callable for setting_type" in str(err)
def test_configured_value_class_ref():
my_field = Setting('x', 5000, int)
class TestClass(Configuration):
test_value = my_field
assert TestClass.test_value == my_field
def test_no_config_path():
class TestClass(Configuration):
test_value = Setting(None, "/usr/var/log")
foo = TestClass({})
assert foo.test_value == "/usr/var/log"
def test_nested_path():
class TestClass(Configuration):
test_value = Setting("a.b.c.d", setting_type=int)
foo = TestClass({"a": {"b": {"c": {"d": "5"}}}})
assert foo.test_value == 5
# ============================================================================
# Preprocessor/Postprocessor tests
# ============================================================================
def test_expanduser():
@enable_expanduser
class TestClass(Configuration):
x = Setting("x", setting_type=pathlib.Path)
y = Setting("y", setting_type=pathlib.Path)
z = Setting("z", setting_type=int)
foo = TestClass({"x": "~/file.txt", "y": "/usr/lib/file.txt", "z": "3"})
assert foo.x == pathlib.Path("~/file.txt").expanduser()
assert foo.y == pathlib.Path("/usr/lib/file.txt")
assert foo.z == 3
def test_nested_settings():
@enable_nested_settings
class TestClass(Configuration):
a = Setting("a", "foo")
b = Setting("b", "${a}bar")
c = Setting("c", "${b}baz")
d = Setting("d", "/usr/tmp/${c}")
foo = TestClass({"d": "/usr/tmp/log/${c}"})
assert foo.a == "foo"
assert foo.b == "foobar"
assert foo.c == "foobarbaz"
assert foo.d == "/usr/tmp/log/foobarbaz"
# ============================================================================
# Provider tests
# ============================================================================
def test_load_requires_provider():
class TestClass(Configuration):
pass
with pytest.raises(TypeError) as err:
TestClass.load()
assert "TestClass has no provider defined" in str(err)
def test_provider():
@configuration_provider({"x": "a"})
class TestClass(Configuration):
x = Setting("x", "foo")
foo = TestClass.load()
assert foo.x == "a"
def test_file_provider():
temp = tempfile.NamedTemporaryFile('w')
@configuration_provider(temp.name)
class TestClass(Configuration):
x = Setting("x", 4, setting_type=int)
with temp as fp:
fp.write(yaml.dump({"x": "5"}))
fp.flush()
foo = TestClass.load()
assert foo.x == 5
def test_must_exist():
temp = tempfile.NamedTemporaryFile('w')
@configuration_provider(temp.name, must_exist=True)
class TestClass(Configuration):
x = Setting("x", 4, setting_type=int)
with temp as fp:
fp.write(yaml.dump({"x": "5"}))
fp.flush()
foo = TestClass.load(temp.name)
assert foo.x == 5
with pytest.raises(ValueError) as err:
TestClass.load()
assert "could not find configuration file at " + temp.name in str(err)
def test_environment_provider():
@configuration_provider("$_dc_test")
@configuration_provider({"x": "bar"})
class TestClass(Configuration):
x = Setting("x", "foo")
# Fallback to default if no environment variable present
assert TestClass.load().x == "bar"
try:
# Load from environment variable if present
temp = tempfile.NamedTemporaryFile('w')
os.environ["_dc_test"] = temp.name
with temp as fp:
fp.write(yaml.dump({"x": "5"}))
fp.flush()
foo = TestClass.load()
assert foo.x == "5"
# environment variable uses must_exist = true by default
with pytest.raises(ValueError) as err:
TestClass.load()
assert "could not find configuration file at " + temp.name in str(err)
finally:
del os.environ["_dc_test"]
def test_nested_providers():
@configuration_provider("$_dc_test")
@configuration_provider({"x": "a"})
class TestClass(Configuration):
x = Setting("x", "foo")
foo = TestClass.load()
assert foo.x == "a"
try:
temp = tempfile.NamedTemporaryFile('w')
os.environ["_dc_test"] = temp.name
with temp as fp:
fp.write(yaml.dump({"x": "5"}))
fp.flush()
foo = TestClass.load()
assert foo.x == "5"
finally:
del os.environ["_dc_test"]