blob: aab1388d79111e8d090e331ca444121872037a09 [file] [log] [blame]
Skip Montanaro4894a972002-03-18 03:00:37 +00001#! /usr/bin/env python
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00002"""Test script for the dbm.open function based on testdumbdbm.py"""
Skip Montanaro4894a972002-03-18 03:00:37 +00003
4import os
Skip Montanaro4894a972002-03-18 03:00:37 +00005import unittest
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00006import dbm
Skip Montanaro4894a972002-03-18 03:00:37 +00007import glob
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00008import test.support
Skip Montanaro4894a972002-03-18 03:00:37 +00009
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000010_fname = test.support.TESTFN
Gregory P. Smith178fefb2007-08-24 21:59:45 +000011
12#
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000013# Iterates over every database module supported by dbm currently available,
14# setting dbm to use each in turn, and yielding that module
Gregory P. Smith178fefb2007-08-24 21:59:45 +000015#
16def dbm_iterator():
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000017 old_default = dbm._defaultmod
18 for module in dbm._modules.values():
19 dbm._defaultmod = module
Gregory P. Smith178fefb2007-08-24 21:59:45 +000020 yield module
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000021 dbm._defaultmod = old_default
Gregory P. Smith178fefb2007-08-24 21:59:45 +000022
23#
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000024# Clean up all scratch databases we might have created during testing
Gregory P. Smith178fefb2007-08-24 21:59:45 +000025#
26def delete_files():
Skip Montanaro4894a972002-03-18 03:00:37 +000027 # we don't know the precise name the underlying database uses
28 # so we use glob to locate all names
29 for f in glob.glob(_fname + "*"):
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000030 test.support.unlink(f)
31
Skip Montanaro4894a972002-03-18 03:00:37 +000032
33class AnyDBMTestCase(unittest.TestCase):
Martin v. Löwisca8dd912007-08-07 07:15:50 +000034 _dict = {'0': b'',
35 'a': b'Python:',
36 'b': b'Programming',
37 'c': b'the',
38 'd': b'way',
39 'f': b'Guido',
40 'g': b'intended',
Skip Montanaro4894a972002-03-18 03:00:37 +000041 }
42
43 def __init__(self, *args):
44 unittest.TestCase.__init__(self, *args)
45
46 def test_anydbm_creation(self):
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000047 f = dbm.open(_fname, 'c')
Guido van Rossum49dc35b2007-03-08 01:17:51 +000048 self.assertEqual(list(f.keys()), [])
Skip Montanaro4894a972002-03-18 03:00:37 +000049 for key in self._dict:
Martin v. Löwisca8dd912007-08-07 07:15:50 +000050 f[key.encode("ascii")] = self._dict[key]
Skip Montanaro4894a972002-03-18 03:00:37 +000051 self.read_helper(f)
52 f.close()
53
54 def test_anydbm_modification(self):
55 self.init_db()
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000056 f = dbm.open(_fname, 'c')
Martin v. Löwisca8dd912007-08-07 07:15:50 +000057 self._dict['g'] = f[b'g'] = b"indented"
Skip Montanaro4894a972002-03-18 03:00:37 +000058 self.read_helper(f)
59 f.close()
60
61 def test_anydbm_read(self):
62 self.init_db()
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000063 f = dbm.open(_fname, 'r')
Skip Montanaro4894a972002-03-18 03:00:37 +000064 self.read_helper(f)
65 f.close()
66
67 def test_anydbm_keys(self):
68 self.init_db()
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000069 f = dbm.open(_fname, 'r')
Skip Montanaro4894a972002-03-18 03:00:37 +000070 keys = self.keys_helper(f)
71 f.close()
72
Gregory P. Smith178fefb2007-08-24 21:59:45 +000073 def test_anydbm_access(self):
74 self.init_db()
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000075 f = dbm.open(_fname, 'r')
Gregory P. Smith178fefb2007-08-24 21:59:45 +000076 key = "a".encode("ascii")
77 assert(key in f)
78 assert(f[key] == b"Python:")
79 f.close()
80
Skip Montanaro4894a972002-03-18 03:00:37 +000081 def read_helper(self, f):
82 keys = self.keys_helper(f)
83 for key in self._dict:
Martin v. Löwisca8dd912007-08-07 07:15:50 +000084 self.assertEqual(self._dict[key], f[key.encode("ascii")])
Skip Montanaro4894a972002-03-18 03:00:37 +000085
86 def init_db(self):
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000087 f = dbm.open(_fname, 'n')
Skip Montanaro4894a972002-03-18 03:00:37 +000088 for k in self._dict:
Martin v. Löwisca8dd912007-08-07 07:15:50 +000089 f[k.encode("ascii")] = self._dict[k]
Skip Montanaro4894a972002-03-18 03:00:37 +000090 f.close()
91
92 def keys_helper(self, f):
Martin v. Löwisca8dd912007-08-07 07:15:50 +000093 keys = sorted(k.decode("ascii") for k in f.keys())
Guido van Rossumcc2b0162007-02-11 06:12:03 +000094 dkeys = sorted(self._dict.keys())
Skip Montanaro4894a972002-03-18 03:00:37 +000095 self.assertEqual(keys, dkeys)
96 return keys
97
98 def tearDown(self):
Gregory P. Smith178fefb2007-08-24 21:59:45 +000099 delete_files()
Skip Montanaro4894a972002-03-18 03:00:37 +0000100
101 def setUp(self):
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000102 delete_files()
103
Skip Montanaro4894a972002-03-18 03:00:37 +0000104
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000105class WhichDBTestCase(unittest.TestCase):
106 # Actual test methods are added to namespace after class definition.
107 def __init__(self, *args):
108 unittest.TestCase.__init__(self, *args)
109
110 def test_whichdb(self):
111 for module in dbm_iterator():
112 # Check whether whichdb correctly guesses module name
113 # for databases opened with "module" module.
114 # Try with empty files first
115 name = module.__name__
116 if name == 'dbm.dumb':
117 continue # whichdb can't support dbm.dumb
118 test.support.unlink(_fname)
119 f = module.open(_fname, 'c')
120 f.close()
121 self.assertEqual(name, dbm.whichdb(_fname))
122 # Now add a key
123 f = module.open(_fname, 'w')
124 f[b"1"] = b"1"
125 # and test that we can find it
126 self.assertTrue(b"1" in f)
127 # and read it
128 self.assertTrue(f[b"1"] == b"1")
129 f.close()
130 self.assertEqual(name, dbm.whichdb(_fname))
131
132 def tearDown(self):
133 delete_files()
134
135 def setUp(self):
136 delete_files()
137
138
Skip Montanaro4894a972002-03-18 03:00:37 +0000139def test_main():
140 try:
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000141 for module in dbm_iterator():
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000142 test.support.run_unittest(AnyDBMTestCase, WhichDBTestCase)
Skip Montanaro4894a972002-03-18 03:00:37 +0000143 finally:
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000144 delete_files()
Skip Montanaro4894a972002-03-18 03:00:37 +0000145
146if __name__ == "__main__":
147 test_main()