5 from test
import test_support
7 class TestCase(unittest
.TestCase
):
9 fn
= "shelftemp" + os
.extsep
+ "db"
13 s
= shelve
.Shelf(d1
, protocol
=2, writeback
=False)
15 self
.assertEqual(s
['key1'], [1,2,3,4])
16 self
.assertEqual(len(s
), 1)
18 self
.assertRaises(ValueError, len, s
)
24 self
.fail('Closed shelf should not find a key')
26 def test_ascii_file_shelf(self
):
28 s
= shelve
.open(self
.fn
, protocol
=0)
30 self
.assertEqual(s
['key1'], (1,2,3,4))
33 for f
in glob
.glob(self
.fn
+"*"):
36 def test_binary_file_shelf(self
):
38 s
= shelve
.open(self
.fn
, protocol
=1)
40 self
.assertEqual(s
['key1'], (1,2,3,4))
43 for f
in glob
.glob(self
.fn
+"*"):
46 def test_proto2_file_shelf(self
):
48 s
= shelve
.open(self
.fn
, protocol
=2)
50 self
.assertEqual(s
['key1'], (1,2,3,4))
53 for f
in glob
.glob(self
.fn
+"*"):
56 def test_in_memory_shelf(self
):
58 s
= shelve
.Shelf(d1
, protocol
=0)
60 self
.assertEqual(s
['key1'], (1,2,3,4))
63 s
= shelve
.Shelf(d2
, protocol
=1)
65 self
.assertEqual(s
['key1'], (1,2,3,4))
68 self
.assertEqual(len(d1
), 1)
69 self
.assertNotEqual(d1
, d2
)
71 def test_mutable_entry(self
):
73 s
= shelve
.Shelf(d1
, protocol
=2, writeback
=False)
75 self
.assertEqual(s
['key1'], [1,2,3,4])
77 self
.assertEqual(s
['key1'], [1,2,3,4])
81 s
= shelve
.Shelf(d2
, protocol
=2, writeback
=True)
83 self
.assertEqual(s
['key1'], [1,2,3,4])
85 self
.assertEqual(s
['key1'], [1,2,3,4,5])
88 self
.assertEqual(len(d1
), 1)
89 self
.assertEqual(len(d2
), 1)
92 from test
import mapping_tests
94 class TestShelveBase(mapping_tests
.BasicTestMappingProtocol
):
97 def __init__(self
, *args
, **kw
):
99 mapping_tests
.BasicTestMappingProtocol
.__init
__(self
, *args
, **kw
)
100 type2test
= shelve
.Shelf
101 def _reference(self
):
102 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
103 def _empty_mapping(self
):
105 x
= shelve
.Shelf({}, **self
._args
)
108 x
= shelve
.open(self
.fn
+str(self
.counter
), **self
._args
)
116 for f
in glob
.glob(self
.fn
+"*"):
117 test_support
.unlink(f
)
119 class TestAsciiFileShelve(TestShelveBase
):
122 class TestBinaryFileShelve(TestShelveBase
):
125 class TestProto2FileShelve(TestShelveBase
):
128 class TestAsciiMemShelve(TestShelveBase
):
131 class TestBinaryMemShelve(TestShelveBase
):
134 class TestProto2MemShelve(TestShelveBase
):
139 test_support
.run_unittest(
141 TestBinaryFileShelve
,
142 TestProto2FileShelve
,
149 if __name__
== "__main__":