test_shelve.py 6.24 KB
Newer Older
1 2 3
import unittest
import shelve
import glob
4
from test import support
5
from collections.abc import MutableMapping
6
from test.test_dbm import dbm_iterator
7 8 9 10

def L1(s):
    return s.decode("latin-1")

11
class byteskeydict(MutableMapping):
12 13 14 15 16 17 18 19 20 21 22 23 24 25
    "Mapping that supports bytes keys"

    def __init__(self):
        self.d = {}

    def __getitem__(self, key):
        return self.d[L1(key)]

    def __setitem__(self, key, value):
        self.d[L1(key)] = value

    def __delitem__(self, key):
        del self.d[L1(key)]

26 27 28
    def __len__(self):
        return len(self.d)

29 30
    def iterkeys(self):
        for k in self.d.keys():
31
            yield k.encode("latin-1")
32

33 34
    __iter__ = iterkeys

35 36 37 38 39 40
    def keys(self):
        return list(self.iterkeys())

    def copy(self):
        return byteskeydict(self.d)

41 42 43

class TestCase(unittest.TestCase):

44
    fn = "shelftemp.db"
Tim Peters's avatar
Tim Peters committed
45

46 47
    def tearDown(self):
        for f in glob.glob(self.fn+"*"):
48
            support.unlink(f)
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    def test_close(self):
        d1 = {}
        s = shelve.Shelf(d1, protocol=2, writeback=False)
        s['key1'] = [1,2,3,4]
        self.assertEqual(s['key1'], [1,2,3,4])
        self.assertEqual(len(s), 1)
        s.close()
        self.assertRaises(ValueError, len, s)
        try:
            s['key1']
        except ValueError:
            pass
        else:
            self.fail('Closed shelf should not find a key')

65
    def test_ascii_file_shelf(self):
66
        s = shelve.open(self.fn, protocol=0)
67 68 69 70
        try:
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
        finally:
71
            s.close()
72 73

    def test_binary_file_shelf(self):
74
        s = shelve.open(self.fn, protocol=1)
75 76 77 78
        try:
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
        finally:
79
            s.close()
80

81
    def test_proto2_file_shelf(self):
82
        s = shelve.open(self.fn, protocol=2)
83 84 85 86
        try:
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
        finally:
87
            s.close()
88

89
    def test_in_memory_shelf(self):
90
        d1 = byteskeydict()
91 92 93
        with shelve.Shelf(d1, protocol=0) as s:
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
94
        d2 = byteskeydict()
95 96 97
        with shelve.Shelf(d2, protocol=1) as s:
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
98 99

        self.assertEqual(len(d1), 1)
100 101
        self.assertEqual(len(d2), 1)
        self.assertNotEqual(d1.items(), d2.items())
102

103
    def test_mutable_entry(self):
104
        d1 = byteskeydict()
105 106 107 108 109
        with shelve.Shelf(d1, protocol=2, writeback=False) as s:
            s['key1'] = [1,2,3,4]
            self.assertEqual(s['key1'], [1,2,3,4])
            s['key1'].append(5)
            self.assertEqual(s['key1'], [1,2,3,4])
110

111
        d2 = byteskeydict()
112 113 114 115 116
        with shelve.Shelf(d2, protocol=2, writeback=True) as s:
            s['key1'] = [1,2,3,4]
            self.assertEqual(s['key1'], [1,2,3,4])
            s['key1'].append(5)
            self.assertEqual(s['key1'], [1,2,3,4,5])
117 118 119 120

        self.assertEqual(len(d1), 1)
        self.assertEqual(len(d2), 1)

121 122 123 124 125 126 127
    def test_keyencoding(self):
        d = {}
        key = 'Pöp'
        # the default keyencoding is utf-8
        shelve.Shelf(d)[key] = [1]
        self.assertIn(key.encode('utf-8'), d)
        # but a different one can be given
128 129
        shelve.Shelf(d, keyencoding='latin-1')[key] = [1]
        self.assertIn(key.encode('latin-1'), d)
130 131 132 133
        # with all consequences
        s = shelve.Shelf(d, keyencoding='ascii')
        self.assertRaises(UnicodeEncodeError, s.__setitem__, key, [1])

134 135 136 137 138
    def test_writeback_also_writes_immediately(self):
        # Issue 5754
        d = {}
        key = 'key'
        encodedkey = key.encode('utf-8')
139 140 141 142
        with shelve.Shelf(d, writeback=True) as s:
            s[key] = [1]
            p1 = d[encodedkey]  # Will give a KeyError if backing store not updated
            s['key'].append(2)
143 144 145
        p2 = d[encodedkey]
        self.assertNotEqual(p1, p2)  # Write creates new object in store

146 147 148 149 150 151 152 153 154 155 156 157 158
    def test_with(self):
        d1 = {}
        with shelve.Shelf(d1, protocol=2, writeback=False) as s:
            s['key1'] = [1,2,3,4]
            self.assertEqual(s['key1'], [1,2,3,4])
            self.assertEqual(len(s), 1)
        self.assertRaises(ValueError, len, s)
        try:
            s['key1']
        except ValueError:
            pass
        else:
            self.fail('Closed shelf should not find a key')
159

160 161 162 163
    def test_default_protocol(self):
        with shelve.Shelf({}) as s:
            self.assertEqual(s._protocol, 3)

164
from test import mapping_tests
165

166
class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
167 168 169 170
    fn = "shelftemp.db"
    counter = 0
    def __init__(self, *args, **kw):
        self._db = []
171
        mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
172
    type2test = shelve.Shelf
173 174 175 176
    def _reference(self):
        return {"key1":"value1", "key2":2, "key3":(1,2,3)}
    def _empty_mapping(self):
        if self._in_mem:
177
            x= shelve.Shelf(byteskeydict(), **self._args)
178 179
        else:
            self.counter+=1
180
            x= shelve.open(self.fn+str(self.counter), **self._args)
181 182 183 184 185 186 187 188
        self._db.append(x)
        return x
    def tearDown(self):
        for db in self._db:
            db.close()
        self._db = []
        if not self._in_mem:
            for f in glob.glob(self.fn+"*"):
189
                support.unlink(f)
190 191

class TestAsciiFileShelve(TestShelveBase):
192
    _args={'protocol':0}
193 194
    _in_mem = False
class TestBinaryFileShelve(TestShelveBase):
195
    _args={'protocol':1}
196 197 198
    _in_mem = False
class TestProto2FileShelve(TestShelveBase):
    _args={'protocol':2}
199 200
    _in_mem = False
class TestAsciiMemShelve(TestShelveBase):
201
    _args={'protocol':0}
202 203
    _in_mem = True
class TestBinaryMemShelve(TestShelveBase):
204
    _args={'protocol':1}
205 206 207
    _in_mem = True
class TestProto2MemShelve(TestShelveBase):
    _args={'protocol':2}
208 209 210
    _in_mem = True

def test_main():
211
    for module in dbm_iterator():
212
        support.run_unittest(
213 214 215 216 217 218 219 220
            TestAsciiFileShelve,
            TestBinaryFileShelve,
            TestProto2FileShelve,
            TestAsciiMemShelve,
            TestBinaryMemShelve,
            TestProto2MemShelve,
            TestCase
        )
221 222 223

if __name__ == "__main__":
    test_main()