2 """Test script for the dumbdbm module
3 Original by Roger E. Masse
9 from test
import test_support
11 _fname
= test_support
.TESTFN
14 for ext
in [".dir", ".dat", ".bak"]:
16 os
.unlink(_fname
+ ext
)
20 class DumbDBMTestCase(unittest
.TestCase
):
30 def __init__(self
, *args
):
31 unittest
.TestCase
.__init
__(self
, *args
)
33 def test_dumbdbm_creation(self
):
34 f
= dumbdbm
.open(_fname
, 'c')
35 self
.assertEqual(f
.keys(), [])
36 for key
in self
._dict
:
37 f
[key
] = self
._dict
[key
]
41 def test_dumbdbm_creation_mode(self
):
42 # On platforms without chmod, don't do anything.
43 if not (hasattr(os
, 'chmod') and hasattr(os
, 'umask')):
47 old_umask
= os
.umask(0002)
48 f
= dumbdbm
.open(_fname
, 'c', 0637)
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.
60 st
= os
.stat(_fname
+ '.dat')
61 self
.assertEqual(stat
.S_IMODE(st
.st_mode
), expected_mode
)
62 st
= os
.stat(_fname
+ '.dir')
63 self
.assertEqual(stat
.S_IMODE(st
.st_mode
), expected_mode
)
65 def test_close_twice(self
):
66 f
= dumbdbm
.open(_fname
)
68 self
.assertEqual(f
['a'], 'b')
72 def test_dumbdbm_modification(self
):
74 f
= dumbdbm
.open(_fname
, 'w')
75 self
._dict
['g'] = f
['g'] = "indented"
79 def test_dumbdbm_read(self
):
81 f
= dumbdbm
.open(_fname
, 'r')
85 def test_dumbdbm_keys(self
):
87 f
= dumbdbm
.open(_fname
)
88 keys
= self
.keys_helper(f
)
91 def test_write_write_read(self
):
92 # test for bug #482460
93 f
= dumbdbm
.open(_fname
)
97 f
= dumbdbm
.open(_fname
)
98 self
.assertEqual(f
['1'], 'hello2')
101 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
)
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
)
114 f
= dumbdbm
.open(_fname
)
115 self
.assertEqual(f
['1'], 'hello')
116 self
.assertEqual(f
['2'], 'hello2')
119 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
])
125 f
= dumbdbm
.open(_fname
, 'w')
130 def keys_helper(self
, f
):
133 dkeys
= self
._dict
.keys()
135 self
.assertEqual(keys
, dkeys
)
138 # Perform randomized operations. This doesn't make assumptions about
140 def test_random(self
):
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:
152 v
= random
.choice('abc') * random
.randrange(10000)
155 self
.assertEqual(f
[k
], v
)
158 f
= dumbdbm
.open(_fname
)
163 self
.assertEqual(expected
, got
)
174 test_support
.run_unittest(DumbDBMTestCase
)
178 if __name__
== "__main__":