2002-03-17 23:00:37 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for the anydbm module
|
|
|
|
based on testdumbdbm.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import anydbm
|
|
|
|
import glob
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2002-03-17 23:00:37 -04:00
|
|
|
|
2002-08-09 13:38:32 -03:00
|
|
|
_fname = test_support.TESTFN
|
2002-03-17 23:00:37 -04:00
|
|
|
|
|
|
|
def _delete_files():
|
|
|
|
# we don't know the precise name the underlying database uses
|
|
|
|
# so we use glob to locate all names
|
|
|
|
for f in glob.glob(_fname + "*"):
|
|
|
|
try:
|
|
|
|
os.unlink(f)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AnyDBMTestCase(unittest.TestCase):
|
2007-05-23 17:51:02 -03:00
|
|
|
_dict = {'0': b'',
|
|
|
|
'a': b'Python:',
|
|
|
|
'b': b'Programming',
|
|
|
|
'c': b'the',
|
|
|
|
'd': b'way',
|
|
|
|
'f': b'Guido',
|
|
|
|
'g': b'intended',
|
2002-03-17 23:00:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
unittest.TestCase.__init__(self, *args)
|
|
|
|
|
|
|
|
def test_anydbm_creation(self):
|
|
|
|
f = anydbm.open(_fname, 'c')
|
2007-03-07 21:17:51 -04:00
|
|
|
self.assertEqual(list(f.keys()), [])
|
2002-03-17 23:00:37 -04:00
|
|
|
for key in self._dict:
|
|
|
|
f[key] = self._dict[key]
|
|
|
|
self.read_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_anydbm_modification(self):
|
|
|
|
self.init_db()
|
|
|
|
f = anydbm.open(_fname, 'c')
|
2007-05-23 17:51:02 -03:00
|
|
|
self._dict['g'] = f['g'] = b"indented"
|
2002-03-17 23:00:37 -04:00
|
|
|
self.read_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_anydbm_read(self):
|
|
|
|
self.init_db()
|
|
|
|
f = anydbm.open(_fname, 'r')
|
|
|
|
self.read_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_anydbm_keys(self):
|
|
|
|
self.init_db()
|
|
|
|
f = anydbm.open(_fname, 'r')
|
|
|
|
keys = self.keys_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
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 = anydbm.open(_fname, 'n')
|
|
|
|
for k in self._dict:
|
|
|
|
f[k] = self._dict[k]
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def keys_helper(self, f):
|
2007-02-11 02:12:03 -04:00
|
|
|
keys = sorted(f.keys())
|
|
|
|
dkeys = sorted(self._dict.keys())
|
2002-03-17 23:00:37 -04:00
|
|
|
self.assertEqual(keys, dkeys)
|
|
|
|
return keys
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
_delete_files()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
_delete_files()
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
try:
|
|
|
|
test_support.run_unittest(AnyDBMTestCase)
|
|
|
|
finally:
|
|
|
|
_delete_files()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|