1 | n/a | """ |
---|
2 | n/a | Test implementation of the PEP 509: dictionary versionning. |
---|
3 | n/a | """ |
---|
4 | n/a | import unittest |
---|
5 | n/a | from test import support |
---|
6 | n/a | |
---|
7 | n/a | # PEP 509 is implemented in CPython but other Python implementations |
---|
8 | n/a | # don't require to implement it |
---|
9 | n/a | _testcapi = support.import_module('_testcapi') |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | class DictVersionTests(unittest.TestCase): |
---|
13 | n/a | type2test = dict |
---|
14 | n/a | |
---|
15 | n/a | def setUp(self): |
---|
16 | n/a | self.seen_versions = set() |
---|
17 | n/a | self.dict = None |
---|
18 | n/a | |
---|
19 | n/a | def check_version_unique(self, mydict): |
---|
20 | n/a | version = _testcapi.dict_get_version(mydict) |
---|
21 | n/a | self.assertNotIn(version, self.seen_versions) |
---|
22 | n/a | self.seen_versions.add(version) |
---|
23 | n/a | |
---|
24 | n/a | def check_version_changed(self, mydict, method, *args, **kw): |
---|
25 | n/a | result = method(*args, **kw) |
---|
26 | n/a | self.check_version_unique(mydict) |
---|
27 | n/a | return result |
---|
28 | n/a | |
---|
29 | n/a | def check_version_dont_change(self, mydict, method, *args, **kw): |
---|
30 | n/a | version1 = _testcapi.dict_get_version(mydict) |
---|
31 | n/a | self.seen_versions.add(version1) |
---|
32 | n/a | |
---|
33 | n/a | result = method(*args, **kw) |
---|
34 | n/a | |
---|
35 | n/a | version2 = _testcapi.dict_get_version(mydict) |
---|
36 | n/a | self.assertEqual(version2, version1, "version changed") |
---|
37 | n/a | |
---|
38 | n/a | return result |
---|
39 | n/a | |
---|
40 | n/a | def new_dict(self, *args, **kw): |
---|
41 | n/a | d = self.type2test(*args, **kw) |
---|
42 | n/a | self.check_version_unique(d) |
---|
43 | n/a | return d |
---|
44 | n/a | |
---|
45 | n/a | def test_constructor(self): |
---|
46 | n/a | # new empty dictionaries must all have an unique version |
---|
47 | n/a | empty1 = self.new_dict() |
---|
48 | n/a | empty2 = self.new_dict() |
---|
49 | n/a | empty3 = self.new_dict() |
---|
50 | n/a | |
---|
51 | n/a | # non-empty dictionaries must also have an unique version |
---|
52 | n/a | nonempty1 = self.new_dict(x='x') |
---|
53 | n/a | nonempty2 = self.new_dict(x='x', y='y') |
---|
54 | n/a | |
---|
55 | n/a | def test_copy(self): |
---|
56 | n/a | d = self.new_dict(a=1, b=2) |
---|
57 | n/a | |
---|
58 | n/a | d2 = self.check_version_dont_change(d, d.copy) |
---|
59 | n/a | |
---|
60 | n/a | # dict.copy() must create a dictionary with a new unique version |
---|
61 | n/a | self.check_version_unique(d2) |
---|
62 | n/a | |
---|
63 | n/a | def test_setitem(self): |
---|
64 | n/a | d = self.new_dict() |
---|
65 | n/a | |
---|
66 | n/a | # creating new keys must change the version |
---|
67 | n/a | self.check_version_changed(d, d.__setitem__, 'x', 'x') |
---|
68 | n/a | self.check_version_changed(d, d.__setitem__, 'y', 'y') |
---|
69 | n/a | |
---|
70 | n/a | # changing values must change the version |
---|
71 | n/a | self.check_version_changed(d, d.__setitem__, 'x', 1) |
---|
72 | n/a | self.check_version_changed(d, d.__setitem__, 'y', 2) |
---|
73 | n/a | |
---|
74 | n/a | def test_setitem_same_value(self): |
---|
75 | n/a | value = object() |
---|
76 | n/a | d = self.new_dict() |
---|
77 | n/a | |
---|
78 | n/a | # setting a key must change the version |
---|
79 | n/a | self.check_version_changed(d, d.__setitem__, 'key', value) |
---|
80 | n/a | |
---|
81 | n/a | # setting a key to the same value with dict.__setitem__ |
---|
82 | n/a | # must change the version |
---|
83 | n/a | self.check_version_changed(d, d.__setitem__, 'key', value) |
---|
84 | n/a | |
---|
85 | n/a | # setting a key to the same value with dict.update |
---|
86 | n/a | # must change the version |
---|
87 | n/a | self.check_version_changed(d, d.update, key=value) |
---|
88 | n/a | |
---|
89 | n/a | d2 = self.new_dict(key=value) |
---|
90 | n/a | self.check_version_changed(d, d.update, d2) |
---|
91 | n/a | |
---|
92 | n/a | def test_setitem_equal(self): |
---|
93 | n/a | class AlwaysEqual: |
---|
94 | n/a | def __eq__(self, other): |
---|
95 | n/a | return True |
---|
96 | n/a | |
---|
97 | n/a | value1 = AlwaysEqual() |
---|
98 | n/a | value2 = AlwaysEqual() |
---|
99 | n/a | self.assertTrue(value1 == value2) |
---|
100 | n/a | self.assertFalse(value1 != value2) |
---|
101 | n/a | |
---|
102 | n/a | d = self.new_dict() |
---|
103 | n/a | self.check_version_changed(d, d.__setitem__, 'key', value1) |
---|
104 | n/a | |
---|
105 | n/a | # setting a key to a value equal to the current value |
---|
106 | n/a | # with dict.__setitem__() must change the version |
---|
107 | n/a | self.check_version_changed(d, d.__setitem__, 'key', value2) |
---|
108 | n/a | |
---|
109 | n/a | # setting a key to a value equal to the current value |
---|
110 | n/a | # with dict.update() must change the version |
---|
111 | n/a | self.check_version_changed(d, d.update, key=value1) |
---|
112 | n/a | |
---|
113 | n/a | d2 = self.new_dict(key=value2) |
---|
114 | n/a | self.check_version_changed(d, d.update, d2) |
---|
115 | n/a | |
---|
116 | n/a | def test_setdefault(self): |
---|
117 | n/a | d = self.new_dict() |
---|
118 | n/a | |
---|
119 | n/a | # setting a key with dict.setdefault() must change the version |
---|
120 | n/a | self.check_version_changed(d, d.setdefault, 'key', 'value1') |
---|
121 | n/a | |
---|
122 | n/a | # don't change the version if the key already exists |
---|
123 | n/a | self.check_version_dont_change(d, d.setdefault, 'key', 'value2') |
---|
124 | n/a | |
---|
125 | n/a | def test_delitem(self): |
---|
126 | n/a | d = self.new_dict(key='value') |
---|
127 | n/a | |
---|
128 | n/a | # deleting a key with dict.__delitem__() must change the version |
---|
129 | n/a | self.check_version_changed(d, d.__delitem__, 'key') |
---|
130 | n/a | |
---|
131 | n/a | # don't change the version if the key doesn't exist |
---|
132 | n/a | self.check_version_dont_change(d, self.assertRaises, KeyError, |
---|
133 | n/a | d.__delitem__, 'key') |
---|
134 | n/a | |
---|
135 | n/a | def test_pop(self): |
---|
136 | n/a | d = self.new_dict(key='value') |
---|
137 | n/a | |
---|
138 | n/a | # pop() must change the version if the key exists |
---|
139 | n/a | self.check_version_changed(d, d.pop, 'key') |
---|
140 | n/a | |
---|
141 | n/a | # pop() must not change the version if the key does not exist |
---|
142 | n/a | self.check_version_dont_change(d, self.assertRaises, KeyError, |
---|
143 | n/a | d.pop, 'key') |
---|
144 | n/a | |
---|
145 | n/a | def test_popitem(self): |
---|
146 | n/a | d = self.new_dict(key='value') |
---|
147 | n/a | |
---|
148 | n/a | # popitem() must change the version if the dict is not empty |
---|
149 | n/a | self.check_version_changed(d, d.popitem) |
---|
150 | n/a | |
---|
151 | n/a | # popitem() must not change the version if the dict is empty |
---|
152 | n/a | self.check_version_dont_change(d, self.assertRaises, KeyError, |
---|
153 | n/a | d.popitem) |
---|
154 | n/a | |
---|
155 | n/a | def test_update(self): |
---|
156 | n/a | d = self.new_dict(key='value') |
---|
157 | n/a | |
---|
158 | n/a | # update() calling with no argument must not change the version |
---|
159 | n/a | self.check_version_dont_change(d, d.update) |
---|
160 | n/a | |
---|
161 | n/a | # update() must change the version |
---|
162 | n/a | self.check_version_changed(d, d.update, key='new value') |
---|
163 | n/a | |
---|
164 | n/a | d2 = self.new_dict(key='value 3') |
---|
165 | n/a | self.check_version_changed(d, d.update, d2) |
---|
166 | n/a | |
---|
167 | n/a | def test_clear(self): |
---|
168 | n/a | d = self.new_dict(key='value') |
---|
169 | n/a | |
---|
170 | n/a | # clear() must change the version if the dict is not empty |
---|
171 | n/a | self.check_version_changed(d, d.clear) |
---|
172 | n/a | |
---|
173 | n/a | # clear() must not change the version if the dict is empty |
---|
174 | n/a | self.check_version_dont_change(d, d.clear) |
---|
175 | n/a | |
---|
176 | n/a | |
---|
177 | n/a | class Dict(dict): |
---|
178 | n/a | pass |
---|
179 | n/a | |
---|
180 | n/a | |
---|
181 | n/a | class DictSubtypeVersionTests(DictVersionTests): |
---|
182 | n/a | type2test = Dict |
---|
183 | n/a | |
---|
184 | n/a | |
---|
185 | n/a | if __name__ == "__main__": |
---|
186 | n/a | unittest.main() |
---|