The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_anydbm.py
blob0cdc2c321d0c911cab464bf4e54e0ab9f0af9118
1 #! /usr/bin/env python
2 """Test script for the anydbm module
3 based on testdumbdbm.py
4 """
6 import os
7 import unittest
8 import anydbm
9 import glob
10 from test import test_support
12 _fname = test_support.TESTFN
14 def _delete_files():
15 # we don't know the precise name the underlying database uses
16 # so we use glob to locate all names
17 for f in glob.glob(_fname + "*"):
18 try:
19 os.unlink(f)
20 except OSError:
21 pass
23 class AnyDBMTestCase(unittest.TestCase):
24 _dict = {'0': '',
25 'a': 'Python:',
26 'b': 'Programming',
27 'c': 'the',
28 'd': 'way',
29 'f': 'Guido',
30 'g': 'intended'
33 def __init__(self, *args):
34 unittest.TestCase.__init__(self, *args)
36 def test_anydbm_creation(self):
37 f = anydbm.open(_fname, 'c')
38 self.assertEqual(f.keys(), [])
39 for key in self._dict:
40 f[key] = self._dict[key]
41 self.read_helper(f)
42 f.close()
44 def test_anydbm_modification(self):
45 self.init_db()
46 f = anydbm.open(_fname, 'c')
47 self._dict['g'] = f['g'] = "indented"
48 self.read_helper(f)
49 f.close()
51 def test_anydbm_read(self):
52 self.init_db()
53 f = anydbm.open(_fname, 'r')
54 self.read_helper(f)
55 f.close()
57 def test_anydbm_keys(self):
58 self.init_db()
59 f = anydbm.open(_fname, 'r')
60 keys = self.keys_helper(f)
61 f.close()
63 def read_helper(self, f):
64 keys = self.keys_helper(f)
65 for key in self._dict:
66 self.assertEqual(self._dict[key], f[key])
68 def init_db(self):
69 f = anydbm.open(_fname, 'n')
70 for k in self._dict:
71 f[k] = self._dict[k]
72 f.close()
74 def keys_helper(self, f):
75 keys = f.keys()
76 keys.sort()
77 dkeys = self._dict.keys()
78 dkeys.sort()
79 self.assertEqual(keys, dkeys)
80 return keys
82 def tearDown(self):
83 _delete_files()
85 def setUp(self):
86 _delete_files()
88 def test_main():
89 try:
90 test_support.run_unittest(AnyDBMTestCase)
91 finally:
92 _delete_files()
94 if __name__ == "__main__":
95 test_main()