2001-01-18 12:46:52 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for the dumbdbm module
|
|
|
|
Original by Roger E. Masse
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2001-11-13 16:16:52 -04:00
|
|
|
import unittest
|
|
|
|
import dumbdbm
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-11-13 16:16:52 -04:00
|
|
|
|
2002-08-09 13:38:32 -03:00
|
|
|
_fname = test_support.TESTFN
|
2002-01-30 03:32:53 -04:00
|
|
|
|
|
|
|
def _delete_files():
|
|
|
|
for ext in [".dir", ".dat", ".bak"]:
|
|
|
|
try:
|
|
|
|
os.unlink(_fname + ext)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2001-11-13 16:16:52 -04:00
|
|
|
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)
|
2001-11-13 17:51:26 -04:00
|
|
|
|
2001-11-13 16:16:52 -04:00
|
|
|
def test_dumbdbm_creation(self):
|
2002-01-30 03:32:53 -04:00
|
|
|
f = dumbdbm.open(_fname, 'c')
|
2001-11-13 16:16:52 -04:00
|
|
|
self.assertEqual(f.keys(), [])
|
|
|
|
for key in self._dict:
|
|
|
|
f[key] = self._dict[key]
|
|
|
|
self.read_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
2006-12-22 11:04:45 -04:00
|
|
|
def test_dumbdbm_creation_mode(self):
|
|
|
|
# On platforms without chmod, don't do anything.
|
2006-12-22 11:16:58 -04:00
|
|
|
if not (hasattr(os, 'chmod') and hasattr(os, 'umask')):
|
2006-12-22 11:04:45 -04:00
|
|
|
return
|
|
|
|
|
2006-12-22 11:16:58 -04:00
|
|
|
try:
|
|
|
|
old_umask = os.umask(0002)
|
|
|
|
f = dumbdbm.open(_fname, 'c', 0637)
|
|
|
|
f.close()
|
|
|
|
finally:
|
|
|
|
os.umask(old_umask)
|
2007-01-29 23:03:46 -04:00
|
|
|
|
2007-01-18 02:20:55 -04:00
|
|
|
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
|
|
|
|
|
2006-12-22 11:04:45 -04:00
|
|
|
import stat
|
|
|
|
st = os.stat(_fname + '.dat')
|
2007-01-18 02:20:55 -04:00
|
|
|
self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
|
2006-12-22 11:04:45 -04:00
|
|
|
st = os.stat(_fname + '.dir')
|
2007-01-18 02:20:55 -04:00
|
|
|
self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
|
2007-01-29 23:03:46 -04:00
|
|
|
|
2003-07-13 14:21:10 -03:00
|
|
|
def test_close_twice(self):
|
|
|
|
f = dumbdbm.open(_fname)
|
|
|
|
f['a'] = 'b'
|
|
|
|
self.assertEqual(f['a'], 'b')
|
|
|
|
f.close()
|
|
|
|
f.close()
|
|
|
|
|
2001-11-13 16:16:52 -04:00
|
|
|
def test_dumbdbm_modification(self):
|
2002-03-17 19:03:42 -04:00
|
|
|
self.init_db()
|
2002-01-30 03:32:53 -04:00
|
|
|
f = dumbdbm.open(_fname, 'w')
|
2001-11-13 16:16:52 -04:00
|
|
|
self._dict['g'] = f['g'] = "indented"
|
|
|
|
self.read_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_dumbdbm_read(self):
|
2002-03-17 19:03:42 -04:00
|
|
|
self.init_db()
|
2002-01-30 03:32:53 -04:00
|
|
|
f = dumbdbm.open(_fname, 'r')
|
2001-11-13 16:16:52 -04:00
|
|
|
self.read_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_dumbdbm_keys(self):
|
2002-03-17 19:03:42 -04:00
|
|
|
self.init_db()
|
2002-01-30 03:32:53 -04:00
|
|
|
f = dumbdbm.open(_fname)
|
2001-11-13 16:16:52 -04:00
|
|
|
keys = self.keys_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
2002-03-17 19:03:42 -04:00
|
|
|
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()
|
|
|
|
|
2005-06-07 16:36:10 -03:00
|
|
|
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)
|
2005-06-08 01:35:50 -03:00
|
|
|
|
2005-06-07 16:36:10 -03:00
|
|
|
f = dumbdbm.open(_fname)
|
|
|
|
self.assertEqual(f['1'], 'hello')
|
|
|
|
self.assertEqual(f['2'], 'hello2')
|
2005-06-08 01:35:50 -03:00
|
|
|
|
|
|
|
|
2001-11-13 16:16:52 -04:00
|
|
|
def read_helper(self, f):
|
|
|
|
keys = self.keys_helper(f)
|
|
|
|
for key in self._dict:
|
|
|
|
self.assertEqual(self._dict[key], f[key])
|
2001-11-13 17:51:26 -04:00
|
|
|
|
2002-03-17 19:03:42 -04:00
|
|
|
def init_db(self):
|
|
|
|
f = dumbdbm.open(_fname, 'w')
|
|
|
|
for k in self._dict:
|
|
|
|
f[k] = self._dict[k]
|
|
|
|
f.close()
|
|
|
|
|
2001-11-13 16:16:52 -04:00
|
|
|
def keys_helper(self, f):
|
|
|
|
keys = f.keys()
|
|
|
|
keys.sort()
|
2002-03-17 19:03:42 -04:00
|
|
|
dkeys = self._dict.keys()
|
|
|
|
dkeys.sort()
|
|
|
|
self.assertEqual(keys, dkeys)
|
2001-11-13 16:16:52 -04:00
|
|
|
return keys
|
|
|
|
|
2003-07-11 01:09:09 -03:00
|
|
|
# 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 = d.items()
|
|
|
|
expected.sort()
|
|
|
|
got = f.items()
|
|
|
|
got.sort()
|
|
|
|
self.assertEqual(expected, got)
|
|
|
|
f.close()
|
|
|
|
|
2002-03-17 19:03:42 -04:00
|
|
|
def tearDown(self):
|
|
|
|
_delete_files()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
_delete_files()
|
|
|
|
|
2001-11-13 16:16:52 -04:00
|
|
|
def test_main():
|
2002-01-30 03:32:53 -04:00
|
|
|
try:
|
|
|
|
test_support.run_unittest(DumbDBMTestCase)
|
|
|
|
finally:
|
|
|
|
_delete_files()
|
2001-11-13 16:16:52 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|