test_shelve.py 3.64 KB
Newer Older
1 2 3 4 5 6 7 8
import os
import unittest
import shelve
import glob
from test import test_support

class TestCase(unittest.TestCase):

9
    fn = "shelftemp" + os.extsep + "db"
Tim Peters's avatar
Tim Peters committed
10

11 12
    def test_ascii_file_shelf(self):
        try:
13
            s = shelve.open(self.fn, protocol=0)
14 15 16 17 18 19 20 21 22
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
            s.close()
        finally:
            for f in glob.glob(self.fn+"*"):
                os.unlink(f)

    def test_binary_file_shelf(self):
        try:
23
            s = shelve.open(self.fn, protocol=1)
24 25 26 27 28 29 30
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
            s.close()
        finally:
            for f in glob.glob(self.fn+"*"):
                os.unlink(f)

31 32 33 34 35 36 37 38 39 40
    def test_proto2_file_shelf(self):
        try:
            s = shelve.open(self.fn, protocol=2)
            s['key1'] = (1,2,3,4)
            self.assertEqual(s['key1'], (1,2,3,4))
            s.close()
        finally:
            for f in glob.glob(self.fn+"*"):
                os.unlink(f)

41 42
    def test_in_memory_shelf(self):
        d1 = {}
43
        s = shelve.Shelf(d1, protocol=0)
44 45 46 47
        s['key1'] = (1,2,3,4)
        self.assertEqual(s['key1'], (1,2,3,4))
        s.close()
        d2 = {}
48
        s = shelve.Shelf(d2, protocol=1)
49 50 51 52 53 54 55
        s['key1'] = (1,2,3,4)
        self.assertEqual(s['key1'], (1,2,3,4))
        s.close()

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

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    def test_mutable_entry(self):
        d1 = {}
        s = shelve.Shelf(d1, protocol=2, writeback=False)
        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])
        s.close()

        d2 = {}
        s = shelve.Shelf(d2, protocol=2, writeback=True)
        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])
        s.close()

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


77
from test import mapping_tests
78

79
class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
80 81 82 83
    fn = "shelftemp.db"
    counter = 0
    def __init__(self, *args, **kw):
        self._db = []
84
        mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
85
    type2test = shelve.Shelf
86 87 88 89
    def _reference(self):
        return {"key1":"value1", "key2":2, "key3":(1,2,3)}
    def _empty_mapping(self):
        if self._in_mem:
90
            x= shelve.Shelf({}, **self._args)
91 92
        else:
            self.counter+=1
93
            x= shelve.open(self.fn+str(self.counter), **self._args)
94 95 96 97 98 99 100 101 102 103 104
        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+"*"):
                os.unlink(f)

class TestAsciiFileShelve(TestShelveBase):
105
    _args={'protocol':0}
106 107
    _in_mem = False
class TestBinaryFileShelve(TestShelveBase):
108
    _args={'protocol':1}
109 110 111
    _in_mem = False
class TestProto2FileShelve(TestShelveBase):
    _args={'protocol':2}
112 113
    _in_mem = False
class TestAsciiMemShelve(TestShelveBase):
114
    _args={'protocol':0}
115 116
    _in_mem = True
class TestBinaryMemShelve(TestShelveBase):
117
    _args={'protocol':1}
118 119 120
    _in_mem = True
class TestProto2MemShelve(TestShelveBase):
    _args={'protocol':2}
121 122 123
    _in_mem = True

def test_main():
124 125 126 127 128 129 130 131 132
    test_support.run_unittest(
        TestAsciiFileShelve,
        TestBinaryFileShelve,
        TestProto2FileShelve,
        TestAsciiMemShelve,
        TestBinaryMemShelve,
        TestProto2MemShelve,
        TestCase
    )
133 134 135

if __name__ == "__main__":
    test_main()