File: test_merge.py

package info (click to toggle)
python-hiyapyco 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: python: 2,012; makefile: 237
file content (237 lines) | stat: -rwxr-xr-x 6,722 bytes parent folder | download
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
228
229
230
231
232
233
234
235
236
237
#! /usr/bin/env python

import sys
import os
import logging
import platform
import hiyapyco

sys.path.insert(
        0,
        os.path.join(
            os.path.dirname(
                os.path.realpath(os.path.abspath(sys.argv[0]))
                ),
            'lib'
            )
        )
import testsetup

logger = testsetup.setup(sys.argv[1:])

basepath = os.path.dirname(os.path.realpath(__file__))

print('start test %s for hiyapyco %s using python %s (loglevel:%s)' % (
            __file__,
            hiyapyco.__version__,
            platform.python_version(),
            logging.getLevelName(logger.getEffectiveLevel())
        )
    )

logger.info('test substitute vals ...')
conf = hiyapyco.load(
        os.path.join(basepath, 'base.yaml'),
        os.path.join(basepath, 'baseext.yaml'),
        method=hiyapyco.METHOD_SUBSTITUTE,
        failonmissingfiles=True
        )

t = conf['singel']
logger.info('test single val ... %s' % t)
assert t == 'ext'

t = conf['int']
logger.info('test int val ... %s' % t)
assert t == 10

t = conf['array']
logger.info('test list val ... %s' % t)
assert t == ['baseext1', 'baseext2']

t = conf['hash']
logger.info('test simple dict ... %s' % t)
assert t == {'k1': 'b1', 'k2': 'b2', 'ek1': 'be1', 'ek2': 'be2'}

t = conf['deeplist']
logger.info('test deeplist ... %s' % t)
assert t == [{'d1': {'d1ext': 'vext'}}, {'dext1': {'d1k1ext': 'v1ext'}}, {'d31': {'a': 'AAA'}, 'd32': {'a': 'A2A2', 'c': 'CCC'}}]

t = conf.get('deepmap')
logger.info('test deepmap ... %s' % t)
assert t == {'l1k1' : {'l2k1': 'overwr', 'l2k2': 'abc', 'l2k3': 'new val'}, 'l1k2': {'l2k1': 'bli', 'l2k2': 'bla', 'l2k3': 'blub'}, 'l1k1ext': {'l2k1ext': 'ext'}}

t = conf['ext']
logger.info('test extonly ... %s' % t)
assert t == 'extendet only'

t = conf['missing_key']
logger.info('test missing_key ... %s' % t)
assert t == 'one'

t = conf['missing_key_parent']
logger.info('test missing_key_parent ... %s' % t)
assert t == {'a': 'b'}

t = conf['common_key']
logger.info('test common_key ... %s' % t)
assert t == {'common_subkey_deep': 'oneext', 'missing_key_base': 'val2', 'missing_key_ext': 'val2missingInBase'}

logger.info('test simple vals ...')
conf = hiyapyco.load(
        os.path.join(basepath, 'base.yaml'),
        os.path.join(basepath, 'baseext.yaml'),
        method=hiyapyco.METHOD_SIMPLE,
        failonmissingfiles=True
        )

t = conf['singel']
logger.info('test single val ... %s' % t)
assert t == 'ext'

t = conf['int']
logger.info('test int val ... %s' % t)
assert t == 10

t = conf['array']
logger.info('test list val ... %s' % t)
assert t == ['baseext1', 'baseext2']

t = conf['hash']
logger.info('test simple dict ... %s' % t)
assert t == {'ek1': 'be1', 'ek2': 'be2'}

t = conf['deeplist']
logger.info('test deeplist ... %s' % t)
assert t == [{'d1': {'d1ext': 'vext'}}, {'dext1': {'d1k1ext': 'v1ext'}}, {'d32': {'a': 'A2A2', 'c': 'CCC'}, 'd31': {'a': 'AAA'}}]

t = conf['deepmap']
logger.info('test deepmap ... %s' % t)
assert t == {'l1k1': {'l2k1': 'overwr', 'l2k3': 'new val'}, 'l1k1ext': {'l2k1ext': 'ext'}}

t = conf['ext']
logger.info('test extonly ... %s' % t)
assert t == 'extendet only'

try:
    conf['nosuchelement']
    raise Error
except KeyError as e:
    assert '%s' % e == '\'nosuchelement\''

logger.info('test merged vals ...')
conf = hiyapyco.load(
        os.path.join(basepath, 'base.yaml'),
        os.path.join(basepath, 'baseext.yaml'),
        method=hiyapyco.METHOD_MERGE,
        failonmissingfiles=True
        )

t = conf['singel']
logger.info('test single val ... %s' % t)
assert t == 'ext'

t = conf['int']
logger.info('test int val ... %s' % t)
assert t == 10

t = conf['array']
logger.info('test list val ... %s' % t)
assert t == ['base1', 'base2', 'baseext1', 'baseext2']

t = conf['hash']
logger.info('test simple dict ... %s' % t)
assert t == {'k1': 'b1', 'k2': 'b2', 'ek1': 'be1', 'ek2': 'be2'}

t = conf['deeplist']
logger.info('test deeplist ... %s' % t)
assert t == [{'d1': {'d1k1': 'v1', 'd1k2': 'v2', 'd1ext': 'vext'}}, {'d2': {'d2k2': 'x2', 'd2k1': 'x1'}}, {'d32': {'a': 'A2A2', 'c': 'CCC', 'b': 'B2'}, 'd31': {'a': 'AAA', 'c': 'C', 'b': 'B'}}, {'dext1': {'d1k1ext': 'v1ext'}}]

t = conf.get('deepmap')
logger.info('test deepmap ... %s' % t)
assert t == {'l1k1' : {'l2k1': 'overwr', 'l2k2': 'abc', 'l2k3': 'new val'}, 'l1k2': {'l2k1': 'bli', 'l2k2': 'bla', 'l2k3': 'blub'}, 'l1k1ext': {'l2k1ext': 'ext'}}

t = conf['ext']
logger.info('test extonly ... %s' % t)
assert t == 'extendet only'

t = conf['missing_key']
logger.info('test missing_key ... %s' % t)
assert t == 'one'

t = conf['missing_key_parent']
logger.info('test missing_key_parent ... %s' % t)
assert t == {'a': 'b'}

t = conf['common_key']
logger.info('test common_key ... %s' % t)
assert t == {'common_subkey_deep': 'oneext', 'missing_key_base': 'val2', 'missing_key_ext': 'val2missingInBase'}

try:
    conf['nosuchelement']
    raise Error
except KeyError as e:
    assert '%s' % e == '\'nosuchelement\''


logger.info("test none behavior DEFAULT ...")
for method in [hiyapyco.METHOD_MERGE, hiyapyco.METHOD_SUBSTITUTE]:
    try:
        conf = hiyapyco.load(
            os.path.join(basepath, "base.yaml"),
            os.path.join(basepath, "base_none_behavior.yaml"),
            method=method,
            failonmissingfiles=True,
        )
    except hiyapyco.HiYaPyCoImplementationException:
        pass
    else:
        raise AssertionError("Default None behavior is expected to fail on this merge")

logger.info("test none behavior OVERRIDE ...")
for method in hiyapyco.METHODS.values():
    conf = hiyapyco.load(
        os.path.join(basepath, "base.yaml"),
        os.path.join(basepath, "base_none_behavior.yaml"),
        none_behavior=hiyapyco.NONE_BEHAVIOR_OVERRIDE,
        method=hiyapyco.METHOD_MERGE,
        failonmissingfiles=True,
    )

    t = conf["singel"]
    logger.info("test single val ... %s" % t)
    assert t is None

    t = conf["int"]
    logger.info("test int val ... %s" % t)
    assert t is None

    t = conf["array"]
    logger.info("test list val ... %s" % t)
    assert t is None

    t = conf["hash"]
    logger.info("test simple dict ... %s" % t)
    assert t is None

    t = conf["deeplist"]
    logger.info("test deeplist ... %s" % t)
    assert t == [
        {"d1": None},
        {"d2": {"d2k2": "x2", "d2k1": None}},
        {
            "d32": {"a": "A2", "b": "B2"},
            "d31": {"a": "A", "c": "C", "b": "B"},
        },
    ]

    t = conf["deepmap"]
    logger.info("test deepmap ... %s" % t)
    assert t == {
        "l1k1": {"l2k1": None, "l2k2": "abc"},
        "l1k2": None,
    }

print("passed test %s" % __file__)

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 smartindent nu