blob: 51b62172e3d602301737d2457ee15aef6aa67d68 [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
Barry Warsaw3ca656f2001-11-13 20:16:52 +000012class DumbDBMTestCase(unittest.TestCase):
13 _fname = tempfile.mktemp()
14 _dict = {'0': '',
15 'a': 'Python:',
16 'b': 'Programming',
17 'c': 'the',
18 'd': 'way',
19 'f': 'Guido',
20 'g': 'intended'
21 }
22
23 def __init__(self, *args):
24 unittest.TestCase.__init__(self, *args)
25 self._dkeys = self._dict.keys()
26 self._dkeys.sort()
27
28 def test_dumbdbm_creation(self):
29 for ext in [".dir", ".dat", ".bak"]:
30 try: os.unlink(self._fname+ext)
31 except OSError: pass
32
33 f = dumbdbm.open(self._fname, 'c')
34 self.assertEqual(f.keys(), [])
35 for key in self._dict:
36 f[key] = self._dict[key]
37 self.read_helper(f)
38 f.close()
39
40 def test_dumbdbm_modification(self):
41 f = dumbdbm.open(self._fname, 'w')
42 self._dict['g'] = f['g'] = "indented"
43 self.read_helper(f)
44 f.close()
45
46 def test_dumbdbm_read(self):
47 f = dumbdbm.open(self._fname, 'r')
48 self.read_helper(f)
49 f.close()
50
51 def test_dumbdbm_keys(self):
52 f = dumbdbm.open(self._fname)
53 keys = self.keys_helper(f)
54 f.close()
55
56 def read_helper(self, f):
57 keys = self.keys_helper(f)
58 for key in self._dict:
59 self.assertEqual(self._dict[key], f[key])
60
61 def keys_helper(self, f):
62 keys = f.keys()
63 keys.sort()
64 self.assertEqual(keys, self._dkeys)
65 return keys
66
67def test_main():
68 test_support.run_unittest(DumbDBMTestCase)
69
70
71if __name__ == "__main__":
72 test_main()