1from test import test_support 2import unittest 3dbm = test_support.import_module('dbm') 4 5class DbmTestCase(unittest.TestCase): 6 7 def setUp(self): 8 self.filename = test_support.TESTFN 9 self.d = dbm.open(self.filename, 'c') 10 self.d.close() 11 12 def tearDown(self): 13 for suffix in ['', '.pag', '.dir', '.db']: 14 test_support.unlink(self.filename + suffix) 15 16 def test_keys(self): 17 self.d = dbm.open(self.filename, 'c') 18 self.assertEqual(self.d.keys(), []) 19 a = [('a', 'b'), ('12345678910', '019237410982340912840198242')] 20 for k, v in a: 21 self.d[k] = v 22 self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a)) 23 for k, v in a: 24 self.assertIn(k, self.d) 25 self.assertEqual(self.d[k], v) 26 self.assertNotIn('xxx', self.d) 27 self.assertRaises(KeyError, lambda: self.d['xxx']) 28 self.d.close() 29 30 def test_modes(self): 31 for mode in ['r', 'rw', 'w', 'n']: 32 try: 33 self.d = dbm.open(self.filename, mode) 34 self.d.close() 35 except dbm.error: 36 self.fail() 37 38def test_main(): 39 test_support.run_unittest(DbmTestCase) 40 41if __name__ == '__main__': 42 test_main() 43