blob: 4d1bc0e5ed56e574a9a50768d10a1f762f9cb7e3 [file] [log] [blame]
Guido van Rossum2f1064c2001-01-18 16:46:52 +00001#! /usr/bin/env python
2"""Test script for the dumbdbm module
3 Original by Roger E. Masse
4"""
5
Guido van Rossum2f1064c2001-01-18 16:46:52 +00006import os
Barry Warsaw3ca656f2001-11-13 20:16:52 +00007import unittest
8import dumbdbm
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test import test_support
Guido van Rossum2f1064c2001-01-18 16:46:52 +000010
Guido van Rossum3b0a3292002-08-09 16:38:32 +000011_fname = test_support.TESTFN
Tim Petersd9fbf352002-01-30 07:32:53 +000012
13def _delete_files():
14 for ext in [".dir", ".dat", ".bak"]:
15 try:
16 os.unlink(_fname + ext)
17 except OSError:
18 pass
19
Barry Warsaw3ca656f2001-11-13 20:16:52 +000020class DumbDBMTestCase(unittest.TestCase):
Barry Warsaw3ca656f2001-11-13 20:16:52 +000021 _dict = {'0': '',
22 'a': 'Python:',
23 'b': 'Programming',
24 'c': 'the',
25 'd': 'way',
26 'f': 'Guido',
27 'g': 'intended'
28 }
29
30 def __init__(self, *args):
31 unittest.TestCase.__init__(self, *args)
Tim Peters88768482001-11-13 21:51:26 +000032
Barry Warsaw3ca656f2001-11-13 20:16:52 +000033 def test_dumbdbm_creation(self):
Tim Petersd9fbf352002-01-30 07:32:53 +000034 f = dumbdbm.open(_fname, 'c')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000035 self.assertEqual(f.keys(), [])
36 for key in self._dict:
37 f[key] = self._dict[key]
38 self.read_helper(f)
39 f.close()
40
41 def test_dumbdbm_modification(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000042 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000043 f = dumbdbm.open(_fname, 'w')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000044 self._dict['g'] = f['g'] = "indented"
45 self.read_helper(f)
46 f.close()
47
48 def test_dumbdbm_read(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000049 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000050 f = dumbdbm.open(_fname, 'r')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000051 self.read_helper(f)
52 f.close()
53
54 def test_dumbdbm_keys(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000055 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000056 f = dumbdbm.open(_fname)
Barry Warsaw3ca656f2001-11-13 20:16:52 +000057 keys = self.keys_helper(f)
58 f.close()
59
Skip Montanaroc08fe822002-03-17 23:03:42 +000060 def test_write_write_read(self):
61 # test for bug #482460
62 f = dumbdbm.open(_fname)
63 f['1'] = 'hello'
64 f['1'] = 'hello2'
65 f.close()
66 f = dumbdbm.open(_fname)
67 self.assertEqual(f['1'], 'hello2')
68 f.close()
69
Barry Warsaw3ca656f2001-11-13 20:16:52 +000070 def read_helper(self, f):
71 keys = self.keys_helper(f)
72 for key in self._dict:
73 self.assertEqual(self._dict[key], f[key])
Tim Peters88768482001-11-13 21:51:26 +000074
Skip Montanaroc08fe822002-03-17 23:03:42 +000075 def init_db(self):
76 f = dumbdbm.open(_fname, 'w')
77 for k in self._dict:
78 f[k] = self._dict[k]
79 f.close()
80
Barry Warsaw3ca656f2001-11-13 20:16:52 +000081 def keys_helper(self, f):
82 keys = f.keys()
83 keys.sort()
Skip Montanaroc08fe822002-03-17 23:03:42 +000084 dkeys = self._dict.keys()
85 dkeys.sort()
86 self.assertEqual(keys, dkeys)
Barry Warsaw3ca656f2001-11-13 20:16:52 +000087 return keys
88
Skip Montanaroc08fe822002-03-17 23:03:42 +000089 def tearDown(self):
90 _delete_files()
91
92 def setUp(self):
93 _delete_files()
94
Barry Warsaw3ca656f2001-11-13 20:16:52 +000095def test_main():
Tim Petersd9fbf352002-01-30 07:32:53 +000096 try:
97 test_support.run_unittest(DumbDBMTestCase)
98 finally:
99 _delete_files()
Barry Warsaw3ca656f2001-11-13 20:16:52 +0000100
101if __name__ == "__main__":
102 test_main()