ยปCore Development>Code coverage>Lib/test/test_dict_version.py

Python code coverage for Lib/test/test_dict_version.py

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