»Core Development>Code coverage>Lib/test/test_shelve.py

Python code coverage for Lib/test/test_shelve.py

#countcontent
1n/aimport unittest
2n/aimport shelve
3n/aimport glob
4n/afrom test import support
5n/afrom collections.abc import MutableMapping
6n/afrom test.test_dbm import dbm_iterator
7n/a
8n/adef L1(s):
9n/a return s.decode("latin-1")
10n/a
11n/aclass byteskeydict(MutableMapping):
12n/a "Mapping that supports bytes keys"
13n/a
14n/a def __init__(self):
15n/a self.d = {}
16n/a
17n/a def __getitem__(self, key):
18n/a return self.d[L1(key)]
19n/a
20n/a def __setitem__(self, key, value):
21n/a self.d[L1(key)] = value
22n/a
23n/a def __delitem__(self, key):
24n/a del self.d[L1(key)]
25n/a
26n/a def __len__(self):
27n/a return len(self.d)
28n/a
29n/a def iterkeys(self):
30n/a for k in self.d.keys():
31n/a yield k.encode("latin-1")
32n/a
33n/a __iter__ = iterkeys
34n/a
35n/a def keys(self):
36n/a return list(self.iterkeys())
37n/a
38n/a def copy(self):
39n/a return byteskeydict(self.d)
40n/a
41n/a
42n/aclass TestCase(unittest.TestCase):
43n/a
44n/a fn = "shelftemp.db"
45n/a
46n/a def tearDown(self):
47n/a for f in glob.glob(self.fn+"*"):
48n/a support.unlink(f)
49n/a
50n/a def test_close(self):
51n/a d1 = {}
52n/a s = shelve.Shelf(d1, protocol=2, writeback=False)
53n/a s['key1'] = [1,2,3,4]
54n/a self.assertEqual(s['key1'], [1,2,3,4])
55n/a self.assertEqual(len(s), 1)
56n/a s.close()
57n/a self.assertRaises(ValueError, len, s)
58n/a try:
59n/a s['key1']
60n/a except ValueError:
61n/a pass
62n/a else:
63n/a self.fail('Closed shelf should not find a key')
64n/a
65n/a def test_ascii_file_shelf(self):
66n/a s = shelve.open(self.fn, protocol=0)
67n/a try:
68n/a s['key1'] = (1,2,3,4)
69n/a self.assertEqual(s['key1'], (1,2,3,4))
70n/a finally:
71n/a s.close()
72n/a
73n/a def test_binary_file_shelf(self):
74n/a s = shelve.open(self.fn, protocol=1)
75n/a try:
76n/a s['key1'] = (1,2,3,4)
77n/a self.assertEqual(s['key1'], (1,2,3,4))
78n/a finally:
79n/a s.close()
80n/a
81n/a def test_proto2_file_shelf(self):
82n/a s = shelve.open(self.fn, protocol=2)
83n/a try:
84n/a s['key1'] = (1,2,3,4)
85n/a self.assertEqual(s['key1'], (1,2,3,4))
86n/a finally:
87n/a s.close()
88n/a
89n/a def test_in_memory_shelf(self):
90n/a d1 = byteskeydict()
91n/a s = shelve.Shelf(d1, protocol=0)
92n/a s['key1'] = (1,2,3,4)
93n/a self.assertEqual(s['key1'], (1,2,3,4))
94n/a s.close()
95n/a d2 = byteskeydict()
96n/a s = shelve.Shelf(d2, protocol=1)
97n/a s['key1'] = (1,2,3,4)
98n/a self.assertEqual(s['key1'], (1,2,3,4))
99n/a s.close()
100n/a
101n/a self.assertEqual(len(d1), 1)
102n/a self.assertEqual(len(d2), 1)
103n/a self.assertNotEqual(d1.items(), d2.items())
104n/a
105n/a def test_mutable_entry(self):
106n/a d1 = byteskeydict()
107n/a s = shelve.Shelf(d1, protocol=2, writeback=False)
108n/a s['key1'] = [1,2,3,4]
109n/a self.assertEqual(s['key1'], [1,2,3,4])
110n/a s['key1'].append(5)
111n/a self.assertEqual(s['key1'], [1,2,3,4])
112n/a s.close()
113n/a
114n/a d2 = byteskeydict()
115n/a s = shelve.Shelf(d2, protocol=2, writeback=True)
116n/a s['key1'] = [1,2,3,4]
117n/a self.assertEqual(s['key1'], [1,2,3,4])
118n/a s['key1'].append(5)
119n/a self.assertEqual(s['key1'], [1,2,3,4,5])
120n/a s.close()
121n/a
122n/a self.assertEqual(len(d1), 1)
123n/a self.assertEqual(len(d2), 1)
124n/a
125n/a def test_keyencoding(self):
126n/a d = {}
127n/a key = 'Pöp'
128n/a # the default keyencoding is utf-8
129n/a shelve.Shelf(d)[key] = [1]
130n/a self.assertIn(key.encode('utf-8'), d)
131n/a # but a different one can be given
132n/a shelve.Shelf(d, keyencoding='latin-1')[key] = [1]
133n/a self.assertIn(key.encode('latin-1'), d)
134n/a # with all consequences
135n/a s = shelve.Shelf(d, keyencoding='ascii')
136n/a self.assertRaises(UnicodeEncodeError, s.__setitem__, key, [1])
137n/a
138n/a def test_writeback_also_writes_immediately(self):
139n/a # Issue 5754
140n/a d = {}
141n/a key = 'key'
142n/a encodedkey = key.encode('utf-8')
143n/a s = shelve.Shelf(d, writeback=True)
144n/a s[key] = [1]
145n/a p1 = d[encodedkey] # Will give a KeyError if backing store not updated
146n/a s['key'].append(2)
147n/a s.close()
148n/a p2 = d[encodedkey]
149n/a self.assertNotEqual(p1, p2) # Write creates new object in store
150n/a
151n/a def test_with(self):
152n/a d1 = {}
153n/a with shelve.Shelf(d1, protocol=2, writeback=False) as s:
154n/a s['key1'] = [1,2,3,4]
155n/a self.assertEqual(s['key1'], [1,2,3,4])
156n/a self.assertEqual(len(s), 1)
157n/a self.assertRaises(ValueError, len, s)
158n/a try:
159n/a s['key1']
160n/a except ValueError:
161n/a pass
162n/a else:
163n/a self.fail('Closed shelf should not find a key')
164n/a
165n/a def test_default_protocol(self):
166n/a with shelve.Shelf({}) as s:
167n/a self.assertEqual(s._protocol, 3)
168n/a
169n/afrom test import mapping_tests
170n/a
171n/aclass TestShelveBase(mapping_tests.BasicTestMappingProtocol):
172n/a fn = "shelftemp.db"
173n/a counter = 0
174n/a def __init__(self, *args, **kw):
175n/a self._db = []
176n/a mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
177n/a type2test = shelve.Shelf
178n/a def _reference(self):
179n/a return {"key1":"value1", "key2":2, "key3":(1,2,3)}
180n/a def _empty_mapping(self):
181n/a if self._in_mem:
182n/a x= shelve.Shelf(byteskeydict(), **self._args)
183n/a else:
184n/a self.counter+=1
185n/a x= shelve.open(self.fn+str(self.counter), **self._args)
186n/a self._db.append(x)
187n/a return x
188n/a def tearDown(self):
189n/a for db in self._db:
190n/a db.close()
191n/a self._db = []
192n/a if not self._in_mem:
193n/a for f in glob.glob(self.fn+"*"):
194n/a support.unlink(f)
195n/a
196n/aclass TestAsciiFileShelve(TestShelveBase):
197n/a _args={'protocol':0}
198n/a _in_mem = False
199n/aclass TestBinaryFileShelve(TestShelveBase):
200n/a _args={'protocol':1}
201n/a _in_mem = False
202n/aclass TestProto2FileShelve(TestShelveBase):
203n/a _args={'protocol':2}
204n/a _in_mem = False
205n/aclass TestAsciiMemShelve(TestShelveBase):
206n/a _args={'protocol':0}
207n/a _in_mem = True
208n/aclass TestBinaryMemShelve(TestShelveBase):
209n/a _args={'protocol':1}
210n/a _in_mem = True
211n/aclass TestProto2MemShelve(TestShelveBase):
212n/a _args={'protocol':2}
213n/a _in_mem = True
214n/a
215n/adef test_main():
216n/a for module in dbm_iterator():
217n/a support.run_unittest(
218n/a TestAsciiFileShelve,
219n/a TestBinaryFileShelve,
220n/a TestProto2FileShelve,
221n/a TestAsciiMemShelve,
222n/a TestBinaryMemShelve,
223n/a TestProto2MemShelve,
224n/a TestCase
225n/a )
226n/a
227n/aif __name__ == "__main__":
228n/a test_main()