Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(31761)

Side by Side Diff: Lib/test/test_shelve.py

Issue 5178: Add context manager for temporary directory
Patch Set: Created 8 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_commands.py ('k') | Lib/test/test_tempfile.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import os 1 import os
2 import unittest 2 import unittest
3 import shelve 3 import shelve
4 import glob 4 import glob
5 import tempfile
5 from test import test_support 6 from test import test_support
6 7
7 class TestCase(unittest.TestCase): 8 class TestCase(unittest.TestCase):
8 9
9 fn = "shelftemp" + os.extsep + "db" 10 fn = "shelftemp" + os.extsep + "db"
10 11
11 def test_close(self): 12 def test_close(self):
12 d1 = {} 13 d1 = {}
13 s = shelve.Shelf(d1, protocol=2, writeback=False) 14 s = shelve.Shelf(d1, protocol=2, writeback=False)
14 s['key1'] = [1,2,3,4] 15 s['key1'] = [1,2,3,4]
15 self.assertEqual(s['key1'], [1,2,3,4]) 16 self.assertEqual(s['key1'], [1,2,3,4])
16 self.assertEqual(len(s), 1) 17 self.assertEqual(len(s), 1)
17 s.close() 18 s.close()
18 self.assertRaises(ValueError, len, s) 19 self.assertRaises(ValueError, len, s)
19 try: 20 try:
20 s['key1'] 21 s['key1']
21 except ValueError: 22 except ValueError:
22 pass 23 pass
23 else: 24 else:
24 self.fail('Closed shelf should not find a key') 25 self.fail('Closed shelf should not find a key')
25 26
26 def test_ascii_file_shelf(self): 27 def test_ascii_file_shelf(self):
27 try: 28 with tempfile.TemporaryDirectory() as tmpdir:
28 s = shelve.open(self.fn, protocol=0) 29 s = shelve.open(os.path.join(tmpdir, self.fn), protocol=0)
29 s['key1'] = (1,2,3,4) 30 s['key1'] = (1,2,3,4)
30 self.assertEqual(s['key1'], (1,2,3,4)) 31 self.assertEqual(s['key1'], (1,2,3,4))
31 s.close() 32 s.close()
32 finally:
33 for f in glob.glob(self.fn+"*"):
34 os.unlink(f)
35 33
36 def test_binary_file_shelf(self): 34 def test_binary_file_shelf(self):
37 try: 35 with tempfile.TemporaryDirectory() as tmpdir:
38 s = shelve.open(self.fn, protocol=1) 36 s = shelve.open(os.path.join(tmpdir, self.fn), protocol=1)
39 s['key1'] = (1,2,3,4) 37 s['key1'] = (1,2,3,4)
40 self.assertEqual(s['key1'], (1,2,3,4)) 38 self.assertEqual(s['key1'], (1,2,3,4))
41 s.close() 39 s.close()
42 finally:
43 for f in glob.glob(self.fn+"*"):
44 os.unlink(f)
45 40
46 def test_proto2_file_shelf(self): 41 def test_proto2_file_shelf(self):
47 try: 42 with tempfile.TemporaryDirectory() as tmpdir:
48 s = shelve.open(self.fn, protocol=2) 43 s = shelve.open(os.path.join(tmpdir, self.fn), protocol=2)
49 s['key1'] = (1,2,3,4) 44 s['key1'] = (1,2,3,4)
50 self.assertEqual(s['key1'], (1,2,3,4)) 45 self.assertEqual(s['key1'], (1,2,3,4))
51 s.close() 46 s.close()
52 finally:
53 for f in glob.glob(self.fn+"*"):
54 os.unlink(f)
55 47
56 def test_in_memory_shelf(self): 48 def test_in_memory_shelf(self):
57 d1 = {} 49 d1 = {}
58 s = shelve.Shelf(d1, protocol=0) 50 s = shelve.Shelf(d1, protocol=0)
59 s['key1'] = (1,2,3,4) 51 s['key1'] = (1,2,3,4)
60 self.assertEqual(s['key1'], (1,2,3,4)) 52 self.assertEqual(s['key1'], (1,2,3,4))
61 s.close() 53 s.close()
62 d2 = {} 54 d2 = {}
63 s = shelve.Shelf(d2, protocol=1) 55 s = shelve.Shelf(d2, protocol=1)
64 s['key1'] = (1,2,3,4) 56 s['key1'] = (1,2,3,4)
(...skipping 21 matching lines...) Expand all
86 s.close() 78 s.close()
87 79
88 self.assertEqual(len(d1), 1) 80 self.assertEqual(len(d1), 1)
89 self.assertEqual(len(d2), 1) 81 self.assertEqual(len(d2), 1)
90 82
91 83
92 from test import mapping_tests 84 from test import mapping_tests
93 85
94 class TestShelveBase(mapping_tests.BasicTestMappingProtocol): 86 class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
95 fn = "shelftemp.db" 87 fn = "shelftemp.db"
96 counter = 0
97 def __init__(self, *args, **kw): 88 def __init__(self, *args, **kw):
98 self._db = [] 89 self._db = []
90 self._dirnames = []
99 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw) 91 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
100 type2test = shelve.Shelf 92 type2test = shelve.Shelf
101 def _reference(self): 93 def _reference(self):
102 return {"key1":"value1", "key2":2, "key3":(1,2,3)} 94 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
103 def _empty_mapping(self): 95 def _empty_mapping(self):
104 if self._in_mem: 96 if self._in_mem:
105 x= shelve.Shelf({}, **self._args) 97 x= shelve.Shelf({}, **self._args)
106 else: 98 else:
107 self.counter+=1 99 tmpdir = tempfile.mkdtemp()
108 x= shelve.open(self.fn+str(self.counter), **self._args) 100 x= shelve.open(os.path.join(tmpdir, self.fn), **self._args)
101 self._dirnames.append(tmpdir)
109 self._db.append(x) 102 self._db.append(x)
110 return x 103 return x
111 def tearDown(self): 104 def tearDown(self):
112 for db in self._db: 105 for db in self._db:
113 db.close() 106 db.close()
114 self._db = [] 107 self._db = []
115 if not self._in_mem: 108 for tmpdir in self._dirnames:
116 for f in glob.glob(self.fn+"*"): 109 test_support.rmtree(tmpdir)
117 test_support.unlink(f) 110 self._dirnames = []
118 111
119 class TestAsciiFileShelve(TestShelveBase): 112 class TestAsciiFileShelve(TestShelveBase):
120 _args={'protocol':0} 113 _args={'protocol':0}
121 _in_mem = False 114 _in_mem = False
122 class TestBinaryFileShelve(TestShelveBase): 115 class TestBinaryFileShelve(TestShelveBase):
123 _args={'protocol':1} 116 _args={'protocol':1}
124 _in_mem = False 117 _in_mem = False
125 class TestProto2FileShelve(TestShelveBase): 118 class TestProto2FileShelve(TestShelveBase):
126 _args={'protocol':2} 119 _args={'protocol':2}
127 _in_mem = False 120 _in_mem = False
(...skipping 13 matching lines...) Expand all
141 TestBinaryFileShelve, 134 TestBinaryFileShelve,
142 TestProto2FileShelve, 135 TestProto2FileShelve,
143 TestAsciiMemShelve, 136 TestAsciiMemShelve,
144 TestBinaryMemShelve, 137 TestBinaryMemShelve,
145 TestProto2MemShelve, 138 TestProto2MemShelve,
146 TestCase 139 TestCase
147 ) 140 )
148 141
149 if __name__ == "__main__": 142 if __name__ == "__main__":
150 test_main() 143 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_commands.py ('k') | Lib/test/test_tempfile.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+