-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
183 lines (114 loc) · 4.54 KB
/
tests.py
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
import unittest
import chainmap
class TestInit(unittest.TestCase):
def test_accept_n_dicts(self):
chainmap.ChainMap()
chainmap.ChainMap({})
chainmap.ChainMap({}, {})
chainmap.ChainMap({}, {}, {})
def test_get_item(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
self.assertEqual(1, cm["a"])
self.assertEqual(3, cm["b"])
with self.assertRaises(KeyError):
cm["d"]
class TestMapsAttribute(unittest.TestCase):
def test_slice(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
self.assertEqual({'a': 1}, cm.maps[0])
self.assertEqual({'a': 2, 'b': 3}, cm.maps[1])
self.assertEqual({'a': 2, 'b': 3}, cm.maps[-1])
with self.assertRaises(IndexError):
cm.maps[2]
def test_index_0_always_exists(self):
cm = chainmap.ChainMap()
self.assertEqual({}, cm.maps[0])
class TestNewChild(unittest.TestCase):
def test_no_param(self):
cm = chainmap.ChainMap({'brian': "wanda"})
cm_copy = cm.new_child()
self.assertEqual(({}, {"brian": "wanda"}), cm_copy.maps)
self.assertNotEqual(id(cm), id(cm_copy))
def test_with_param(self):
cm = chainmap.ChainMap({'brian': "wanda"})
cm_copy = cm.new_child({"spam": "SPAM"})
self.assertEqual(({"spam": "SPAM"}, {"brian": "wanda"}), cm_copy.maps)
self.assertNotEqual(id(cm), id(cm_copy))
class TestParents(unittest.TestCase):
def test_empty(self):
cm = chainmap.ChainMap()
cm_parent = cm.parents
self.assertEqual([{}], cm_parent.maps)
self.assertNotEqual(id(cm), id(cm_parent))
def test_with_param(self):
cm = chainmap.ChainMap({'brian': "wanda"}, {"spam": "SPAM"}, {'holy': "graal"})
cm_parent = cm.parents
self.assertEqual(({"spam": "SPAM"}, {'holy': "graal"}), cm_parent.maps)
self.assertNotEqual(id(cm), id(cm_parent))
class TestSetItem(unittest.TestCase):
def test_in_first_mapping(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
cm["a"] = "blurp"
self.assertEqual("blurp", cm["a"])
self.assertEqual(2, cm.maps[1]['a'])
def test_in_other_mapping(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
cm["b"] = "cheese"
self.assertEqual("cheese", cm["b"])
self.assertEqual("cheese", cm.maps[0]['b'])
self.assertEqual(3, cm.maps[1]['b'])
def test_new_value(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
cm["c"] = "spam"
self.assertEqual("spam", cm["c"])
class TestDeleteItem(unittest.TestCase):
def test_in_first_mapping(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
del cm["a"]
self.assertEqual(2, cm["a"])
self.assertIn("a", cm)
def test_does_not_exist(self):
cm = chainmap.ChainMap()
with self.assertRaises(KeyError):
del cm["a"]
class TestInOperator(unittest.TestCase):
def test_success(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
self.assertIn("a", cm)
self.assertIn("b", cm)
def test_failure(self):
cm = chainmap.ChainMap()
self.assertNotIn("a", cm)
class TestCastToList(unittest.TestCase):
def test_success(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3}, {"c": "monty"})
l = list(cm)
self.assertEqual(set(["a", "b", "c"]), set(l)) #ordre important?
self.assertIn("b", cm)
class TestLength(unittest.TestCase):
def test_empty(self):
cm = chainmap.ChainMap()
self.assertEqual(0, len(cm))
def test_filled(self):
cm = chainmap.ChainMap({"a": 1}, {"a": 2, "b": 3})
self.assertEqual(2, len(cm))
class TestCastToBool(unittest.TestCase):
def test_empty(self):
cm = chainmap.ChainMap()
self.assertFalse(bool(cm))
def test_filled(self):
cm = chainmap.ChainMap({"a": 1})
self.assertTrue(bool(cm))
class TestItems(unittest.TestCase):
def test(self):
cm = chainmap.ChainMap({'d': 567, 'a': 'e'}, {'d': 34}, {'f': 45})
for k, v in cm.items():
self.assertIn((k, v), (("d", 567), ("a", 'e'), ("f", 45)))
self.assertFalse((k == "d" and v == 34))
class TestCastToDict(unittest.TestCase):
def test(self):
cm = chainmap.ChainMap({'d': 567, 'a': 'e'}, {'d': 34}, {'f': 45})
d = dict(cm)
self.assertEqual({'d': 567, 'f': 45, 'a': 'e'}, d)
if __name__ == '__main__':
unittest.main()