blob: f3675a537d88288023f5b38b0f4b584efb0c68c3 [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 test_support
8import unittest
9import dumbdbm
10import tempfile
Guido van Rossum2f1064c2001-01-18 16:46:52 +000011
Tim Petersd9fbf352002-01-30 07:32:53 +000012_fname = tempfile.mktemp()
13
14def _delete_files():
15 for ext in [".dir", ".dat", ".bak"]:
16 try:
17 os.unlink(_fname + ext)
18 except OSError:
19 pass
20
Barry Warsaw3ca656f2001-11-13 20:16:52 +000021class DumbDBMTestCase(unittest.TestCase):
Barry Warsaw3ca656f2001-11-13 20:16:52 +000022 _dict = {'0': '',
23 'a': 'Python:',
24 'b': 'Programming',
25 'c': 'the',
26 'd': 'way',
27 'f': 'Guido',
28 'g': 'intended'
29 }
30
31 def __init__(self, *args):
32 unittest.TestCase.__init__(self, *args)
Tim Peters88768482001-11-13 21:51:26 +000033
Barry Warsaw3ca656f2001-11-13 20:16:52 +000034 def test_dumbdbm_creation(self):
Tim Petersd9fbf352002-01-30 07:32:53 +000035 f = dumbdbm.open(_fname, 'c')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000036 self.assertEqual(f.keys(), [])
37 for key in self._dict:
38 f[key] = self._dict[key]
39 self.read_helper(f)
40 f.close()
41
42 def test_dumbdbm_modification(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000043 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000044 f = dumbdbm.open(_fname, 'w')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000045 self._dict['g'] = f['g'] = "indented"
46 self.read_helper(f)
47 f.close()
48
49 def test_dumbdbm_read(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000050 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000051 f = dumbdbm.open(_fname, 'r')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000052 self.read_helper(f)
53 f.close()
54
55 def test_dumbdbm_keys(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000056 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000057 f = dumbdbm.open(_fname)
Barry Warsaw3ca656f2001-11-13 20:16:52 +000058 keys = self.keys_helper(f)
59 f.close()
60
Skip Montanaroc08fe822002-03-17 23:03:42 +000061 def test_write_write_read(self):
62 # test for bug #482460
63 f = dumbdbm.open(_fname)
64 f['1'] = 'hello'
65 f['1'] = 'hello2'
66 f.close()
67 f = dumbdbm.open(_fname)
68 self.assertEqual(f['1'], 'hello2')
69 f.close()
70
Barry Warsaw3ca656f2001-11-13 20:16:52 +000071 def read_helper(self, f):
72 keys = self.keys_helper(f)
73 for key in self._dict:
74 self.assertEqual(self._dict[key], f[key])
Tim Peters88768482001-11-13 21:51:26 +000075
Skip Montanaroc08fe822002-03-17 23:03:42 +000076 def init_db(self):
77 f = dumbdbm.open(_fname, 'w')
78 for k in self._dict:
79 f[k] = self._dict[k]
80 f.close()
81
Barry Warsaw3ca656f2001-11-13 20:16:52 +000082 def keys_helper(self, f):
83 keys = f.keys()
84 keys.sort()
Skip Montanaroc08fe822002-03-17 23:03:42 +000085 dkeys = self._dict.keys()
86 dkeys.sort()
87 self.assertEqual(keys, dkeys)
Barry Warsaw3ca656f2001-11-13 20:16:52 +000088 return keys
89
Skip Montanaroc08fe822002-03-17 23:03:42 +000090 def tearDown(self):
91 _delete_files()
92
93 def setUp(self):
94 _delete_files()
95
Barry Warsaw3ca656f2001-11-13 20:16:52 +000096def test_main():
Tim Petersd9fbf352002-01-30 07:32:53 +000097 try:
98 test_support.run_unittest(DumbDBMTestCase)
99 finally:
100 _delete_files()
Barry Warsaw3ca656f2001-11-13 20:16:52 +0000101
102if __name__ == "__main__":
103 test_main()