|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2011 - 2016 Satoru SATOH <ssato @ redhat.com> |
|
3
|
|
|
# |
|
4
|
|
|
# pylint: disable=missing-docstring,invalid-name |
|
5
|
|
|
from __future__ import absolute_import |
|
6
|
|
|
import unittest |
|
7
|
|
|
import m9dicts.dicts as TT |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class Test_10_UpdateWithReplaceDict(unittest.TestCase): |
|
11
|
|
|
|
|
12
|
|
|
od0 = TT.OrderedDict((("a", 1), ("b", [1, 3]), ("c", "abc"), ("f", None))) |
|
13
|
|
|
up0 = TT.OrderedDict((("a", 2), ("b", [0, 1]), |
|
14
|
|
|
("c", TT.OrderedDict((("d", "d"), ("e", 1)))), |
|
15
|
|
|
("d", "d"))) |
|
16
|
|
|
cls = TT.UpdateWithReplaceDict |
|
17
|
|
|
|
|
18
|
|
|
def setUp(self): |
|
19
|
|
|
self.mds = (self.cls(self.od0), self.cls(self.od0.items()), |
|
20
|
|
|
self.cls(**self.od0)) |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
def check_updated(self, *others, **another): |
|
|
|
|
|
|
23
|
|
|
upd = self.up0 |
|
24
|
|
|
ref = self.mds[0].copy() |
|
25
|
|
|
|
|
26
|
|
|
for md in self.mds: |
|
27
|
|
|
md.update(*others, **another) |
|
28
|
|
|
self.assertTrue(all(md[k] == upd[k] for k in upd.keys())) |
|
29
|
|
|
self.assertTrue(all(md[k] == ref[k] for k in ref.keys() |
|
30
|
|
|
if k not in upd)) |
|
31
|
|
|
|
|
32
|
|
|
def test_10_update_with_a_mdict(self): |
|
33
|
|
|
self.check_updated(self.cls(self.up0)) |
|
34
|
|
|
|
|
35
|
|
|
def test_12_update_with_a_dict(self): |
|
36
|
|
|
self.check_updated(dict(self.up0)) |
|
37
|
|
|
|
|
38
|
|
|
def test_14_update_with_kv_tuples(self): |
|
39
|
|
|
self.check_updated(list(self.up0.items())) |
|
40
|
|
|
|
|
41
|
|
|
def test_16_update_with_invalid(self): |
|
42
|
|
|
md0 = self.cls(self.od0) |
|
43
|
|
|
self.assertTrue(isinstance(md0, self.cls)) |
|
44
|
|
|
try: |
|
45
|
|
|
raised = False |
|
46
|
|
|
md0.update(1) |
|
47
|
|
|
except (ValueError, TypeError): |
|
48
|
|
|
raised = True |
|
49
|
|
|
|
|
50
|
|
|
self.assertTrue(raised) |
|
51
|
|
|
|
|
52
|
|
|
def test_20_update_with_a_odict_and_kwargs(self): |
|
53
|
|
|
other = self.up0.copy() |
|
54
|
|
|
another = TT.OrderedDict((("b", other["b"]), )) |
|
55
|
|
|
del other["b"] |
|
56
|
|
|
|
|
57
|
|
|
self.check_updated(other, **another) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
class Test_20_UpdateWoReplaceDict(Test_10_UpdateWithReplaceDict): |
|
61
|
|
|
|
|
62
|
|
|
cls = TT.UpdateWoReplaceDict |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
def check_updated(self, *others, **another): |
|
|
|
|
|
|
65
|
|
|
upd = self.up0 |
|
66
|
|
|
ref = self.mds[0].copy() |
|
67
|
|
|
|
|
68
|
|
|
for md in self.mds: |
|
69
|
|
|
md.update(*others, **another) |
|
70
|
|
|
self.assertTrue(all(md[k] == upd[k] for k in upd.keys() |
|
71
|
|
|
if k not in ref)) |
|
72
|
|
|
self.assertTrue(all(md[k] == ref[k] for k in ref.keys())) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
class Test_30_UpdateWithMergeDict(Test_10_UpdateWithReplaceDict): |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
cls = TT.UpdateWithMergeDict |
|
78
|
|
|
replaced_keys = "a b d".split() |
|
79
|
|
|
|
|
80
|
|
|
def check_updated(self, *others, **another): |
|
81
|
|
|
upd = self.up0 |
|
82
|
|
|
ref = self.mds[0].copy() |
|
83
|
|
|
|
|
84
|
|
|
for md in self.mds: |
|
85
|
|
|
md.update(*others, **another) |
|
86
|
|
|
self.assertTrue(all(md[k] == upd[k] for k in self.replaced_keys)) |
|
87
|
|
|
self.assertTrue(all(md["c"][k] == upd["c"][k] for k |
|
88
|
|
|
in upd["c"].keys())) |
|
89
|
|
|
self.assertTrue(all(md[k] == ref[k] for k in ref.keys() |
|
90
|
|
|
if k not in upd)) |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
View Code Duplication |
class Test_32_UpdateWithMergeDict_kept(Test_10_UpdateWithReplaceDict): |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
class UWMDK(TT.UpdateWithMergeDict): |
|
96
|
|
|
keep = True |
|
97
|
|
|
|
|
98
|
|
|
cls = UWMDK |
|
99
|
|
|
|
|
100
|
|
|
def check_updated(self, *others, **another): |
|
101
|
|
|
upd = self.up0 |
|
102
|
|
|
ref = self.mds[0].copy() |
|
103
|
|
|
|
|
104
|
|
|
for md in self.mds: |
|
105
|
|
|
md.update(*others, **another) |
|
106
|
|
|
self.assertTrue(all(md[k] == upd[k] for k in ["d"])) |
|
107
|
|
|
self.assertEqual(md["c"], ref["c"]) |
|
108
|
|
|
self.assertTrue(all(md[k] == ref[k] for k in ref.keys() |
|
109
|
|
|
if k not in upd)) |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
class Test_34_UpdateWithMergeListsDict(Test_30_UpdateWithMergeDict): |
|
113
|
|
|
|
|
114
|
|
|
cls = TT.UpdateWithMergeListsDict |
|
115
|
|
|
replaced_keys = "a d".split() |
|
116
|
|
|
|
|
117
|
|
|
def check_updated(self, *others, **another): |
|
118
|
|
|
tcls = Test_34_UpdateWithMergeListsDict |
|
119
|
|
|
super(tcls, self).check_updated(*others, **another) |
|
120
|
|
|
for md in self.mds: |
|
121
|
|
|
self.assertEqual(md["b"], [1, 3, 0]) |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
class Test_40_UpdateWithReplaceOrderedDict(Test_10_UpdateWithReplaceDict): |
|
125
|
|
|
|
|
126
|
|
|
cls = TT.UpdateWithReplaceOrderedDict |
|
127
|
|
|
|
|
128
|
|
|
def setUp(self): |
|
129
|
|
|
self.mds = (self.cls(self.od0), self.cls(self.od0.items())) |
|
130
|
|
|
|
|
131
|
|
|
def check_updated(self, *others, **another): |
|
132
|
|
|
tcls = Test_40_UpdateWithReplaceOrderedDict |
|
133
|
|
|
super(tcls, self).check_updated(*others, **another) |
|
134
|
|
|
for md in self.mds: |
|
135
|
|
|
self.assertEqual(list(md.keys()), "a b c f d".split()) |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
class Test_50_UpdateWoReplaceOrderedDict(Test_20_UpdateWoReplaceDict): |
|
139
|
|
|
|
|
140
|
|
|
cls = TT.UpdateWoReplaceOrderedDict |
|
141
|
|
|
|
|
142
|
|
|
def setUp(self): |
|
143
|
|
|
self.mds = (self.cls(self.od0), self.cls(self.od0.items())) |
|
144
|
|
|
|
|
145
|
|
|
def check_updated(self, *others, **another): |
|
146
|
|
|
tcls = Test_50_UpdateWoReplaceOrderedDict |
|
147
|
|
|
super(tcls, self).check_updated(*others, **another) |
|
148
|
|
|
for md in self.mds: |
|
149
|
|
|
self.assertEqual(list(md.keys()), "a b c f d".split()) |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
class Test_60_UpdateWithMergeOrderedDict(Test_30_UpdateWithMergeDict): |
|
153
|
|
|
|
|
154
|
|
|
cls = TT.UpdateWithMergeOrderedDict |
|
155
|
|
|
|
|
156
|
|
|
def setUp(self): |
|
157
|
|
|
self.mds = (self.cls(self.od0), self.cls(self.od0.items())) |
|
158
|
|
|
|
|
159
|
|
|
def check_updated(self, *others, **another): |
|
160
|
|
|
tcls = Test_60_UpdateWithMergeOrderedDict |
|
161
|
|
|
super(tcls, self).check_updated(*others, **another) |
|
162
|
|
|
for md in self.mds: |
|
163
|
|
|
self.assertEqual(list(md.keys()), "a b c f d".split()) |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
_TCLS = Test_34_UpdateWithMergeListsDict |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
class Test_62_UpdateWithMergeListsOrderedDict(_TCLS): |
|
170
|
|
|
|
|
171
|
|
|
cls = TT.UpdateWithMergeListsOrderedDict |
|
172
|
|
|
|
|
173
|
|
|
def setUp(self): |
|
174
|
|
|
self.mds = (self.cls(self.od0), self.cls(self.od0.items())) |
|
175
|
|
|
|
|
176
|
|
|
def check_updated(self, *others, **another): |
|
177
|
|
|
tcls = Test_62_UpdateWithMergeListsOrderedDict |
|
178
|
|
|
super(tcls, self).check_updated(*others, **another) |
|
179
|
|
|
for md in self.mds: |
|
180
|
|
|
self.assertEqual(list(md.keys()), "a b c f d".split()) |
|
181
|
|
|
|
|
182
|
|
|
# vim:sw=4:ts=4:et: |
|
183
|
|
|
|