1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#! /usr/bin/env python
"""Test script for the dumbdbm module
Original by Roger E. Masse
"""
import os
import unittest
import dumbdbm
from test import test_support
_fname = test_support.TESTFN
def _delete_files():
for ext in [".dir", ".dat", ".bak"]:
try:
os.unlink(_fname + ext)
except OSError:
pass
class DumbDBMTestCase(unittest.TestCase):
_dict = {'0': '',
'a': 'Python:',
'b': 'Programming',
'c': 'the',
'd': 'way',
'f': 'Guido',
'g': 'intended'
}
def __init__(self, *args):
unittest.TestCase.__init__(self, *args)
def test_dumbdbm_creation(self):
f = dumbdbm.open(_fname, 'c')
self.assertEqual(list(f.keys()), [])
for key in self._dict:
f[key] = self._dict[key]
self.read_helper(f)
f.close()
def test_dumbdbm_creation_mode(self):
# On platforms without chmod, don't do anything.
if not (hasattr(os, 'chmod') and hasattr(os, 'umask')):
return
try:
old_umask = os.umask(0002)
f = dumbdbm.open(_fname, 'c', 0637)
f.close()
finally:
os.umask(old_umask)
expected_mode = 0635
if os.name != 'posix':
# Windows only supports setting the read-only attribute.
# This shouldn't fail, but doesn't work like Unix either.
expected_mode = 0666
import stat
st = os.stat(_fname + '.dat')
self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
st = os.stat(_fname + '.dir')
self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
def test_close_twice(self):
f = dumbdbm.open(_fname)
f['a'] = 'b'
self.assertEqual(f['a'], 'b')
f.close()
f.close()
def test_dumbdbm_modification(self):
self.init_db()
f = dumbdbm.open(_fname, 'w')
self._dict['g'] = f['g'] = "indented"
self.read_helper(f)
f.close()
def test_dumbdbm_read(self):
self.init_db()
f = dumbdbm.open(_fname, 'r')
self.read_helper(f)
f.close()
def test_dumbdbm_keys(self):
self.init_db()
f = dumbdbm.open(_fname)
keys = self.keys_helper(f)
f.close()
def test_write_write_read(self):
# test for bug #482460
f = dumbdbm.open(_fname)
f['1'] = 'hello'
f['1'] = 'hello2'
f.close()
f = dumbdbm.open(_fname)
self.assertEqual(f['1'], 'hello2')
f.close()
def test_line_endings(self):
# test for bug #1172763: dumbdbm would die if the line endings
# weren't what was expected.
f = dumbdbm.open(_fname)
f['1'] = 'hello'
f['2'] = 'hello2'
f.close()
# Mangle the file by adding \r before each newline
data = open(_fname + '.dir').read()
data = data.replace('\n', '\r\n')
open(_fname + '.dir', 'wb').write(data)
f = dumbdbm.open(_fname)
self.assertEqual(f['1'], 'hello')
self.assertEqual(f['2'], 'hello2')
def read_helper(self, f):
keys = self.keys_helper(f)
for key in self._dict:
self.assertEqual(self._dict[key], f[key])
def init_db(self):
f = dumbdbm.open(_fname, 'w')
for k in self._dict:
f[k] = self._dict[k]
f.close()
def keys_helper(self, f):
keys = sorted(f.keys())
dkeys = sorted(self._dict.keys())
self.assertEqual(keys, dkeys)
return keys
# Perform randomized operations. This doesn't make assumptions about
# what *might* fail.
def test_random(self):
import random
d = {} # mirror the database
for dummy in range(5):
f = dumbdbm.open(_fname)
for dummy in range(100):
k = random.choice('abcdefghijklm')
if random.random() < 0.2:
if k in d:
del d[k]
del f[k]
else:
v = random.choice('abc') * random.randrange(10000)
d[k] = v
f[k] = v
self.assertEqual(f[k], v)
f.close()
f = dumbdbm.open(_fname)
expected = sorted(d.items())
got = sorted(f.items())
self.assertEqual(expected, got)
f.close()
def tearDown(self):
_delete_files()
def setUp(self):
_delete_files()
def test_main():
try:
test_support.run_unittest(DumbDBMTestCase)
finally:
_delete_files()
if __name__ == "__main__":
test_main()
|