blob: 02df7e3cab3c1a1224d05052427197a873bc74bc [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Georg Brandl7f986ac2008-05-26 10:30:20 +00002"""Test script for the dbm.open function based on testdumbdbm.py"""
3
4import os
5import unittest
Georg Brandl7f986ac2008-05-26 10:30:20 +00006import glob
7import test.support
8
R. David Murraya21e4ca2009-03-31 23:16:50 +00009# Skip tests if dbm module doesn't exist.
10dbm = test.support.import_module('dbm')
11
Georg Brandl7f986ac2008-05-26 10:30:20 +000012_fname = test.support.TESTFN
13
14#
15# Iterates over every database module supported by dbm currently available,
16# setting dbm to use each in turn, and yielding that module
17#
18def dbm_iterator():
Georg Brandlb17acad2008-05-28 08:43:17 +000019 for name in dbm._names:
20 try:
21 mod = __import__(name, fromlist=['open'])
22 except ImportError:
23 continue
24 dbm._modules[name] = mod
25 yield mod
Georg Brandl7f986ac2008-05-26 10:30:20 +000026
27#
28# Clean up all scratch databases we might have created during testing
29#
30def delete_files():
31 # we don't know the precise name the underlying database uses
32 # so we use glob to locate all names
33 for f in glob.glob(_fname + "*"):
34 test.support.unlink(f)
35
36
37class AnyDBMTestCase(unittest.TestCase):
38 _dict = {'0': b'',
39 'a': b'Python:',
40 'b': b'Programming',
41 'c': b'the',
42 'd': b'way',
43 'f': b'Guido',
44 'g': b'intended',
45 }
46
Georg Brandlb17acad2008-05-28 08:43:17 +000047 def init_db(self):
48 f = dbm.open(_fname, 'n')
49 for k in self._dict:
50 f[k.encode("ascii")] = self._dict[k]
51 f.close()
52
53 def keys_helper(self, f):
54 keys = sorted(k.decode("ascii") for k in f.keys())
55 dkeys = sorted(self._dict.keys())
56 self.assertEqual(keys, dkeys)
57 return keys
58
59 def test_error(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000060 self.assertTrue(issubclass(self.module.error, IOError))
Georg Brandl7f986ac2008-05-26 10:30:20 +000061
Amaury Forgeot d'Arcb5cf3012008-09-25 22:27:43 +000062 def test_anydbm_not_existing(self):
63 self.assertRaises(dbm.error, dbm.open, _fname)
64
Georg Brandl7f986ac2008-05-26 10:30:20 +000065 def test_anydbm_creation(self):
66 f = dbm.open(_fname, 'c')
67 self.assertEqual(list(f.keys()), [])
68 for key in self._dict:
69 f[key.encode("ascii")] = self._dict[key]
70 self.read_helper(f)
71 f.close()
72
briancurtin525c25d2011-03-14 16:03:54 -040073 def test_anydbm_creation_n_file_exists_with_invalid_contents(self):
Victor Stinnerbf816222011-06-30 23:25:47 +020074 # create an empty file
75 test.support.create_empty_file(_fname)
briancurtin525c25d2011-03-14 16:03:54 -040076
77 f = dbm.open(_fname, 'n')
78 self.addCleanup(f.close)
79 self.assertEqual(len(f), 0)
80
Georg Brandl7f986ac2008-05-26 10:30:20 +000081 def test_anydbm_modification(self):
82 self.init_db()
83 f = dbm.open(_fname, 'c')
84 self._dict['g'] = f[b'g'] = b"indented"
85 self.read_helper(f)
86 f.close()
87
88 def test_anydbm_read(self):
89 self.init_db()
90 f = dbm.open(_fname, 'r')
91 self.read_helper(f)
92 f.close()
93
94 def test_anydbm_keys(self):
95 self.init_db()
96 f = dbm.open(_fname, 'r')
97 keys = self.keys_helper(f)
98 f.close()
99
100 def test_anydbm_access(self):
101 self.init_db()
102 f = dbm.open(_fname, 'r')
103 key = "a".encode("ascii")
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000104 self.assertIn(key, f)
Georg Brandl7f986ac2008-05-26 10:30:20 +0000105 assert(f[key] == b"Python:")
106 f.close()
107
108 def read_helper(self, f):
109 keys = self.keys_helper(f)
110 for key in self._dict:
111 self.assertEqual(self._dict[key], f[key.encode("ascii")])
112
Georg Brandl7f986ac2008-05-26 10:30:20 +0000113 def tearDown(self):
114 delete_files()
115
116 def setUp(self):
Georg Brandlb17acad2008-05-28 08:43:17 +0000117 dbm._defaultmod = self.module
Georg Brandl7f986ac2008-05-26 10:30:20 +0000118 delete_files()
119
120
121class WhichDBTestCase(unittest.TestCase):
122 # Actual test methods are added to namespace after class definition.
123 def __init__(self, *args):
124 unittest.TestCase.__init__(self, *args)
125
126 def test_whichdb(self):
127 for module in dbm_iterator():
128 # Check whether whichdb correctly guesses module name
129 # for databases opened with "module" module.
130 # Try with empty files first
131 name = module.__name__
132 if name == 'dbm.dumb':
133 continue # whichdb can't support dbm.dumb
Senthil Kumaran725c2b92011-02-26 07:11:27 +0000134 delete_files()
Georg Brandl7f986ac2008-05-26 10:30:20 +0000135 f = module.open(_fname, 'c')
136 f.close()
137 self.assertEqual(name, dbm.whichdb(_fname))
138 # Now add a key
139 f = module.open(_fname, 'w')
140 f[b"1"] = b"1"
141 # and test that we can find it
Benjamin Peterson577473f2010-01-19 00:09:57 +0000142 self.assertIn(b"1", f)
Georg Brandl7f986ac2008-05-26 10:30:20 +0000143 # and read it
144 self.assertTrue(f[b"1"] == b"1")
145 f.close()
146 self.assertEqual(name, dbm.whichdb(_fname))
147
148 def tearDown(self):
149 delete_files()
150
151 def setUp(self):
152 delete_files()
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000153 self.filename = test.support.TESTFN
154 self.d = dbm.open(self.filename, 'c')
155 self.d.close()
156
157 def test_keys(self):
158 self.d = dbm.open(self.filename, 'c')
159 self.assertEqual(self.d.keys(), [])
160 a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')]
161 for k, v in a:
162 self.d[k] = v
163 self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a))
164 for k, v in a:
Benjamin Peterson577473f2010-01-19 00:09:57 +0000165 self.assertIn(k, self.d)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000166 self.assertEqual(self.d[k], v)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000167 self.assertNotIn(b'xxx', self.d)
Benjamin Peterson1bc15c22008-10-11 02:19:18 +0000168 self.assertRaises(KeyError, lambda: self.d[b'xxx'])
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000169 self.d.close()
Georg Brandl7f986ac2008-05-26 10:30:20 +0000170
171
172def test_main():
Georg Brandlb17acad2008-05-28 08:43:17 +0000173 classes = [WhichDBTestCase]
174 for mod in dbm_iterator():
175 classes.append(type("TestCase-" + mod.__name__, (AnyDBMTestCase,),
176 {'module': mod}))
177 test.support.run_unittest(*classes)
Georg Brandl7f986ac2008-05-26 10:30:20 +0000178
179if __name__ == "__main__":
180 test_main()