1
|
|
|
# |
2
|
|
|
# Copyright (C) 2012 - 2015 Satoru SATOH <ssato at redhat.com> |
3
|
|
|
# License: MIT |
4
|
|
|
# |
5
|
|
|
# pylint: disable=missing-docstring, invalid-name, no-member |
6
|
|
|
from __future__ import absolute_import |
7
|
|
|
|
8
|
|
|
import logging |
9
|
|
|
import io |
10
|
|
|
import os |
11
|
|
|
import os.path |
12
|
|
|
import unittest |
13
|
|
|
|
14
|
|
|
import anyconfig.api as TT |
15
|
|
|
import anyconfig.backends |
16
|
|
|
import anyconfig.mdicts |
17
|
|
|
import anyconfig.template |
18
|
|
|
import anyconfig.tests.common |
19
|
|
|
|
20
|
|
|
from anyconfig.tests.common import CNF_0, SCM_0, dicts_equal |
21
|
|
|
from anyconfig.compat import OrderedDict, IS_PYTHON_3 |
22
|
|
|
from anyconfig.mdicts import convert_to |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
# suppress logging messages. |
26
|
|
|
TT.LOGGER.setLevel(logging.CRITICAL) |
27
|
|
|
|
28
|
|
|
CNF_TMPL_0 = """name: {{ name|default('a') }} |
29
|
|
|
a: {{ a }} |
30
|
|
|
b: |
31
|
|
|
b: |
32
|
|
|
{% for x in b.b -%} |
33
|
|
|
- {{ x }} |
34
|
|
|
{% endfor %} |
35
|
|
|
c: {{ b.c }} |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
CNF_TMPL_1 = """a: {{ a }} |
39
|
|
|
b: |
40
|
|
|
b: |
41
|
|
|
{% for x in b.b -%} |
42
|
|
|
- {{ x }} |
43
|
|
|
{% endfor %} |
44
|
|
|
c: {{ b.c }} |
45
|
|
|
|
46
|
|
|
name: {{ name }} |
47
|
|
|
""" |
48
|
|
|
|
49
|
|
|
CNF_TMPL_2 = """a: {{ a }} |
50
|
|
|
b: |
51
|
|
|
b: |
52
|
|
|
{% for x in b.b -%} |
53
|
|
|
- {{ x }} |
54
|
|
|
{% endfor %} |
55
|
|
|
d: {{ b.d }} |
56
|
|
|
""" |
57
|
|
|
|
58
|
|
|
CNF_XML_1 = { |
59
|
|
|
'config': {'@attrs': {'name': 'foo'}, |
60
|
|
|
'@children': [{'a': {'@text': '0'}}, |
61
|
|
|
{'b': {'@attrs': {'id': 'b0'}, |
62
|
|
|
'@text': 'bbb'}}]}} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def _is_file_object(obj): |
66
|
|
|
try: |
67
|
|
|
return isinstance(obj, file) |
68
|
|
|
except NameError: # python 3.x |
69
|
|
|
return isinstance(obj, io.IOBase) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
class Test_10_find_loader(unittest.TestCase): |
73
|
|
|
|
74
|
|
|
def _assert_isinstance(self, obj, cls, msg=False): |
75
|
|
|
self.assertTrue(isinstance(obj, cls), msg or repr(obj)) |
76
|
|
|
|
77
|
|
|
def test_10_find_loader__w_given_parser_type(self): |
78
|
|
|
cpath = "dummy.conf" |
79
|
|
|
for psr in anyconfig.backends.PARSERS: |
80
|
|
|
self._assert_isinstance(TT.find_loader(cpath, psr.type()), psr) |
81
|
|
|
|
82
|
|
|
def test_12_find_loader__w_given_parser_instance(self): |
83
|
|
|
cpath = "dummy.conf" |
84
|
|
|
for psr in anyconfig.backends.PARSERS: |
85
|
|
|
self._assert_isinstance(TT.find_loader(cpath, psr()), psr) |
86
|
|
|
|
87
|
|
|
def test_20_find_loader__by_file(self): |
88
|
|
|
for psr in anyconfig.backends.PARSERS: |
89
|
|
|
for ext in psr.extensions(): |
90
|
|
|
self._assert_isinstance(TT.find_loader("dummy." + ext), psr, |
91
|
|
|
"ext=%s, psr=%r" % (ext, psr)) |
92
|
|
|
|
93
|
|
|
def test_30_find_loader__not_found(self): |
94
|
|
|
self.assertEqual(TT.find_loader("a.cnf", "type_not_exist"), None) |
95
|
|
|
self.assertEqual(TT.find_loader("dummy.ext_not_found"), None) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
class Test_20_dumps_and_loads(unittest.TestCase): |
99
|
|
|
|
100
|
|
|
cnf = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
101
|
|
|
|
102
|
|
|
def test_30_dumps_and_loads(self): |
103
|
|
|
cnf = TT.loads(TT.dumps(self.cnf, "json"), "json") |
104
|
|
|
self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) |
105
|
|
|
|
106
|
|
|
def test_30_dumps_and_loads__w_options(self): |
107
|
|
|
cnf = TT.loads(TT.dumps(self.cnf, "json", indent=2), "json", |
108
|
|
|
ensure_ascii=False) |
109
|
|
|
self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) |
110
|
|
|
|
111
|
|
|
def test_32_dumps_and_loads__w_options__no_dumper(self): |
112
|
|
|
cnf = TT.loads(TT.dumps(self.cnf, "type_not_exist"), "json") |
113
|
|
|
self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) |
114
|
|
|
|
115
|
|
|
def test_40_loads_wo_type(self): |
116
|
|
|
a_s = "requires:bash,zsh" |
117
|
|
|
|
118
|
|
|
a1 = TT.loads(a_s) |
119
|
|
|
# a = dict(requires=["bash", "zsh"]) |
120
|
|
|
# self.assertEqual(a1["requires"], a["requires"]) |
121
|
|
|
self.assertTrue(a1 is None) |
122
|
|
|
|
123
|
|
|
def test_42_loads_w_type_not_exist(self): |
124
|
|
|
a_s = "requires:bash,zsh" |
125
|
|
|
|
126
|
|
|
a1 = TT.loads(a_s, "type_not_exist") |
127
|
|
|
# a = dict(requires=["bash", "zsh"]) |
128
|
|
|
# self.assertEqual(a1["requires"], a["requires"]) |
129
|
|
|
self.assertTrue(a1 is None) |
130
|
|
|
|
131
|
|
|
def test_44_loads_w_type__template(self): |
132
|
|
|
if not anyconfig.template.SUPPORTED: |
133
|
|
|
return |
134
|
|
|
|
135
|
|
|
a_s = "requires: [{{ requires|join(', ') }}]" |
136
|
|
|
context = dict(requires=["bash", "zsh"], ) |
137
|
|
|
|
138
|
|
|
a1 = TT.loads(a_s, ac_parser="yaml", ac_template=True, |
139
|
|
|
ac_context=context) |
140
|
|
View Code Duplication |
|
|
|
|
|
141
|
|
|
a = dict(requires=["bash", "zsh"]) |
142
|
|
|
self.assertEqual(a1["requires"], a["requires"]) |
143
|
|
|
|
144
|
|
|
def test_46_loads_w_type__broken_template(self): |
145
|
|
|
if not anyconfig.template.SUPPORTED: |
146
|
|
|
return |
147
|
|
|
|
148
|
|
|
a = dict(requires="{% }}", ) |
149
|
|
|
a_s = 'requires: "{% }}"' |
150
|
|
|
a1 = TT.loads(a_s, ac_parser="yaml", ac_template=True, |
151
|
|
|
ac_context={}) |
152
|
|
|
|
153
|
|
|
self.assertEqual(a1["requires"], a["requires"]) |
154
|
|
|
|
155
|
|
|
def test_48_loads_w_validation(self): |
156
|
|
|
cnf_s = TT.dumps(CNF_0, "json") |
157
|
|
|
scm_s = TT.dumps(SCM_0, "json") |
158
|
|
|
cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_context={}, |
159
|
|
|
ac_validate=scm_s) |
160
|
|
|
|
161
|
|
|
self.assertEqual(cnf_2["name"], CNF_0["name"]) |
162
|
|
|
self.assertEqual(cnf_2["a"], CNF_0["a"]) |
163
|
|
|
self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"]) |
164
|
|
|
self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"]) |
165
|
|
|
|
166
|
|
|
def test_49_loads_w_validation_error(self): |
167
|
|
|
cnf_s = """{"a": "aaa"}""" |
168
|
|
|
scm_s = TT.dumps(SCM_0, "json") |
169
|
|
|
cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_schema=scm_s) |
170
|
|
|
self.assertTrue(cnf_2 is None, cnf_2) |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
class Test_30_single_load(unittest.TestCase): |
174
|
|
|
|
175
|
|
|
cnf = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
176
|
|
|
|
177
|
|
|
def setUp(self): |
178
|
|
|
self.workdir = anyconfig.tests.common.setup_workdir() |
179
|
|
|
|
180
|
|
|
def tearDown(self): |
181
|
|
|
anyconfig.tests.common.cleanup_workdir(self.workdir) |
182
|
|
|
|
183
|
|
|
def test_10_dump_and_single_load(self): |
184
|
|
|
cpath = os.path.join(self.workdir, "a.json") |
185
|
|
|
|
186
|
|
|
TT.dump(self.cnf, cpath) |
187
|
|
|
self.assertTrue(os.path.exists(cpath)) |
188
|
|
|
cnf1 = TT.single_load(cpath) |
189
|
|
|
|
190
|
|
|
self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1)) |
191
|
|
|
|
192
|
|
|
def test_11_dump_and_single_load__to_from_stream(self): |
193
|
|
|
cpath = os.path.join(self.workdir, "a.json") |
194
|
|
|
|
195
|
|
|
TT.dump(self.cnf, open(cpath, 'w')) |
196
|
|
|
self.assertTrue(os.path.exists(cpath)) |
197
|
|
|
cnf1 = TT.single_load(open(cpath)) |
198
|
|
|
|
199
|
|
|
self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1)) |
200
|
|
|
|
201
|
|
|
def test_12_dump_and_single_load__no_parser(self): |
202
|
|
|
self.assertEqual(TT.single_load("dummy.ext_not_exist"), None) |
203
|
|
|
|
204
|
|
|
def test_13_dump_and_single_load__namedtuple(self): |
205
|
|
|
if not IS_PYTHON_3: # TODO: it does not work with python3. |
206
|
|
|
cpath = os.path.join(self.workdir, "a.json") |
207
|
|
|
cnf = OrderedDict(sorted(self.cnf.items())) |
208
|
|
|
cnf0 = convert_to(cnf, ac_namedtuple=True) |
209
|
|
|
|
210
|
|
|
TT.dump(cnf0, cpath) |
211
|
|
|
self.assertTrue(os.path.exists(cpath)) |
212
|
|
|
|
213
|
|
|
cnf1 = TT.single_load(cpath, ac_namedtuple=True) |
214
|
|
|
self.assertTrue(cnf0 == cnf1, "\n%r ->\n%r" % (cnf0, cnf1)) |
215
|
|
|
|
216
|
|
|
def test_14_single_load__ignore_missing(self): |
217
|
|
|
null_cntnr = TT.to_container() |
218
|
|
|
cpath = os.path.join(os.curdir, "conf_file_should_not_exist") |
219
|
|
|
assert not os.path.exists(cpath) |
220
|
|
|
|
221
|
|
|
self.assertEqual(TT.single_load(cpath, "ini", ignore_missing=True), |
222
|
|
|
null_cntnr) |
223
|
|
|
|
224
|
|
|
def test_15_single_load__fail_to_render_template(self): |
225
|
|
|
if not anyconfig.template.SUPPORTED: |
226
|
|
|
return |
227
|
|
|
|
228
|
|
|
cnf_s = "name: '{{ name'" # Should cause template renering error. |
229
|
|
|
cpath = os.path.join(self.workdir, "a.yaml") |
230
|
|
|
open(cpath, 'w').write(cnf_s) |
231
|
|
|
|
232
|
|
|
cnf = TT.single_load(cpath, ac_template=True, ac_context=dict(a=1)) |
233
|
|
|
self.assertEqual(cnf["name"], "{{ name") |
234
|
|
|
|
235
|
|
|
def test_16_single_load__template(self): |
236
|
|
|
if not anyconfig.template.SUPPORTED: |
237
|
|
|
return |
238
|
|
|
|
239
|
|
|
cpath = os.path.join(self.workdir, "a.yaml") |
240
|
|
|
open(cpath, 'w').write(CNF_TMPL_0) |
241
|
|
|
|
242
|
|
|
cnf = TT.single_load(cpath, ac_template=True, ac_context=self.cnf) |
243
|
|
|
self.assertTrue(dicts_equal(self.cnf, cnf), str(cnf)) |
244
|
|
|
|
245
|
|
|
spath = os.path.join(self.workdir, "scm.json") |
246
|
|
|
TT.dump(dict(type="integer"), spath) # Validation should fail. |
247
|
|
|
|
248
|
|
|
cnf2 = TT.single_load(cpath, ac_template=True, ac_context=self.cnf, |
249
|
|
|
ac_schema=spath) |
250
|
|
|
self.assertTrue(cnf2 is None) |
251
|
|
|
|
252
|
|
|
def test_18_single_load__templates(self): |
253
|
|
|
if not anyconfig.template.SUPPORTED: |
254
|
|
|
return |
255
|
|
|
|
256
|
|
|
a_path = os.path.join(self.workdir, "a.yml") |
257
|
|
|
b_path = os.path.join(self.workdir, "b.yml") |
258
|
|
|
a2_path = os.path.join(self.workdir, "x/y/z", "a.yml") |
259
|
|
|
|
260
|
|
|
open(a_path, 'w').write("{% include 'b.yml' %}") |
261
|
|
|
open(b_path, 'w').write(CNF_TMPL_0) |
262
|
|
|
os.makedirs(os.path.dirname(a2_path)) |
263
|
|
|
open(a2_path, 'w').write("a: 'xyz'") |
264
|
|
|
|
265
|
|
|
cnf1 = TT.single_load(a_path, ac_template=True, ac_context=self.cnf) |
266
|
|
|
self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1)) |
267
|
|
|
|
268
|
|
|
cnf2 = TT.single_load(a2_path, ac_template=True) |
269
|
|
|
self.assertEqual(cnf2["a"], "xyz") |
270
|
|
|
|
271
|
|
|
def test_19_dump_and_single_load_with_validation(self): |
272
|
|
|
cnf = CNF_0 |
273
|
|
|
scm = SCM_0 |
274
|
|
|
|
275
|
|
|
cnf_path = os.path.join(self.workdir, "cnf_19.json") |
276
|
|
|
scm_path = os.path.join(self.workdir, "scm_19.json") |
277
|
|
|
|
278
|
|
|
TT.dump(cnf, cnf_path) |
279
|
|
|
TT.dump(scm, scm_path) |
280
|
|
|
self.assertTrue(os.path.exists(cnf_path)) |
281
|
|
|
self.assertTrue(os.path.exists(scm_path)) |
282
|
|
|
|
283
|
|
|
cnf_1 = TT.single_load(cnf_path, ac_schema=scm_path) |
284
|
|
|
|
285
|
|
|
self.assertFalse(cnf_1 is None) # Validation should succeed. |
286
|
|
|
self.assertTrue(dicts_equal(cnf_1, cnf), cnf_1) |
287
|
|
|
|
288
|
|
|
cnf_2 = cnf.copy() |
289
|
|
|
cnf_2["a"] = "aaa" # It's type should be integer not string. |
290
|
|
|
cnf_2_path = os.path.join(self.workdir, "cnf_19_2.json") |
291
|
|
|
TT.dump(cnf_2, cnf_2_path) |
292
|
|
|
self.assertTrue(os.path.exists(cnf_2_path)) |
293
|
|
|
|
294
|
|
|
cnf_3 = TT.single_load(cnf_2_path, ac_schema=scm_path) |
295
|
|
|
self.assertTrue(cnf_3 is None) # Validation should fail. |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
class Test_32_single_load(unittest.TestCase): |
299
|
|
|
|
300
|
|
|
cnf = CNF_XML_1 |
301
|
|
|
|
302
|
|
|
def setUp(self): |
303
|
|
|
self.workdir = anyconfig.tests.common.setup_workdir() |
304
|
|
|
|
305
|
|
|
def tearDown(self): |
306
|
|
|
anyconfig.tests.common.cleanup_workdir(self.workdir) |
307
|
|
|
|
308
|
|
|
def _load_and_dump_with_opened_files(self, filename, rmode='r', wmode='w', |
309
|
|
|
**oopts): |
310
|
|
|
cpath = os.path.join(self.workdir, filename) |
311
|
|
|
|
312
|
|
|
with TT.open(cpath, 'w', **oopts) as out: |
313
|
|
|
TT.dump(self.cnf, out) |
314
|
|
|
self.assertTrue(_is_file_object(out)) |
315
|
|
|
self.assertEquals(out.mode, wmode) |
316
|
|
|
|
317
|
|
|
with TT.open(cpath, 'rb', **oopts) as inp: |
318
|
|
|
cnf1 = TT.single_load(inp) |
319
|
|
|
self.assertTrue(_is_file_object(inp)) |
320
|
|
|
self.assertEquals(inp.mode, rmode) |
321
|
|
|
cpair = (self.cnf, cnf1) |
322
|
|
|
self.assertTrue(dicts_equal(*cpair), "%r vs. %r" % cpair) |
323
|
|
|
|
324
|
|
|
def test_10_open_json_file(self): |
325
|
|
|
self._load_and_dump_with_opened_files("a.json") |
326
|
|
|
|
327
|
|
|
def test_20_open_xml_file(self): |
328
|
|
|
if "bson" in anyconfig.backends.list_types(): |
329
|
|
|
self._load_and_dump_with_opened_files("a.xml", 'rb', 'wb') |
330
|
|
|
|
331
|
|
|
def test_30_open_bson_file(self): |
332
|
|
|
if "bson" in anyconfig.backends.list_types(): |
333
|
|
|
self._load_and_dump_with_opened_files("a.bson", 'rb', 'wb') |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
class Test_40_multi_load(unittest.TestCase): |
337
|
|
|
|
338
|
|
|
cnf = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
339
|
|
|
|
340
|
|
|
def setUp(self): |
341
|
|
|
self.workdir = anyconfig.tests.common.setup_workdir() |
342
|
|
|
|
343
|
|
|
def tearDown(self): |
344
|
|
|
anyconfig.tests.common.cleanup_workdir(self.workdir) |
345
|
|
|
|
346
|
|
|
def test_10_dump_and_multi_load__default_merge_strategy(self): |
347
|
|
|
a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
348
|
|
|
b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) |
349
|
|
|
|
350
|
|
|
a_path = os.path.join(self.workdir, "a.json") |
351
|
|
|
b_path = os.path.join(self.workdir, "b.json") |
352
|
|
|
g_path = os.path.join(self.workdir, "*.json") |
353
|
|
|
|
354
|
|
|
TT.dump(a, a_path) |
355
|
|
|
TT.dump(b, b_path) |
356
|
|
|
|
357
|
|
|
a0 = TT.multi_load(g_path) |
358
|
|
|
a02 = TT.multi_load([g_path, b_path]) |
359
|
|
|
|
360
|
|
|
self.assertEqual(a0["name"], a["name"]) |
361
|
|
|
self.assertEqual(a0["a"], b["a"]) |
362
|
|
|
self.assertEqual(a0["b"]["b"], b["b"]["b"]) |
363
|
|
|
self.assertEqual(a0["b"]["c"], a["b"]["c"]) |
364
|
|
|
self.assertEqual(a0["b"]["d"], b["b"]["d"]) |
365
|
|
|
|
366
|
|
|
self.assertEqual(a02["name"], a["name"]) |
367
|
|
|
self.assertEqual(a02["a"], b["a"]) |
368
|
|
|
self.assertEqual(a02["b"]["b"], b["b"]["b"]) |
369
|
|
|
self.assertEqual(a02["b"]["c"], a["b"]["c"]) |
370
|
|
|
self.assertEqual(a02["b"]["d"], b["b"]["d"]) |
371
|
|
|
|
372
|
|
|
a1 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS) |
373
|
|
|
|
374
|
|
|
self.assertEqual(a1["name"], a["name"]) |
375
|
|
|
self.assertEqual(a1["a"], b["a"]) |
376
|
|
|
self.assertEqual(a1["b"]["b"], b["b"]["b"]) |
377
|
|
|
self.assertEqual(a1["b"]["c"], a["b"]["c"]) |
378
|
|
|
self.assertEqual(a1["b"]["d"], b["b"]["d"]) |
379
|
|
|
|
380
|
|
|
def test_12_dump_and_multi_load__default_merge_strategy(self): |
381
|
|
|
a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
382
|
|
|
b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) |
383
|
|
|
|
384
|
|
|
a_path = os.path.join(self.workdir, "a.json") |
385
|
|
|
b_path = os.path.join(self.workdir, "b.json") |
386
|
|
|
g_path = os.path.join(self.workdir, "*.json") |
387
|
|
|
|
388
|
|
|
TT.dump(a, a_path) |
389
|
|
|
TT.dump(b, b_path) |
390
|
|
|
|
391
|
|
|
a2 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS_AND_LISTS) |
392
|
|
|
|
393
|
|
|
self.assertEqual(a2["name"], a["name"]) |
394
|
|
|
self.assertEqual(a2["a"], b["a"]) |
395
|
|
|
self.assertEqual(a2["b"]["b"], [0, 1, 2, 3, 4, 5]) |
396
|
|
|
self.assertEqual(a2["b"]["c"], a["b"]["c"]) |
397
|
|
|
self.assertEqual(a2["b"]["d"], b["b"]["d"]) |
398
|
|
|
|
399
|
|
|
a3 = TT.multi_load(g_path) |
400
|
|
|
|
401
|
|
|
self.assertEqual(a3["name"], a["name"]) |
402
|
|
|
self.assertEqual(a3["a"], b["a"]) |
403
|
|
|
self.assertEqual(a3["b"]["b"], b["b"]["b"]) |
404
|
|
|
self.assertEqual(a3["b"]["c"], a["b"]["c"]) |
405
|
|
|
self.assertEqual(a3["b"]["d"], b["b"]["d"]) |
406
|
|
|
|
407
|
|
|
a4 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_REPLACE) |
408
|
|
|
|
409
|
|
|
self.assertEqual(a4["name"], a["name"]) |
410
|
|
|
self.assertEqual(a4["a"], b["a"]) |
411
|
|
|
self.assertEqual(a4["b"]["b"], b["b"]["b"]) |
412
|
|
|
self.assertFalse("c" in a4["b"]) |
413
|
|
|
self.assertEqual(a4["b"]["d"], b["b"]["d"]) |
414
|
|
|
|
415
|
|
|
a5 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_NO_REPLACE) |
416
|
|
|
|
417
|
|
|
self.assertEqual(a5["name"], a["name"]) |
418
|
|
|
self.assertEqual(a5["a"], a["a"]) |
419
|
|
|
self.assertEqual(a5["b"]["b"], a["b"]["b"]) |
420
|
|
|
self.assertEqual(a5["b"]["c"], a["b"]["c"]) |
421
|
|
|
self.assertFalse("d" in a5["b"]) |
422
|
|
|
|
423
|
|
|
def test_14_multi_load__wrong_merge_strategy(self): |
424
|
|
|
try: |
425
|
|
|
TT.multi_load("/dummy/*.json", ac_merge="merge_st_not_exist") |
426
|
|
|
raise RuntimeError("Wrong merge strategy was not handled!") |
427
|
|
|
except ValueError: |
428
|
|
|
self.assertTrue(1 == 1) # To suppress warn of pylint. |
429
|
|
|
|
430
|
|
|
def test_15_multi_load__empty_path_list(self): |
431
|
|
|
self.assertEqual(TT.multi_load([]), TT.to_container()) |
432
|
|
|
|
433
|
|
|
def test_16_dump_and_multi_load__mixed_file_types(self): |
434
|
|
|
a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
435
|
|
|
b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) |
436
|
|
|
|
437
|
|
|
a_path = os.path.join(self.workdir, "a.json") |
438
|
|
|
b_path = os.path.join(self.workdir, "b.yml") |
439
|
|
|
|
440
|
|
|
TT.dump(a, a_path) |
441
|
|
|
TT.dump(b, b_path) |
442
|
|
|
|
443
|
|
|
cnf = TT.multi_load([a_path, b_path]) |
444
|
|
|
|
445
|
|
|
self.assertEqual(cnf["name"], a["name"]) |
446
|
|
|
self.assertEqual(cnf["a"], b["a"]) |
447
|
|
|
self.assertEqual(cnf["b"]["b"], b["b"]["b"]) |
448
|
|
|
self.assertEqual(cnf["b"]["c"], a["b"]["c"]) |
449
|
|
|
self.assertEqual(cnf["b"]["d"], b["b"]["d"]) |
450
|
|
|
|
451
|
|
|
def test_18_dump_and_multi_load__namedtuple(self): |
452
|
|
|
a = convert_to(dict(a=1, b=dict(b=[0, 1], c="C"), name="a"), |
453
|
|
|
ac_namedtuple=True) |
454
|
|
|
b = convert_to(dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")), |
455
|
|
|
ac_namedtuple=True) |
456
|
|
|
|
457
|
|
|
a_path = os.path.join(self.workdir, "a.json") |
458
|
|
|
b_path = os.path.join(self.workdir, "b.yml") |
459
|
|
|
|
460
|
|
|
TT.dump(a, a_path) |
461
|
|
|
TT.dump(b, b_path) |
462
|
|
|
cnf = TT.multi_load([a_path, b_path], ac_namedtuple=True) |
463
|
|
|
|
464
|
|
|
self.assertEqual(cnf.name, a.name) |
465
|
|
|
self.assertEqual(cnf.a, b.a) |
466
|
|
|
self.assertEqual(cnf.b.b, b.b.b) |
467
|
|
|
self.assertEqual(cnf.b.c, a.b.c) |
468
|
|
|
self.assertEqual(cnf.b.d, b.b.d) |
469
|
|
|
|
470
|
|
|
def test_20_dump_and_multi_load__to_from_stream(self): |
471
|
|
|
a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
472
|
|
|
b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) |
473
|
|
|
|
474
|
|
|
a_path = os.path.join(self.workdir, "a.json") |
475
|
|
|
b_path = os.path.join(self.workdir, "b.json") |
476
|
|
|
|
477
|
|
|
TT.dump(a, open(a_path, 'w')) |
478
|
|
|
TT.dump(b, open(b_path, 'w')) |
479
|
|
|
self.assertTrue(os.path.exists(a_path)) |
480
|
|
|
self.assertTrue(os.path.exists(b_path)) |
481
|
|
|
|
482
|
|
|
cnf = TT.multi_load([open(a_path), open(b_path)]) |
483
|
|
|
|
484
|
|
|
self.assertEqual(cnf["name"], a["name"]) |
485
|
|
|
self.assertEqual(cnf["a"], b["a"]) |
486
|
|
|
self.assertEqual(cnf["b"]["b"], b["b"]["b"]) |
487
|
|
|
self.assertEqual(cnf["b"]["c"], a["b"]["c"]) |
488
|
|
|
self.assertEqual(cnf["b"]["d"], b["b"]["d"]) |
489
|
|
|
|
490
|
|
|
def test_30_multi_load__ignore_missing(self): |
491
|
|
|
null_cntnr = TT.to_container() |
492
|
|
|
cpath = os.path.join(os.curdir, "conf_file_should_not_exist") |
493
|
|
|
assert not os.path.exists(cpath) |
494
|
|
|
|
495
|
|
|
self.assertEqual(TT.multi_load([cpath], ac_parser="ini", |
496
|
|
|
ignore_missing=True), |
497
|
|
|
null_cntnr) |
498
|
|
|
|
499
|
|
|
def test_40_multi_load__templates(self): |
500
|
|
|
if not anyconfig.template.SUPPORTED: |
501
|
|
|
return |
502
|
|
|
|
503
|
|
|
a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
504
|
|
|
b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) |
505
|
|
|
|
506
|
|
|
ma = TT.to_container(a, ac_merge=TT.MS_DICTS) |
507
|
|
|
ma.update(b) |
508
|
|
|
|
509
|
|
|
a_path = os.path.join(self.workdir, "a.yml") |
510
|
|
|
b_path = os.path.join(self.workdir, "b.yml") |
511
|
|
|
g_path = os.path.join(self.workdir, "*.yml") |
512
|
|
|
|
513
|
|
|
open(a_path, 'w').write(CNF_TMPL_1) |
514
|
|
|
open(b_path, 'w').write(CNF_TMPL_2) |
515
|
|
|
|
516
|
|
|
a0 = TT.multi_load(g_path, ac_merge=TT.MS_DICTS, ac_template=True, |
517
|
|
|
ac_context=ma) |
518
|
|
|
a02 = TT.multi_load([g_path, b_path], ac_merge=TT.MS_DICTS, |
519
|
|
|
ac_template=True, ac_context=ma) |
520
|
|
|
|
521
|
|
|
self.assertEqual(a0["name"], a["name"]) |
522
|
|
|
self.assertEqual(a0["a"], b["a"]) |
523
|
|
|
self.assertEqual(a0["b"]["b"], b["b"]["b"]) |
524
|
|
|
self.assertEqual(a0["b"]["c"], a["b"]["c"]) |
525
|
|
|
self.assertEqual(a0["b"]["d"], b["b"]["d"]) |
526
|
|
|
|
527
|
|
|
self.assertEqual(a02["name"], a["name"]) |
528
|
|
|
self.assertEqual(a02["a"], b["a"]) |
529
|
|
|
self.assertEqual(a02["b"]["b"], b["b"]["b"]) |
530
|
|
|
self.assertEqual(a02["b"]["c"], a["b"]["c"]) |
531
|
|
|
self.assertEqual(a02["b"]["d"], b["b"]["d"]) |
532
|
|
|
|
533
|
|
|
|
534
|
|
|
class Test_50_load_and_dump(unittest.TestCase): |
535
|
|
|
|
536
|
|
|
cnf = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
537
|
|
|
|
538
|
|
|
def setUp(self): |
539
|
|
|
self.workdir = anyconfig.tests.common.setup_workdir() |
540
|
|
|
|
541
|
|
|
def tearDown(self): |
542
|
|
|
anyconfig.tests.common.cleanup_workdir(self.workdir) |
543
|
|
|
|
544
|
|
|
def test_30_dump_and_load(self): |
545
|
|
|
a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
546
|
|
|
b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) |
547
|
|
|
|
548
|
|
|
a_path = os.path.join(self.workdir, "a.json") |
549
|
|
|
b_path = os.path.join(self.workdir, "b.json") |
550
|
|
|
|
551
|
|
|
TT.dump(a, a_path) |
552
|
|
|
self.assertTrue(os.path.exists(a_path)) |
553
|
|
|
|
554
|
|
|
TT.dump(b, b_path) |
555
|
|
|
self.assertTrue(os.path.exists(b_path)) |
556
|
|
|
|
557
|
|
|
a1 = TT.load(a_path) |
558
|
|
|
|
559
|
|
|
self.assertEqual(a1["name"], a["name"]) |
560
|
|
|
self.assertEqual(a1["a"], a["a"]) |
561
|
|
|
self.assertEqual(a1["b"]["b"], a["b"]["b"]) |
562
|
|
|
self.assertEqual(a1["b"]["c"], a["b"]["c"]) |
563
|
|
|
|
564
|
|
|
a2 = TT.load(os.path.join(self.workdir, '*.json')) |
565
|
|
|
|
566
|
|
|
self.assertEqual(a2["name"], a["name"]) |
567
|
|
|
self.assertEqual(a2["a"], b["a"]) |
568
|
|
|
self.assertEqual(a2["b"]["b"], [1, 2, 3, 4, 5]) |
569
|
|
|
self.assertEqual(a2["b"]["c"], a["b"]["c"]) |
570
|
|
|
self.assertEqual(a2["b"]["d"], b["b"]["d"]) |
571
|
|
|
|
572
|
|
|
a3 = TT.load([a_path, b_path]) |
573
|
|
|
|
574
|
|
|
self.assertEqual(a3["name"], a["name"]) |
575
|
|
|
self.assertEqual(a3["a"], b["a"]) |
576
|
|
|
self.assertEqual(a3["b"]["b"], [1, 2, 3, 4, 5]) |
577
|
|
|
self.assertEqual(a3["b"]["c"], a["b"]["c"]) |
578
|
|
|
self.assertEqual(a3["b"]["d"], b["b"]["d"]) |
579
|
|
|
|
580
|
|
|
def test_31_dump_and_load__to_from_stream(self): |
581
|
|
|
cnf = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
582
|
|
|
cpath = os.path.join(self.workdir, "a.json") |
583
|
|
|
|
584
|
|
|
with open(cpath, 'w') as strm: |
585
|
|
|
TT.dump(cnf, strm) |
586
|
|
|
|
587
|
|
View Code Duplication |
self.assertTrue(os.path.exists(cpath)) |
|
|
|
|
588
|
|
|
|
589
|
|
|
with open(cpath, 'r') as strm: |
590
|
|
|
cnf1 = TT.load(strm, ac_parser="json") |
591
|
|
|
|
592
|
|
|
self.assertTrue(dicts_equal(cnf, cnf1), |
593
|
|
|
"cnf vs. cnf1: %s\n\n%s" % (str(cnf), str(cnf1))) |
594
|
|
|
|
595
|
|
|
def test_32_dump_and_load__w_options(self): |
596
|
|
|
a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") |
597
|
|
|
b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) |
598
|
|
|
|
599
|
|
|
a_path = os.path.join(self.workdir, "a.json") |
600
|
|
View Code Duplication |
b_path = os.path.join(self.workdir, "b.json") |
|
|
|
|
601
|
|
|
|
602
|
|
|
TT.dump(a, a_path, indent=2) |
603
|
|
|
self.assertTrue(os.path.exists(a_path)) |
604
|
|
|
|
605
|
|
|
TT.dump(b, b_path, indent=2) |
606
|
|
|
self.assertTrue(os.path.exists(b_path)) |
607
|
|
|
|
608
|
|
|
a1 = TT.load(a_path, parse_int=int) |
609
|
|
|
|
610
|
|
|
self.assertEqual(a1["name"], a["name"]) |
611
|
|
|
self.assertEqual(a1["a"], a["a"]) |
612
|
|
|
self.assertEqual(a1["b"]["b"], a["b"]["b"]) |
613
|
|
|
self.assertEqual(a1["b"]["c"], a["b"]["c"]) |
614
|
|
|
|
615
|
|
|
a2 = TT.load(os.path.join(self.workdir, '*.json'), parse_int=int) |
616
|
|
|
|
617
|
|
|
self.assertEqual(a2["name"], a["name"]) |
618
|
|
|
self.assertEqual(a2["a"], b["a"]) |
619
|
|
|
self.assertEqual(a2["b"]["b"], [1, 2, 3, 4, 5]) |
620
|
|
|
self.assertEqual(a2["b"]["c"], a["b"]["c"]) |
621
|
|
|
self.assertEqual(a2["b"]["d"], b["b"]["d"]) |
622
|
|
|
|
623
|
|
|
a3 = TT.load([a_path, b_path], parse_int=int) |
624
|
|
|
|
625
|
|
|
self.assertEqual(a3["name"], a["name"]) |
626
|
|
|
self.assertEqual(a3["a"], b["a"]) |
627
|
|
|
self.assertEqual(a3["b"]["b"], [1, 2, 3, 4, 5]) |
628
|
|
|
self.assertEqual(a3["b"]["c"], a["b"]["c"]) |
629
|
|
|
self.assertEqual(a3["b"]["d"], b["b"]["d"]) |
630
|
|
|
|
631
|
|
|
def test_34_load__ignore_missing(self): |
632
|
|
|
null_cntnr = TT.to_container() |
633
|
|
|
cpath = os.path.join(os.curdir, "conf_file_should_not_exist") |
634
|
|
|
assert not os.path.exists(cpath) |
635
|
|
|
|
636
|
|
|
self.assertEqual(TT.load([cpath], ac_parser="ini", |
637
|
|
|
ignore_missing=True), |
638
|
|
|
null_cntnr) |
639
|
|
|
|
640
|
|
|
def test_36_load_w_validation(self): |
641
|
|
|
cnf_path = os.path.join(self.workdir, "cnf.json") |
642
|
|
|
scm_path = os.path.join(self.workdir, "scm.json") |
643
|
|
|
TT.dump(CNF_0, cnf_path) |
644
|
|
|
TT.dump(SCM_0, scm_path) |
645
|
|
|
|
646
|
|
|
cnf_2 = TT.load(cnf_path, ac_context={}, ac_validate=scm_path) |
647
|
|
|
|
648
|
|
|
self.assertEqual(cnf_2["name"], CNF_0["name"]) |
649
|
|
|
self.assertEqual(cnf_2["a"], CNF_0["a"]) |
650
|
|
|
self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"]) |
651
|
|
|
self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"]) |
652
|
|
|
|
653
|
|
|
def test_38_load_w_validation_yaml(self): |
654
|
|
|
cnf_path = os.path.join(self.workdir, "cnf.yml") |
655
|
|
|
scm_path = os.path.join(self.workdir, "scm.yml") |
656
|
|
|
TT.dump(CNF_0, cnf_path) |
657
|
|
|
TT.dump(SCM_0, scm_path) |
658
|
|
|
|
659
|
|
|
cnf_2 = TT.load(cnf_path, ac_context={}, ac_validate=scm_path) |
660
|
|
|
|
661
|
|
|
self.assertEqual(cnf_2["name"], CNF_0["name"]) |
662
|
|
|
self.assertEqual(cnf_2["a"], CNF_0["a"]) |
663
|
|
|
self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"]) |
664
|
|
|
self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"]) |
665
|
|
|
|
666
|
|
|
def test_39_single_load__w_validation(self): |
667
|
|
|
(cnf, scm) = (CNF_0, SCM_0) |
668
|
|
|
cpath = os.path.join(self.workdir, "cnf.json") |
669
|
|
|
spath = os.path.join(self.workdir, "scm.json") |
670
|
|
|
|
671
|
|
|
TT.dump(cnf, cpath) |
672
|
|
|
TT.dump(scm, spath) |
673
|
|
|
|
674
|
|
|
cnf1 = TT.single_load(cpath, ac_schema=spath) |
675
|
|
|
self.assertTrue(dicts_equal(cnf, cnf1), str(cnf1)) |
676
|
|
|
|
677
|
|
|
# vim:sw=4:ts=4:et: |
678
|
|
|
|