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

Delta Between Two Patch Sets: Lib/test/test_dbm.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right Patch Set: Created 5 years, 6 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_dbm_gnu.py ('k') | Lib/test/test_epoll.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #! /usr/bin/env python3
2 """Test script for the dbm.open function based on testdumbdbm.py""" 1 """Test script for the dbm.open function based on testdumbdbm.py"""
3 2
4 import os 3 import os
5 import unittest 4 import unittest
6 import glob 5 import glob
7 import test.support 6 import test.support
8 7
9 # Skip tests if dbm module doesn't exist. 8 # Skip tests if dbm module doesn't exist.
10 dbm = test.support.import_module('dbm') 9 dbm = test.support.import_module('dbm')
10
11 try:
12 from dbm import ndbm
13 except ImportError:
14 ndbm = None
11 15
12 _fname = test.support.TESTFN 16 _fname = test.support.TESTFN
13 17
14 # 18 #
15 # Iterates over every database module supported by dbm currently available, 19 # Iterates over every database module supported by dbm currently available,
16 # setting dbm to use each in turn, and yielding that module 20 # setting dbm to use each in turn, and yielding that module
17 # 21 #
18 def dbm_iterator(): 22 def dbm_iterator():
19 for name in dbm._names: 23 for name in dbm._names:
20 try: 24 try:
21 mod = __import__(name, fromlist=['open']) 25 mod = __import__(name, fromlist=['open'])
22 except ImportError: 26 except ImportError:
23 continue 27 continue
24 dbm._modules[name] = mod 28 dbm._modules[name] = mod
25 yield mod 29 yield mod
26 30
27 # 31 #
28 # Clean up all scratch databases we might have created during testing 32 # Clean up all scratch databases we might have created during testing
29 # 33 #
30 def delete_files(): 34 def delete_files():
31 # we don't know the precise name the underlying database uses 35 # we don't know the precise name the underlying database uses
32 # so we use glob to locate all names 36 # so we use glob to locate all names
33 for f in glob.glob(_fname + "*"): 37 for f in glob.glob(_fname + "*"):
34 test.support.unlink(f) 38 test.support.unlink(f)
35 39
36 40
37 class AnyDBMTestCase(unittest.TestCase): 41 class AnyDBMTestCase:
38 _dict = {'0': b'', 42 _dict = {'0': b'',
39 'a': b'Python:', 43 'a': b'Python:',
40 'b': b'Programming', 44 'b': b'Programming',
41 'c': b'the', 45 'c': b'the',
42 'd': b'way', 46 'd': b'way',
43 'f': b'Guido', 47 'f': b'Guido',
44 'g': b'intended', 48 'g': b'intended',
45 } 49 }
46 50
47 def init_db(self): 51 def init_db(self):
48 f = dbm.open(_fname, 'n') 52 f = dbm.open(_fname, 'n')
49 for k in self._dict: 53 for k in self._dict:
50 f[k.encode("ascii")] = self._dict[k] 54 f[k.encode("ascii")] = self._dict[k]
51 f.close() 55 f.close()
52 56
53 def keys_helper(self, f): 57 def keys_helper(self, f):
54 keys = sorted(k.decode("ascii") for k in f.keys()) 58 keys = sorted(k.decode("ascii") for k in f.keys())
55 dkeys = sorted(self._dict.keys()) 59 dkeys = sorted(self._dict.keys())
56 self.assertEqual(keys, dkeys) 60 self.assertEqual(keys, dkeys)
57 return keys 61 return keys
58 62
59 def test_error(self): 63 def test_error(self):
60 self.assertTrue(issubclass(self.module.error, IOError)) 64 self.assertTrue(issubclass(self.module.error, OSError))
61 65
62 def test_anydbm_not_existing(self): 66 def test_anydbm_not_existing(self):
63 self.assertRaises(dbm.error, dbm.open, _fname) 67 self.assertRaises(dbm.error, dbm.open, _fname)
64 68
65 def test_anydbm_creation(self): 69 def test_anydbm_creation(self):
66 f = dbm.open(_fname, 'c') 70 f = dbm.open(_fname, 'c')
67 self.assertEqual(list(f.keys()), []) 71 self.assertEqual(list(f.keys()), [])
68 for key in self._dict: 72 for key in self._dict:
69 f[key.encode("ascii")] = self._dict[key] 73 f[key.encode("ascii")] = self._dict[key]
70 self.read_helper(f) 74 self.read_helper(f)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 116
113 def tearDown(self): 117 def tearDown(self):
114 delete_files() 118 delete_files()
115 119
116 def setUp(self): 120 def setUp(self):
117 dbm._defaultmod = self.module 121 dbm._defaultmod = self.module
118 delete_files() 122 delete_files()
119 123
120 124
121 class WhichDBTestCase(unittest.TestCase): 125 class WhichDBTestCase(unittest.TestCase):
122 # Actual test methods are added to namespace after class definition.
123 def __init__(self, *args):
124 unittest.TestCase.__init__(self, *args)
125
126 def test_whichdb(self): 126 def test_whichdb(self):
127 for module in dbm_iterator(): 127 for module in dbm_iterator():
128 # Check whether whichdb correctly guesses module name 128 # Check whether whichdb correctly guesses module name
129 # for databases opened with "module" module. 129 # for databases opened with "module" module.
130 # Try with empty files first 130 # Try with empty files first
131 name = module.__name__ 131 name = module.__name__
132 if name == 'dbm.dumb': 132 if name == 'dbm.dumb':
133 continue # whichdb can't support dbm.dumb 133 continue # whichdb can't support dbm.dumb
134 delete_files() 134 delete_files()
135 f = module.open(_fname, 'c') 135 f = module.open(_fname, 'c')
136 f.close() 136 f.close()
137 self.assertEqual(name, dbm.whichdb(_fname)) 137 self.assertEqual(name, self.dbm.whichdb(_fname))
138 # Now add a key 138 # Now add a key
139 f = module.open(_fname, 'w') 139 f = module.open(_fname, 'w')
140 f[b"1"] = b"1" 140 f[b"1"] = b"1"
141 # and test that we can find it 141 # and test that we can find it
142 self.assertIn(b"1", f) 142 self.assertIn(b"1", f)
143 # and read it 143 # and read it
144 self.assertEqual(f[b"1"], b"1") 144 self.assertEqual(f[b"1"], b"1")
145 f.close() 145 f.close()
146 self.assertEqual(name, dbm.whichdb(_fname)) 146 self.assertEqual(name, self.dbm.whichdb(_fname))
147
148 @unittest.skipUnless(ndbm, reason='Test requires ndbm')
149 def test_whichdb_ndbm(self):
150 # Issue 17198: check that ndbm which is referenced in whichdb is defined
151 db_file = '{}_ndbm.db'.format(_fname)
152 with open(db_file, 'w'):
153 self.addCleanup(test.support.unlink, db_file)
154 self.assertIsNone(self.dbm.whichdb(db_file[:-3]))
147 155
148 def tearDown(self): 156 def tearDown(self):
149 delete_files() 157 delete_files()
150 158
151 def setUp(self): 159 def setUp(self):
152 delete_files() 160 delete_files()
153 self.filename = test.support.TESTFN 161 self.filename = test.support.TESTFN
154 self.d = dbm.open(self.filename, 'c') 162 self.d = dbm.open(self.filename, 'c')
155 self.d.close() 163 self.d.close()
164 self.dbm = test.support.import_fresh_module('dbm')
156 165
157 def test_keys(self): 166 def test_keys(self):
158 self.d = dbm.open(self.filename, 'c') 167 self.d = dbm.open(self.filename, 'c')
159 self.assertEqual(self.d.keys(), []) 168 self.assertEqual(self.d.keys(), [])
160 a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')] 169 a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')]
161 for k, v in a: 170 for k, v in a:
162 self.d[k] = v 171 self.d[k] = v
163 self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a)) 172 self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a))
164 for k, v in a: 173 for k, v in a:
165 self.assertIn(k, self.d) 174 self.assertIn(k, self.d)
166 self.assertEqual(self.d[k], v) 175 self.assertEqual(self.d[k], v)
167 self.assertNotIn(b'xxx', self.d) 176 self.assertNotIn(b'xxx', self.d)
168 self.assertRaises(KeyError, lambda: self.d[b'xxx']) 177 self.assertRaises(KeyError, lambda: self.d[b'xxx'])
169 self.d.close() 178 self.d.close()
170 179
171 180
172 def test_main(): 181 def load_tests(loader, tests, pattern):
173 classes = [WhichDBTestCase] 182 classes = []
174 for mod in dbm_iterator(): 183 for mod in dbm_iterator():
175 classes.append(type("TestCase-" + mod.__name__, (AnyDBMTestCase,), 184 classes.append(type("TestCase-" + mod.__name__,
185 (AnyDBMTestCase, unittest.TestCase),
176 {'module': mod})) 186 {'module': mod}))
177 test.support.run_unittest(*classes) 187 suites = [unittest.makeSuite(c) for c in classes]
188
189 tests.addTests(suites)
190 return tests
178 191
179 if __name__ == "__main__": 192 if __name__ == "__main__":
180 test_main() 193 unittest.main()
LEFTRIGHT

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