Completed
Push — master ( 4f34ad...3db5fd )
by Satoru
01:10
created

test_20_dumps__format()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 3
rs 10
1
#
2
# Copyright (C) 2012 - 2015 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
# pylint: disable=missing-docstring, protected-access
6
from __future__ import absolute_import
7
8
import os.path
9
import unittest
10
11
import anyconfig.backend.ini as TT
12
import anyconfig.tests.common
13
14
from anyconfig.compat import OrderedDict as ODict
15
from anyconfig.mdicts import UpdateWithReplaceDict
16
from anyconfig.tests.common import dicts_equal
17
18
19
CNF_0_S = """[DEFAULT]
20
a: 0
21
b: bbb
22
c: 5
23
24
[sect0]
25
d: x,y,z
26
"""
27
28
CNF_0 = ODict((("DEFAULT", ODict((("a", "0"), ("b", "bbb"), ("c", "5")))),
29
               ("sect0", ODict((("a", "0"), ("b", "bbb"), ("c", "5"),
30
                                ("d", "x,y,z"))))))
31
CNF_1 = ODict((("DEFAULT", ODict((("a", 0), ("b", "bbb"), ("c", 5)))),
32
               ("sect0", ODict((("a", 0), ("b", "bbb"), ("c", 5),
33
                                ("d", "x y z".split()))))))
34
35
36
class Test10(unittest.TestCase):
37
38
    cnf = CNF_0
39
    cnf_s = CNF_0_S
40
    load_options = dict(allow_no_value=False, defaults=None)
41
    dump_options = dict()
42
    is_order_kept = True
43
44
    def setUp(self):
45
        self.psr = TT.Parser()
46
47
    def test_10_loads(self):
48
        cnf = self.psr.loads(self.cnf_s)
49
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
50
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
51
52
    def test_12_loads__w_options(self):
53
        cnf = self.psr.loads(self.cnf_s, **self.load_options)
54
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
55
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
56
57
    def test_20_dumps(self):
58
        cnf_s = self.psr.dumps(self.cnf)
59
        self.assertTrue(cnf_s)
60
        cnf = self.psr.loads(cnf_s)
61
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
62
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
63
64
    def test_22_dumps__w_options(self):
65
        cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options))
66
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
67
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
68
69
    def test_30_loads_with_order_kept(self):
70
        cnf = self.psr.loads(self.cnf_s, ac_ordered=True)
71
        if self.is_order_kept:
72
            self.assertTrue(dicts_equal(cnf, self.cnf, ordered=True),
73
                            "\n %r\nvs.\n %r" % (cnf, self.cnf))
74
        else:
75
            self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
76
77
78
class Test11(Test10):
79
80
    cnf = CNF_1
81
82
    def test_10_loads(self):
83
        cnf = self.psr.loads(self.cnf_s, ac_parse_value=True)
84
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
85
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
86
87
    def test_12_loads__w_options(self):
88
        cnf = self.psr.loads(self.cnf_s, ac_parse_value=True,
89
                             **self.load_options)
90
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
91
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
92
93
    def test_20_dumps(self):
94
        cnf = self.psr.loads(self.psr.dumps(self.cnf), ac_parse_value=True)
95
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
96
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
97
98
    def test_22_dumps__w_options(self):
99
        cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options),
100
                             ac_parse_value=True)
101
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
102
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
103
104
    def test_30_loads_with_order_kept(self):
105
        cnf = self.psr.loads(self.cnf_s, ac_parse_value=True, ac_ordered=True)
106
        if self.is_order_kept:
107
            self.assertTrue(dicts_equal(cnf, self.cnf, ordered=True),
108
                            "\n %r\nvs.\n %r" % (cnf, self.cnf))
109
        else:
110
            self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
111
112
113
class Test12(Test10):
114
115
    test_10_loads = test_12_loads__w_options = lambda: True
116
    test_20_loads = test_22_loads__w_options = lambda: True
117
118
    def test_10_loads__invalid_input(self):
119
        invalid_ini = "key=name"
120
        self.assertRaises(Exception, self.psr.loads, invalid_ini)
121
122
    def test_20_dumps__format(self):
123
        ref = self.cnf_s.replace(': ', ' = ')
124
        self.assertEquals(self.psr.dumps(self.cnf), ref)
125
126
127
class Test20(unittest.TestCase):
128
129
    psr_cls = TT.Parser
130
    cnf = CNF_0
131
    cnf_s = CNF_0_S
132
    cnf_fn = "conf0.ini"
133
    is_order_kept = True
134
135
    def setUp(self):
136
        self.psr = self.psr_cls()
137
        self.workdir = anyconfig.tests.common.setup_workdir()
138
        self.cpath = os.path.join(self.workdir, self.cnf_fn)
139
        with self.psr.wopen(self.cpath) as out:
140
            out.write(self.cnf_s)
141
142
    def tearDown(self):
143
        anyconfig.tests.common.cleanup_workdir(self.workdir)
144
145
    def test_10_load(self):
146
        cnf = self.psr.load(self.cpath)
147
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
148
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
149
150
    def test_20_dump(self):
151
        self.psr.dump(self.cnf, self.cpath)
152
        cnf = self.psr.load(self.cpath)
153
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
154
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
155
156
    def test_30_load__from_stream(self):
157
        with self.psr.ropen(self.cpath) as strm:
158
            cnf = self.psr.load(strm)
159
160
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
161
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
162
163
    def test_40_dump__to_stream(self):
164
        with self.psr.wopen(self.cpath) as strm:
165
            self.psr.dump(self.cnf, strm)
166
167
        cnf = self.psr.load(self.cpath)
168
        self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
169
        self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
170
171
    def test_50_load_with_order_kept(self):
172
        cnf = self.psr.load(self.cpath, ac_ordered=True)
173
        if self.is_order_kept:
174
            self.assertTrue(list(cnf.keys()), list(self.cnf.keys()))
175
        else:
176
            self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
177
178
# vim:sw=4:ts=4:et:
179