Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 1 | import os |
| 2 | import unittest |
| 3 | import shelve |
| 4 | import glob |
| 5 | from test import test_support |
| 6 | |
| 7 | class TestCase(unittest.TestCase): |
| 8 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 9 | fn = "shelftemp" + os.extsep + "db" |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 10 | |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 11 | def test_ascii_file_shelf(self): |
| 12 | try: |
| 13 | s = shelve.open(self.fn, binary=False) |
| 14 | s['key1'] = (1,2,3,4) |
| 15 | self.assertEqual(s['key1'], (1,2,3,4)) |
| 16 | s.close() |
| 17 | finally: |
| 18 | for f in glob.glob(self.fn+"*"): |
| 19 | os.unlink(f) |
| 20 | |
| 21 | def test_binary_file_shelf(self): |
| 22 | try: |
| 23 | s = shelve.open(self.fn, binary=True) |
| 24 | s['key1'] = (1,2,3,4) |
| 25 | self.assertEqual(s['key1'], (1,2,3,4)) |
| 26 | s.close() |
| 27 | finally: |
| 28 | for f in glob.glob(self.fn+"*"): |
| 29 | os.unlink(f) |
| 30 | |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 31 | def test_proto2_file_shelf(self): |
| 32 | try: |
| 33 | s = shelve.open(self.fn, protocol=2) |
| 34 | s['key1'] = (1,2,3,4) |
| 35 | self.assertEqual(s['key1'], (1,2,3,4)) |
| 36 | s.close() |
| 37 | finally: |
| 38 | for f in glob.glob(self.fn+"*"): |
| 39 | os.unlink(f) |
| 40 | |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 41 | def test_in_memory_shelf(self): |
| 42 | d1 = {} |
| 43 | s = shelve.Shelf(d1, binary=False) |
| 44 | s['key1'] = (1,2,3,4) |
| 45 | self.assertEqual(s['key1'], (1,2,3,4)) |
| 46 | s.close() |
| 47 | d2 = {} |
| 48 | s = shelve.Shelf(d2, binary=True) |
| 49 | s['key1'] = (1,2,3,4) |
| 50 | self.assertEqual(s['key1'], (1,2,3,4)) |
| 51 | s.close() |
| 52 | |
| 53 | self.assertEqual(len(d1), 1) |
| 54 | self.assertNotEqual(d1, d2) |
| 55 | |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 56 | def test_mutable_entry(self): |
| 57 | d1 = {} |
| 58 | s = shelve.Shelf(d1, protocol=2, writeback=False) |
| 59 | s['key1'] = [1,2,3,4] |
| 60 | self.assertEqual(s['key1'], [1,2,3,4]) |
| 61 | s['key1'].append(5) |
| 62 | self.assertEqual(s['key1'], [1,2,3,4]) |
| 63 | s.close() |
| 64 | |
| 65 | d2 = {} |
| 66 | s = shelve.Shelf(d2, protocol=2, writeback=True) |
| 67 | s['key1'] = [1,2,3,4] |
| 68 | self.assertEqual(s['key1'], [1,2,3,4]) |
| 69 | s['key1'].append(5) |
| 70 | self.assertEqual(s['key1'], [1,2,3,4,5]) |
| 71 | s.close() |
| 72 | |
| 73 | self.assertEqual(len(d1), 1) |
| 74 | self.assertEqual(len(d2), 1) |
| 75 | |
| 76 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 77 | from test_userdict import TestMappingProtocol |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 78 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 79 | class TestShelveBase(TestMappingProtocol): |
| 80 | fn = "shelftemp.db" |
| 81 | counter = 0 |
| 82 | def __init__(self, *args, **kw): |
| 83 | self._db = [] |
| 84 | TestMappingProtocol.__init__(self, *args, **kw) |
| 85 | _tested_class = shelve.Shelf |
| 86 | def _reference(self): |
| 87 | return {"key1":"value1", "key2":2, "key3":(1,2,3)} |
| 88 | def _empty_mapping(self): |
| 89 | if self._in_mem: |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 90 | x= shelve.Shelf({}, **self._args) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 91 | else: |
| 92 | self.counter+=1 |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 93 | x= shelve.open(self.fn+str(self.counter), **self._args) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 94 | self._db.append(x) |
| 95 | return x |
| 96 | def tearDown(self): |
| 97 | for db in self._db: |
| 98 | db.close() |
| 99 | self._db = [] |
| 100 | if not self._in_mem: |
| 101 | for f in glob.glob(self.fn+"*"): |
| 102 | os.unlink(f) |
| 103 | |
| 104 | class TestAsciiFileShelve(TestShelveBase): |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 105 | _args={'binary':False} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 106 | _in_mem = False |
| 107 | class TestBinaryFileShelve(TestShelveBase): |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 108 | _args={'binary':True} |
| 109 | _in_mem = False |
| 110 | class TestProto2FileShelve(TestShelveBase): |
| 111 | _args={'protocol':2} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 112 | _in_mem = False |
| 113 | class TestAsciiMemShelve(TestShelveBase): |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 114 | _args={'binary':False} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 115 | _in_mem = True |
| 116 | class TestBinaryMemShelve(TestShelveBase): |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 117 | _args={'binary':True} |
| 118 | _in_mem = True |
| 119 | class TestProto2MemShelve(TestShelveBase): |
| 120 | _args={'protocol':2} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 121 | _in_mem = True |
| 122 | |
| 123 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 124 | test_support.run_unittest( |
| 125 | TestAsciiFileShelve, |
| 126 | TestBinaryFileShelve, |
| 127 | TestProto2FileShelve, |
| 128 | TestAsciiMemShelve, |
| 129 | TestBinaryMemShelve, |
| 130 | TestProto2MemShelve, |
| 131 | TestCase |
| 132 | ) |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 133 | |
| 134 | if __name__ == "__main__": |
| 135 | test_main() |