Completed
Push — master ( c42960...a5a05e )
by Satoru
01:09
created

TestBase._assert_dicts_equal()   B

Complexity

Conditions 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 1
Metric Value
cc 5
c 5
b 2
f 1
dl 0
loc 13
rs 8.5454
1
#
2
# Copyright (C) 2012 - 2017 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
# pylint: disable=missing-docstring,invalid-name,too-few-public-methods
6
# pylint: disable=ungrouped-imports
7
from __future__ import absolute_import
8
9
import copy
10
import os.path
11
import unittest
12
13
import tests.common
14
15
from anyconfig.compat import OrderedDict
16
from tests.common import to_bytes as _bytes
17
18
19
CNF_0 = OrderedDict((("DEFAULT", OrderedDict((("a", "0"), ("b", "bbb"),
20
                                              ("c", "5")))),
21
                     ("sect0", OrderedDict((("a", "0"), ("b", "bbb"),
22
                                            ("c", "5"),
23
                                            ("d", "x,y,z"))))))
24
CNF_1 = copy.deepcopy(CNF_0)
25
CNF_1["sect0"]["d"] = CNF_1["sect0"]["d"].split()
26
27
CNF_2 = OrderedDict((("a", 0.1), ("b", _bytes("bbb")),
28
                     ("sect0", OrderedDict((("c", [_bytes("x"), _bytes("y"),
29
                                                   _bytes("z")]), )))))
30
31
32
class MyDict(dict):
33
    pass
34
35
36
class HasParserTrait(object):
37
38
    psr = None  # Must be a parser instance.
39
    cnf_s = None  # Do.
40
    cnf = cnf_0 = CNF_0
41
    cnf_1 = CNF_1
42
43
    def is_ready(self):
44
        return self.psr is not None
45
46
47
class TestBase(unittest.TestCase, HasParserTrait):
48
49
    def _assert_dicts_equal(self, cnf, ordered=False, cls=None, ref=None):
50
        if ref is None:
51
            ref = self.cnf
52
        self.assertTrue(tests.common.dicts_equal(cnf, ref, ordered=ordered),
53
                        "\n %r\nvs.\n %r" % (cnf, ref))
54
        # .. note::
55
        #    `cnf` may not be an instance of `cls` even if ac_dict option was
56
        #    given because parsers may not allow customize dict class to used
57
        #    for making results.
58
        if cls is None or not self.psr.dict_options():
59
            cls = OrderedDict if ordered else dict
60
        self.assertTrue(isinstance(cnf, cls),
61
                        "cnf=%r [type: %r], cls=%r" % (cnf, type(cnf), cls))
62
63
64
class Test_10_dumps_and_loads(TestBase):
65
66
    load_options = {}  # Must be set to a dict in children classes.
67
    dump_options = {}  # Do.
68
    empty_patterns = ['']  # Do.
69
70
    def test_10_loads(self):
71
        if self.is_ready():
72
            cnf = self.psr.loads(self.cnf_s)
73
            self.assertTrue(cnf)  # Check if it's not None nor {}.
74
            self._assert_dicts_equal(cnf)
75
76
    def test_12_loads_with_options(self):
77
        if self.is_ready():
78
            cnf = self.psr.loads(self.cnf_s, **self.load_options)
79
            self.assertTrue(cnf)
80
            self._assert_dicts_equal(cnf)
81
82
    def test_14_loads_with_invalid_options(self):
83
        if self.is_ready():
84
            cnf = self.psr.loads(self.cnf_s, not_exist_option_a=True)
85
            self.assertTrue(cnf)
86
            self._assert_dicts_equal(cnf)
87
88
    def test_16_loads_with_ac_ordered_option(self):
89
        if self.is_ready():
90
            cnf = self.psr.loads(self.cnf_s, ac_ordered=True)
91
            self.assertTrue(cnf)
92
            self._assert_dicts_equal(cnf, ordered=self.psr.ordered())
93
94
    def test_18_loads_with_ac_dict_option(self):
95
        if self.is_ready():
96
            cnf = self.psr.loads(self.cnf_s, ac_dict=MyDict)
97
            self.assertTrue(cnf)
98
            self._assert_dicts_equal(cnf, cls=MyDict)
99
            # for debug:
100
            # raise RuntimeError("psr=%r, cnf=%r "
101
            #                    "[%r]" % (self.psr, cnf, type(cnf)))
102
103
    def test_20_loads_with_dict_option(self):
104
        if self.is_ready():
105
            dopts = self.psr.dict_options()
106
            if dopts:
107
                opts = {dopts[0]: MyDict}
108
                cnf = self.psr.loads(self.cnf_s, **opts)
109
                self.assertTrue(cnf)
110
                self._assert_dicts_equal(cnf, cls=MyDict)
111
112
    def test_22_loads_empty_data(self):
113
        if self.is_ready():
114
            for pat in self.empty_patterns:
115
                cnf = self.psr.loads(pat)
116
                self.assertEqual(cnf, dict())
117
118
    def test_30_dumps(self):
119
        if self.is_ready():
120
            cnf_s = self.psr.dumps(self.cnf)
121
            self.assertTrue(cnf_s)  # Check if it's not empty.
122
            cnf = self.psr.loads(cnf_s)
123
            self.assertTrue(cnf)
124
            self._assert_dicts_equal(cnf)
125
126
    def test_32_dumps_with_options(self):
127
        if self.is_ready():
128
            cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options))
129
            self._assert_dicts_equal(cnf)
130
131
132
class TestBaseWithIO(TestBase):
133
134
    def setUp(self):
135
        super(TestBaseWithIO, self).setUp()
136
        if self.is_ready():
137
            self.workdir = tests.common.setup_workdir()
138
139
            exts = self.psr.extensions()
140
            ext = exts[0] if exts else "conf"
141
            self.cnf_path = os.path.join(self.workdir, "cnf_0." + ext)
142
143
            with self.psr.wopen(self.cnf_path) as out:
144
                out.write(self.cnf_s)
145
146
    def tearDown(self):
147
        if self.is_ready():
148
            tests.common.cleanup_workdir(self.workdir)
149
150
151
class Test_20_dump_and_load(TestBaseWithIO):
152
153
    def test_10_load(self):
154
        if self.is_ready():
155
            cnf = self.psr.load(self.cnf_path)
156
            self.assertTrue(cnf)
157
            self._assert_dicts_equal(cnf)
158
159
    def test_12_load_from_stream(self):
160
        if self.is_ready():
161
            with self.psr.ropen(self.cnf_path) as strm:
162
                cnf = self.psr.load(strm)
163
164
            self.assertTrue(cnf)
165
            self._assert_dicts_equal(cnf)
166
167
    def test_14_load_with_ac_ordered_option(self):
168
        if self.is_ready():
169
            cnf = self.psr.load(self.cnf_path, ac_ordered=True)
170
            self.assertTrue(cnf)
171
            self._assert_dicts_equal(cnf, ordered=self.psr.ordered())
172
173
    def test_16_load_with_ac_dict_option(self):
174
        if self.is_ready():
175
            cnf = self.psr.load(self.cnf_path, ac_dict=MyDict)
176
            self.assertTrue(cnf)
177
            self._assert_dicts_equal(cnf, cls=MyDict)
178
179
    def test_30_dump(self):
180
        if self.is_ready():
181
            self.psr.dump(self.cnf, self.cnf_path)
182
            cnf = self.psr.load(self.cnf_path)
183
            self.assertTrue(cnf)
184
            self._assert_dicts_equal(cnf)
185
186
    def test_32_dump_to_stream(self):
187
        if self.is_ready():
188
            with self.psr.wopen(self.cnf_path) as strm:
189
                self.psr.dump(self.cnf, strm)
190
191
            cnf = self.psr.load(self.cnf_path)
192
            self.assertTrue(cnf)
193
            self._assert_dicts_equal(cnf)
194
195
# vim:sw=4:ts=4:et:
196