blob: 041fac171ff2490e9ce6fba67a27512047420a12 [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 unittest
8import dumbdbm
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test import test_support
Guido van Rossum2f1064c2001-01-18 16:46:52 +000010
Guido van Rossum3b0a3292002-08-09 16:38:32 +000011_fname = test_support.TESTFN
Tim Petersd9fbf352002-01-30 07:32:53 +000012
13def _delete_files():
14 for ext in [".dir", ".dat", ".bak"]:
15 try:
16 os.unlink(_fname + ext)
17 except OSError:
18 pass
19
Barry Warsaw3ca656f2001-11-13 20:16:52 +000020class DumbDBMTestCase(unittest.TestCase):
Barry Warsaw3ca656f2001-11-13 20:16:52 +000021 _dict = {'0': '',
22 'a': 'Python:',
23 'b': 'Programming',
24 'c': 'the',
25 'd': 'way',
26 'f': 'Guido',
27 'g': 'intended'
28 }
29
30 def __init__(self, *args):
31 unittest.TestCase.__init__(self, *args)
Tim Peters88768482001-11-13 21:51:26 +000032
Barry Warsaw3ca656f2001-11-13 20:16:52 +000033 def test_dumbdbm_creation(self):
Tim Petersd9fbf352002-01-30 07:32:53 +000034 f = dumbdbm.open(_fname, 'c')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000035 self.assertEqual(f.keys(), [])
36 for key in self._dict:
37 f[key] = self._dict[key]
38 self.read_helper(f)
39 f.close()
40
Andrew M. Kuchlingdc267582006-12-22 15:04:45 +000041 def test_dumbdbm_creation_mode(self):
42 # On platforms without chmod, don't do anything.
Andrew M. Kuchling9ef0ef52006-12-22 15:16:58 +000043 if not (hasattr(os, 'chmod') and hasattr(os, 'umask')):
Andrew M. Kuchlingdc267582006-12-22 15:04:45 +000044 return
45
Andrew M. Kuchling9ef0ef52006-12-22 15:16:58 +000046 try:
47 old_umask = os.umask(0002)
48 f = dumbdbm.open(_fname, 'c', 0637)
49 f.close()
50 finally:
51 os.umask(old_umask)
Tim Petersf733abb2007-01-30 03:03:46 +000052
Neal Norwitz06a9a0c2007-01-18 06:20:55 +000053 expected_mode = 0635
54 if os.name != 'posix':
55 # Windows only supports setting the read-only attribute.
56 # This shouldn't fail, but doesn't work like Unix either.
57 expected_mode = 0666
58
Andrew M. Kuchlingdc267582006-12-22 15:04:45 +000059 import stat
60 st = os.stat(_fname + '.dat')
Neal Norwitz06a9a0c2007-01-18 06:20:55 +000061 self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
Andrew M. Kuchlingdc267582006-12-22 15:04:45 +000062 st = os.stat(_fname + '.dir')
Neal Norwitz06a9a0c2007-01-18 06:20:55 +000063 self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
Tim Petersf733abb2007-01-30 03:03:46 +000064
Tim Peters7a6c7332003-07-13 17:21:10 +000065 def test_close_twice(self):
66 f = dumbdbm.open(_fname)
67 f['a'] = 'b'
68 self.assertEqual(f['a'], 'b')
69 f.close()
70 f.close()
71
Barry Warsaw3ca656f2001-11-13 20:16:52 +000072 def test_dumbdbm_modification(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000073 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000074 f = dumbdbm.open(_fname, 'w')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000075 self._dict['g'] = f['g'] = "indented"
76 self.read_helper(f)
77 f.close()
78
79 def test_dumbdbm_read(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000080 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000081 f = dumbdbm.open(_fname, 'r')
Barry Warsaw3ca656f2001-11-13 20:16:52 +000082 self.read_helper(f)
83 f.close()
84
85 def test_dumbdbm_keys(self):
Skip Montanaroc08fe822002-03-17 23:03:42 +000086 self.init_db()
Tim Petersd9fbf352002-01-30 07:32:53 +000087 f = dumbdbm.open(_fname)
Barry Warsaw3ca656f2001-11-13 20:16:52 +000088 keys = self.keys_helper(f)
89 f.close()
90
Skip Montanaroc08fe822002-03-17 23:03:42 +000091 def test_write_write_read(self):
92 # test for bug #482460
93 f = dumbdbm.open(_fname)
94 f['1'] = 'hello'
95 f['1'] = 'hello2'
96 f.close()
97 f = dumbdbm.open(_fname)
98 self.assertEqual(f['1'], 'hello2')
99 f.close()
100
Andrew M. Kuchlingecdad852005-06-07 19:36:10 +0000101 def test_line_endings(self):
102 # test for bug #1172763: dumbdbm would die if the line endings
103 # weren't what was expected.
104 f = dumbdbm.open(_fname)
105 f['1'] = 'hello'
106 f['2'] = 'hello2'
107 f.close()
108
109 # Mangle the file by adding \r before each newline
110 data = open(_fname + '.dir').read()
111 data = data.replace('\n', '\r\n')
112 open(_fname + '.dir', 'wb').write(data)
Anthony Baxter95a2a4e2005-06-08 04:35:50 +0000113
Andrew M. Kuchlingecdad852005-06-07 19:36:10 +0000114 f = dumbdbm.open(_fname)
115 self.assertEqual(f['1'], 'hello')
116 self.assertEqual(f['2'], 'hello2')
Anthony Baxter95a2a4e2005-06-08 04:35:50 +0000117
118
Barry Warsaw3ca656f2001-11-13 20:16:52 +0000119 def read_helper(self, f):
120 keys = self.keys_helper(f)
121 for key in self._dict:
122 self.assertEqual(self._dict[key], f[key])
Tim Peters88768482001-11-13 21:51:26 +0000123
Skip Montanaroc08fe822002-03-17 23:03:42 +0000124 def init_db(self):
125 f = dumbdbm.open(_fname, 'w')
126 for k in self._dict:
127 f[k] = self._dict[k]
128 f.close()
129
Barry Warsaw3ca656f2001-11-13 20:16:52 +0000130 def keys_helper(self, f):
131 keys = f.keys()
132 keys.sort()
Skip Montanaroc08fe822002-03-17 23:03:42 +0000133 dkeys = self._dict.keys()
134 dkeys.sort()
135 self.assertEqual(keys, dkeys)
Barry Warsaw3ca656f2001-11-13 20:16:52 +0000136 return keys
137
Tim Peters663d1b62003-07-11 04:09:09 +0000138 # Perform randomized operations. This doesn't make assumptions about
139 # what *might* fail.
140 def test_random(self):
141 import random
142 d = {} # mirror the database
143 for dummy in range(5):
144 f = dumbdbm.open(_fname)
145 for dummy in range(100):
146 k = random.choice('abcdefghijklm')
147 if random.random() < 0.2:
148 if k in d:
149 del d[k]
150 del f[k]
151 else:
152 v = random.choice('abc') * random.randrange(10000)
153 d[k] = v
154 f[k] = v
155 self.assertEqual(f[k], v)
156 f.close()
157
158 f = dumbdbm.open(_fname)
159 expected = d.items()
160 expected.sort()
161 got = f.items()
162 got.sort()
163 self.assertEqual(expected, got)
164 f.close()
165
Skip Montanaroc08fe822002-03-17 23:03:42 +0000166 def tearDown(self):
167 _delete_files()
168
169 def setUp(self):
170 _delete_files()
171
Barry Warsaw3ca656f2001-11-13 20:16:52 +0000172def test_main():
Tim Petersd9fbf352002-01-30 07:32:53 +0000173 try:
174 test_support.run_unittest(DumbDBMTestCase)
175 finally:
176 _delete_files()
Barry Warsaw3ca656f2001-11-13 20:16:52 +0000177
178if __name__ == "__main__":
179 test_main()