Completed
Push — master ( d14655...6ef2f5 )
by Satoru
55s
created

Test_44_multi_load   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A test_10_multi_load_w_validation_for_partial_single_config_files() 0 9 1
1
#
2
# Copyright (C) 2012 - 2017 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 copy
9
import logging
10
import io
11
import os
12
import os.path
13
import unittest
14
15
import anyconfig.api as TT
16
import anyconfig.backends
17
import anyconfig.compat
18
import anyconfig.dicts
19
import anyconfig.template
20
import tests.common
21
22
from tests.common import CNF_0, SCM_0, dicts_equal, selfdir
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
e: 0
57
"""
58
59
CNF_XML_1 = {'config': {'@attrs': {'name': 'foo'},
60
                        'a': '0',
61
                        'b': {'@attrs': {'id': 'b0'}, '@text': 'bbb'},
62
                        'c': None,
63
                        'sect0': {'d': 'x, y, z'},
64
                        'list1': [{'item': '0'}, {'item': '1'},
65
                                  {'item': '2'}],
66
                        'list2': {'@attrs': {'id': 'list2'},
67
                                  '@children': [{'item': 'i'},
68
                                                {'item': 'j'}]}}}
69
70
NULL_CNTNR = TT.anyconfig.dicts.convert_to({})
71
72
73
class MyODict(anyconfig.compat.OrderedDict):
74
    pass
75
76
77
def _is_file_object(obj):
78
    try:
79
        return isinstance(obj, file)
80
    except NameError:  # python 3.x
81
        return isinstance(obj, io.IOBase)
82
83
84
class Test_10_find_loader(unittest.TestCase):
85
86
    def _assert_isinstance(self, obj, cls, msg=False):
87
        self.assertTrue(isinstance(obj, cls), msg or repr(obj))
88
89
    def test_10_find_loader__w_given_parser_type(self):
90
        cpath = "dummy.conf"
91
        for psr in anyconfig.backends.PARSERS:
92
            self._assert_isinstance(TT.find_loader(cpath, psr.type()), psr)
93
94
    def test_12_find_loader__w_given_parser_instance(self):
95
        cpath = "dummy.conf"
96
        for psr in anyconfig.backends.PARSERS:
97
            self._assert_isinstance(TT.find_loader(cpath, psr()), psr)
98
99
    def test_20_find_loader__by_file(self):
100
        for psr in anyconfig.backends.PARSERS:
101
            for ext in psr.extensions():
102
                self._assert_isinstance(TT.find_loader("dummy." + ext), psr,
103
                                        "ext=%s, psr=%r" % (ext, psr))
104
105
    def test_30_find_loader__unknown_parser_type(self):
106
        self.assertRaises(TT.UnknownParserTypeError,
107
                          TT.find_loader, "a.cnf", "type_not_exist")
108
109
    def test_40_find_loader__unknown_file_type(self):
110
        self.assertRaises(TT.UnknownFileTypeError,
111
                          TT.find_loader, "dummy.ext_not_found")
112
113
114
class TestBase(unittest.TestCase):
115
116
    cnf = dic = dict(a=1, b=dict(b=[0, 1], c="C"), name="a")
117
    upd = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D"), e=0)
118
119
    def assert_dicts_equal(self, dic, ref, ordered=False):
120
        self.assertTrue(dicts_equal(dic, ref, ordered=ordered),
121
                        "%r%s vs.%s%r" % (dic, os.linesep, os.linesep, ref))
122
123
124
class Test_20_dumps_and_loads(TestBase):
125
126
    def test_30_dumps_and_loads(self):
127
        res = TT.loads(TT.dumps(self.cnf, "json"), "json")
128
        self.assert_dicts_equal(res, self.cnf)
129
130
    def test_30_dumps_and_loads__w_options(self):
131
        res = TT.loads(TT.dumps(self.cnf, "json", indent=2), "json",
132
                       ensure_ascii=False)
133
        self.assert_dicts_equal(res, self.cnf)
134
135
    def test_40_loads_wo_type(self):
136
        cnf_s = "requires:bash,zsh"
137
        self.assertTrue(TT.loads(cnf_s) is None)
138
139
    def test_42_loads_w_type_not_exist(self):
140
        a_s = "requires:bash,zsh"
141
        self.assertRaises(TT.UnknownParserTypeError,
142
                          TT.loads, a_s, "type_not_exist")
143
144
    def test_44_loads_w_type__template(self):
145
        if not anyconfig.template.SUPPORTED:
146
            return
147
148
        a_s = "requires: [{{ requires|join(', ') }}]"
149
        reqs = dict(requires=["bash", "zsh"])
150
151
        a1 = TT.loads(a_s, ac_parser="yaml", ac_template=True,
152
                      ac_context=reqs)
153
154
        self.assertEqual(a1["requires"], reqs["requires"])
155
156
    def test_46_loads_w_type__broken_template(self):
157
        if not anyconfig.template.SUPPORTED:
158
            return
159
160
        a = dict(requires="{% }}", )
161
        a_s = 'requires: "{% }}"'
162
        a1 = TT.loads(a_s, ac_parser="yaml", ac_template=True,
163
                      ac_context={})
164
165
        self.assertEqual(a1["requires"], a["requires"])
166
167 View Code Duplication
    def test_48_loads_w_validation(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
168
        cnf_s = TT.dumps(CNF_0, "json")
169
        scm_s = TT.dumps(SCM_0, "json")
170
        cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_context={},
171
                         ac_schema=scm_s)
172
173
        self.assertEqual(cnf_2["name"], CNF_0["name"])
174
        self.assertEqual(cnf_2["a"], CNF_0["a"])
175
        self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"])
176
        self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
177
178
    def test_49_loads_w_validation_error(self):
179
        cnf_s = """{"a": "aaa"}"""
180
        scm_s = TT.dumps(SCM_0, "json")
181
        cnf_2 = TT.loads(cnf_s, ac_parser="json", ac_schema=scm_s)
182
        self.assertTrue(cnf_2 is None, cnf_2)
183
184
185
class TestBaseWithIO(TestBase):
186
187
    def setUp(self):
188
        self.workdir = tests.common.setup_workdir()
189
        self.a_path = os.path.join(self.workdir, "a.json")
190
        self.exp = copy.deepcopy(self.dic)
191
192
    def tearDown(self):
193
        tests.common.cleanup_workdir(self.workdir)
194
195
196
class Test_30_single_load(TestBaseWithIO):
197
198
    def test_10_dump_and_single_load(self):
199
        TT.dump(self.cnf, self.a_path)
200
        self.assertTrue(os.path.exists(self.a_path))
201
202
        res = TT.single_load(self.a_path)
203
        self.assert_dicts_equal(res, self.cnf)
204
205
    def test_11_dump_and_single_load__to_from_stream(self):
206
        TT.dump(self.cnf, TT.open(self.a_path, mode='w'))
207
        self.assertTrue(os.path.exists(self.a_path))
208
209
        res = TT.single_load(TT.open(self.a_path))
210
        self.assert_dicts_equal(res, self.cnf)
211
212
    def test_12_dump_and_single_load__no_parser(self):
213
        self.assertRaises(TT.UnknownFileTypeError,
214
                          TT.single_load, "dummy.ext_not_exist")
215
216
    def test_14_single_load__ignore_missing(self):
217
        cpath = os.path.join(os.curdir, "conf_file_should_not_exist")
218
        assert not os.path.exists(cpath)
219
220
        self.assertEqual(TT.single_load(cpath, "ini", ignore_missing=True),
221
                         NULL_CNTNR)
222
223
    def test_15_single_load__fail_to_render_template(self):
224
        if not anyconfig.template.SUPPORTED:
225
            return
226
227
        cnf_s = "name: '{{ name'"  # Should cause template renering error.
228
        cpath = os.path.join(self.workdir, "a.yaml")
229
        TT.open(cpath, mode='w').write(cnf_s)
230
231
        cnf = TT.single_load(cpath, ac_template=True, ac_context=dict(a=1))
232
        self.assertEqual(cnf["name"], "{{ name")
233
234
    def test_16_single_load__template(self):
235
        if not anyconfig.template.SUPPORTED:
236
            return
237
238
        cpath = os.path.join(self.workdir, "a.yaml")
239
        TT.open(cpath, mode='w').write(CNF_TMPL_0)
240
241
        cnf = TT.single_load(cpath, ac_template=True, ac_context=self.cnf)
242
        self.assert_dicts_equal(cnf, self.cnf)
243
244
        spath = os.path.join(self.workdir, "scm.json")
245
        TT.dump(dict(type="integer"), spath)  # Validation should fail.
246
247
        cnf2 = TT.single_load(cpath, ac_template=True, ac_context=self.cnf,
248
                              ac_schema=spath)
249
        self.assertTrue(cnf2 is None)
250
251
    def test_18_single_load__templates(self):
252
        if not anyconfig.template.SUPPORTED:
253
            return
254
255
        a_path = os.path.join(self.workdir, "a.yml")
256
        b_path = os.path.join(self.workdir, "b.yml")
257
        a2_path = os.path.join(self.workdir, "x/y/z", "a.yml")
258
259
        open(a_path, 'w').write("{% include 'b.yml' %}")
260
        open(b_path, 'w').write(CNF_TMPL_0)
261
        os.makedirs(os.path.dirname(a2_path))
262
        open(a2_path, 'w').write("a: 'xyz'")
263
264
        cnf1 = TT.single_load(a_path, ac_template=True, ac_context=self.cnf)
265
        self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1))
266
267
        cnf2 = TT.single_load(a2_path, ac_template=True)
268
        self.assertEqual(cnf2["a"], "xyz")
269
270
    def test_19_dump_and_single_load_with_validation(self):
271
        cnf = CNF_0
272
        scm = SCM_0
273
274
        cnf_path = os.path.join(self.workdir, "cnf_19.json")
275
        scm_path = os.path.join(self.workdir, "scm_19.json")
276
277
        TT.dump(cnf, cnf_path)
278
        TT.dump(scm, scm_path)
279
        self.assertTrue(os.path.exists(cnf_path))
280
        self.assertTrue(os.path.exists(scm_path))
281
282
        cnf_1 = TT.single_load(cnf_path, ac_schema=scm_path)
283
284
        self.assertFalse(cnf_1 is None)  # Validation should succeed.
285
        self.assertTrue(dicts_equal(cnf_1, cnf), cnf_1)
286
287
        cnf_2 = cnf.copy()
288
        cnf_2["a"] = "aaa"  # It's type should be integer not string.
289
        cnf_2_path = os.path.join(self.workdir, "cnf_19_2.json")
290
        TT.dump(cnf_2, cnf_2_path)
291
        self.assertTrue(os.path.exists(cnf_2_path))
292
293
        cnf_3 = TT.single_load(cnf_2_path, ac_schema=scm_path)
294
        self.assertTrue(cnf_3 is None)  # Validation should fail.
295
296
    def test_20_dump_and_single_load__w_ordered_option(self):
297
        TT.dump(self.cnf, self.a_path)
298
        self.assertTrue(os.path.exists(self.a_path))
299
300
        # It works w/ JSON backend but some backend cannot keep the order of
301
        # items and the tests might fail.
302
        res = TT.single_load(self.a_path, ac_ordered=True)
303
        self.assert_dicts_equal(res, self.cnf, ordered=True)
304
        self.assertTrue(isinstance(res, anyconfig.compat.OrderedDict))
305
306
    def test_22_dump_and_single_load__w_ac_dict_option(self):
307
        TT.dump(self.cnf, self.a_path)
308
        self.assertTrue(os.path.exists(self.a_path))
309
310
        res = TT.single_load(self.a_path, ac_dict=MyODict)
311
        self.assert_dicts_equal(res, self.cnf, ordered=True)
312
        self.assertTrue(isinstance(res, MyODict))
313
314
315
class Test_32_single_load(unittest.TestCase):
316
317
    cnf = CNF_XML_1
318
319
    def setUp(self):
320
        self.workdir = tests.common.setup_workdir()
321
322
    def tearDown(self):
323
        tests.common.cleanup_workdir(self.workdir)
324
325
    def _load_and_dump_with_opened_files(self, filename, rmode='r', wmode='w',
326
                                         **oopts):
327
        cpath = os.path.join(self.workdir, filename)
328
329
        with TT.open(cpath, 'w', **oopts) as out:
330
            TT.dump(self.cnf, out)
331
            self.assertTrue(_is_file_object(out))
332
            self.assertEqual(out.mode, wmode)
333
334
        with TT.open(cpath, 'rb', **oopts) as inp:
335
            cnf1 = TT.single_load(inp)
336
            self.assertTrue(_is_file_object(inp))
337
            self.assertEqual(inp.mode, rmode)
338
            cpair = (self.cnf, cnf1)
339
            self.assertTrue(dicts_equal(*cpair), "%r vs. %r" % cpair)
340
341
    def test_10_open_json_file(self):
342
        self._load_and_dump_with_opened_files("a.json")
343
344
    def test_20_open_xml_file(self):
345
        if "xml" in anyconfig.backends.list_types():
346
            self._load_and_dump_with_opened_files("a.xml", 'rb', 'wb')
347
348
    def test_30_open_bson_file(self):
349
        if "bson" in anyconfig.backends.list_types():
350
            self._load_and_dump_with_opened_files("a.bson", 'rb', 'wb')
351
352
    def test_40_open_yaml_file(self):
353
        if "yaml" in anyconfig.backends.list_types():
354
            self._load_and_dump_with_opened_files("a.yaml")
355
            self._load_and_dump_with_opened_files("a.yml")
356
357
358
class Test_34_single_load(TestBaseWithIO):
359
360 View Code Duplication
    def test_10_single_load_w_validation(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
361
        cnf_path = os.path.join(self.workdir, "cnf.json")
362
        scm_path = os.path.join(self.workdir, "scm.json")
363
        TT.dump(CNF_0, cnf_path)
364
        TT.dump(SCM_0, scm_path)
365
366
        cnf_2 = TT.single_load(cnf_path, ac_context={}, ac_schema=scm_path)
367
368
        self.assertEqual(cnf_2["name"], CNF_0["name"])
369
        self.assertEqual(cnf_2["a"], CNF_0["a"])
370
        self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"])
371
        self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
372
373 View Code Duplication
    def test_20_single_load_w_query(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
374
        cpath = os.path.join(self.workdir, "cnf.json")
375
        TT.dump(CNF_0, cpath)
376
377
        try:
378
            if TT.query.jmespath:
379
                self.assertEqual(TT.single_load(cpath, ac_query="a"), 1)
380
                self.assertEqual(TT.single_load(cpath, ac_query="b.b"), [1, 2])
381
                self.assertEqual(TT.single_load(cpath, ac_query="b.b[1]"), 2)
382
                self.assertEqual(TT.single_load(cpath, ac_query="b.b[1:]"),
383
                                 [2])
384
                self.assertEqual(TT.single_load(cpath, ac_query="b.b[::-1]"),
385
                                 [2, 1])
386
                self.assertEqual(TT.single_load(cpath, ac_query="length(b.b)"),
387
                                 2)
388
        except (NameError, AttributeError):
389
            pass  # jmespath is not available.
390
391
392
class TestBaseWithIOMultiFiles(TestBaseWithIO):
393
394
    def setUp(self):
395
        super(TestBaseWithIOMultiFiles, self).setUp()
396
        self.b_path = os.path.join(self.workdir, "b.json")
397 View Code Duplication
        self.g_path = os.path.join(self.workdir, "*.json")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
398
399
        exp = copy.deepcopy(self.upd)  # Assume MS_DICTS strategy was used.
400
        exp["b"]["c"] = self.dic["b"]["c"]
401
        exp["name"] = self.dic["name"]
402
        self.exp = exp
403
404
405
class Test_40_multi_load_with_strategies(TestBaseWithIOMultiFiles):
406
407
    def _check_multi_load_with_strategy(self, exp, merge=TT.MS_DICTS):
408
        TT.dump(self.dic, self.a_path)
409
        TT.dump(self.upd, self.b_path)
410
411
        self.assertTrue(os.path.exists(self.a_path))
412
        self.assertTrue(os.path.exists(self.b_path))
413
414
        res0 = TT.multi_load(self.g_path, ac_merge=merge)
415
        res1 = TT.multi_load([self.g_path, self.b_path], ac_merge=merge)
416
417
        self.assertTrue(res0)
418
        self.assertTrue(res1)
419
420
        self.assert_dicts_equal(res0, exp)
421
        self.assert_dicts_equal(res1, exp)
422
423
    def test_10_default_merge_strategy(self):
424
        exp = copy.deepcopy(self.upd)
425
        exp["b"]["c"] = self.dic["b"]["c"]
426
        exp["name"] = self.dic["name"]
427
428
        self._check_multi_load_with_strategy(exp, merge=None)
429
        self._check_multi_load_with_strategy(exp)
430
431
    def test_20_merge_dicts_and_lists(self):
432
        exp = copy.deepcopy(self.upd)
433
        exp["b"]["b"] = [0] + self.upd["b"]["b"]
434
        exp["b"]["c"] = self.dic["b"]["c"]
435
        exp["name"] = self.dic["name"]
436
        self._check_multi_load_with_strategy(exp, merge=TT.MS_DICTS_AND_LISTS)
437
438
    def test_30_merge_with_replace(self):
439
        exp = copy.deepcopy(self.upd)
440
        exp["name"] = self.dic["name"]
441
        self._check_multi_load_with_strategy(exp, merge=TT.MS_REPLACE)
442
443
    def test_40_merge_wo_replace(self):
444
        exp = copy.deepcopy(self.dic)
445
        exp["e"] = self.upd["e"]
446
        self._check_multi_load_with_strategy(exp, merge=TT.MS_NO_REPLACE)
447
448
    def test_60_wrong_merge_strategy(self):
449
        cpath = os.path.join(self.workdir, "a.json")
450
        TT.dump(dict(a=1, b=2), cpath)
451
        try:
452
            TT.multi_load([cpath, cpath], ac_merge="merge_st_not_exist")
453
            raise RuntimeError("Wrong merge strategy was not handled!")
454
        except ValueError:
455
            self.assertTrue(1 == 1)  # To suppress warn of pylint.
456
457
458
class Test_42_multi_load(TestBaseWithIOMultiFiles):
459
460
    def test_10_multi_load__empty_path_list(self):
461
        self.assertEqual(TT.multi_load([]), NULL_CNTNR)
462
463
    def test_20_dump_and_multi_load__mixed_file_types(self):
464
        c_path = os.path.join(self.workdir, "c.yml")
465
466
        TT.dump(self.dic, self.a_path)  # JSON
467
        try:
468
            TT.dump(self.upd, c_path)  # YAML
469
        except (TT.UnknownParserTypeError, TT.UnknownFileTypeError):
470
            return  # YAML backend is not available in this env.
471
472
        self.assertTrue(os.path.exists(self.a_path))
473
        self.assertTrue(os.path.exists(c_path))
474
475
        res = TT.multi_load([self.a_path, c_path])
476
        self.assert_dicts_equal(res, self.exp)
477
478
    def test_30_dump_and_multi_load__to_from_stream(self):
479
        TT.dump(self.dic, self.a_path)
480
        TT.dump(self.upd, self.b_path)
481
482
        res = TT.multi_load([TT.open(self.a_path), TT.open(self.b_path)])
483
        self.assert_dicts_equal(res, self.exp)
484
485
    def test_40_multi_load__ignore_missing(self):
486
        cpath = os.path.join(os.curdir, "conf_file_should_not_exist")
487
        assert not os.path.exists(cpath)
488
489
        self.assertEqual(TT.multi_load([cpath], ac_parser="ini",
490
                                       ignore_missing=True),
491
                         NULL_CNTNR)
492
493
    def test_50_multi_load__templates(self):
494
        if not anyconfig.template.SUPPORTED:
495
            return
496
497
        ctx = self.dic.copy()
498
        TT.merge(ctx, self.upd, ac_merge=TT.MS_DICTS)
499
500
        a_path = self.a_path.replace(".json", ".yml")
501
        b_path = self.b_path.replace(".json", ".yml")
502
        g_path = self.g_path.replace(".json", ".yml")
503
504
        TT.open(a_path, mode='w').write(CNF_TMPL_1)
505
        TT.open(b_path, mode='w').write(CNF_TMPL_2)
506
507
        opts = dict(ac_merge=TT.MS_DICTS, ac_template=True, ac_context=ctx)
508
        try:
509
            res0 = TT.multi_load(g_path, **opts)
510
            res1 = TT.multi_load([g_path, b_path], **opts)
511
        except (TT.UnknownParserTypeError, TT.UnknownFileTypeError):
512
            return
513
514
        self.assert_dicts_equal(res0, self.exp)
515
        self.assert_dicts_equal(res1, self.exp)
516
517
    def test_60_multi_load__w_ac_dict_option(self):
518
        TT.dump(self.dic, self.a_path)
519
        TT.dump(self.upd, self.b_path)
520
521
        res = TT.multi_load(self.g_path, ac_dict=MyODict)
522
        self.assert_dicts_equal(res, self.exp)
523
        self.assertTrue(isinstance(res, MyODict))
524
525
526
class Test_44_multi_load(TestBase):
527
528
    def test_10_multi_load_w_validation_for_partial_single_config_files(self):
529
        cpaths = [os.path.join(selfdir(), "00-00-cnf.json"),
530
                  os.path.join(selfdir(), "00-01-cnf.json"),
531
                  os.path.join(selfdir(), "00-02-cnf.json")]
532
        spath = os.path.join(selfdir(), "00-scm.json")
533
534
        cnf = TT.multi_load(cpaths, ac_schema=spath)
535
        ref = TT.multi_load(cpaths)
536
        self.assert_dicts_equal(cnf, ref, ordered=False)
537
538
539
class Test_50_load_and_dump(TestBaseWithIOMultiFiles):
540
541
    def test_30_dump_and_load(self):
542
        TT.dump(self.dic, self.a_path)
543
        TT.dump(self.upd, self.b_path)
544
545
        self.assertTrue(os.path.exists(self.a_path))
546
        self.assertTrue(os.path.exists(self.b_path))
547
548
        res = TT.load(self.a_path)
549
        self.assert_dicts_equal(res, self.dic)
550
551
        res = TT.load(self.g_path)
552
        self.assert_dicts_equal(res, self.exp)
553
554
        res = TT.load([self.a_path, self.b_path])
555
        self.assert_dicts_equal(res, self.exp)
556
557
    def test_31_dump_and_load__to_from_stream(self):
558
        with TT.open(self.a_path, mode='w') as strm:
559
            TT.dump(self.dic, strm)
560
561
        self.assertTrue(os.path.exists(self.a_path))
562
563
        with TT.open(self.a_path) as strm:
564
            res = TT.load(strm, ac_parser="json")
565
            self.assert_dicts_equal(res, self.dic)
566
567
    def test_32_dump_and_load__w_options(self):
568
        TT.dump(self.dic, self.a_path, indent=2)
569
        self.assertTrue(os.path.exists(self.a_path))
570
571
        TT.dump(self.upd, self.b_path, indent=2)
572
        self.assertTrue(os.path.exists(self.b_path))
573
574
        res = TT.load(self.a_path, parse_int=int)
575
        dic = copy.deepcopy(self.dic)
576
        self.assert_dicts_equal(res, dic)
577
578
        res = TT.load(self.g_path, parse_int=int)
579
        exp = copy.deepcopy(self.exp)
580
        self.assert_dicts_equal(res, exp)
581
582
        res = TT.load([self.a_path, self.b_path], parse_int=int)
583
        exp = copy.deepcopy(self.exp)
584
        self.assert_dicts_equal(res, exp)
585
586
    def test_34_load__ignore_missing(self):
587
        cpath = os.path.join(os.curdir, "conf_file_should_not_exist")
588
        assert not os.path.exists(cpath)
589
590
        self.assertEqual(TT.load([cpath], ac_parser="ini",
591
                                 ignore_missing=True),
592
                         NULL_CNTNR)
593
594 View Code Duplication
    def test_36_load_w_validation(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
595
        cnf_path = os.path.join(self.workdir, "cnf.json")
596
        scm_path = os.path.join(self.workdir, "scm.json")
597
        TT.dump(CNF_0, cnf_path)
598
        TT.dump(SCM_0, scm_path)
599
600
        cnf_2 = TT.load(cnf_path, ac_context={}, ac_schema=scm_path)
601
602
        self.assertEqual(cnf_2["name"], CNF_0["name"])
603
        self.assertEqual(cnf_2["a"], CNF_0["a"])
604
        self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"])
605
        self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
606
607 View Code Duplication
    def test_38_load_w_validation_yaml(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
608
        cnf_path = os.path.join(self.workdir, "cnf.yml")
609
        scm_path = os.path.join(self.workdir, "scm.yml")
610
        TT.dump(CNF_0, cnf_path)
611
        TT.dump(SCM_0, scm_path)
612
613
        cnf_2 = TT.load(cnf_path, ac_context={}, ac_schema=scm_path)
614
615
        self.assertEqual(cnf_2["name"], CNF_0["name"])
616
        self.assertEqual(cnf_2["a"], CNF_0["a"])
617
        self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"])
618
        self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
619
620
    def test_39_single_load__w_validation(self):
621
        (cnf, scm) = (CNF_0, SCM_0)
622
        cpath = os.path.join(self.workdir, "cnf.json")
623
        spath = os.path.join(self.workdir, "scm.json")
624
625
        TT.dump(cnf, cpath)
626
        TT.dump(scm, spath)
627
628
        cnf1 = TT.single_load(cpath, ac_schema=spath)
629
        self.assert_dicts_equal(cnf, cnf1)
630
631 View Code Duplication
    def test_40_load_w_query(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
632
        cnf_path = os.path.join(self.workdir, "cnf.json")
633
        TT.dump(CNF_0, cnf_path)
634
635
        try:
636
            if TT.query.jmespath:
637
                self.assertEqual(TT.load(cnf_path, ac_query="a"), 1)
638
                self.assertEqual(TT.load(cnf_path, ac_query="b.b"), [1, 2])
639
                self.assertEqual(TT.load(cnf_path, ac_query="b.b[1]"), 2)
640
                self.assertEqual(TT.load(cnf_path, ac_query="b.b[1:]"), [2])
641
                self.assertEqual(TT.load(cnf_path, ac_query="b.b[::-1]"),
642
                                 [2, 1])
643
                self.assertEqual(TT.load(cnf_path, ac_query="length(b.b)"), 2)
644
        except (NameError, AttributeError):
645
            pass  # jmespath is not available.
646
647
# vim:sw=4:ts=4:et:
648