summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-05-26 10:29:35 +0000
committerGeorg Brandl <georg@python.org>2008-05-26 10:29:35 +0000
commit0a7ac7d70d370544c6a9d118bbbd6886ad4f5ce5 (patch)
treeec61fd6d53e6425b8639567860140c724ea7bc63 /Lib/test
parente6f00637be87c8f5f0e50bf317d684ea421a6d19 (diff)
downloadcpython-git-0a7ac7d70d370544c6a9d118bbbd6886ad4f5ce5.tar.gz
Create the dbm package from PEP 3108. #2881.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test___all__.py2
-rw-r--r--Lib/test/test_anydbm.py90
-rwxr-xr-xLib/test/test_bsddb.py2
-rw-r--r--Lib/test/test_dbm_dumb.py (renamed from Lib/test/test_dumbdbm.py)2
-rwxr-xr-xLib/test/test_dbm_gnu.py (renamed from Lib/test/test_gdbm.py)2
-rwxr-xr-xLib/test/test_dbm_ndbm.py (renamed from Lib/test/test_dbm.py)12
-rw-r--r--Lib/test/test_whichdb.py58
7 files changed, 64 insertions, 104 deletions
diff --git a/Lib/test/test___all__.py b/Lib/test/test___all__.py
index 615f6c5a1e..ed8d8d9c80 100644
--- a/Lib/test/test___all__.py
+++ b/Lib/test/test___all__.py
@@ -57,7 +57,7 @@ class AllTest(unittest.TestCase):
self.check_all("copy")
self.check_all("copyreg")
self.check_all("csv")
- self.check_all("dbhash")
+ self.check_all("dbm.bsd")
self.check_all("decimal")
self.check_all("difflib")
self.check_all("dircache")
diff --git a/Lib/test/test_anydbm.py b/Lib/test/test_anydbm.py
index ace9dd207c..aab1388d79 100644
--- a/Lib/test/test_anydbm.py
+++ b/Lib/test/test_anydbm.py
@@ -1,50 +1,34 @@
#! /usr/bin/env python
-"""Test script for the anydbm module
- based on testdumbdbm.py
-"""
+"""Test script for the dbm.open function based on testdumbdbm.py"""
import os
import unittest
-import anydbm
+import dbm
import glob
-from test import support
-
-_fname = support.TESTFN
-
-_all_modules = []
-
-for _name in anydbm._names:
- try:
- _module = __import__(_name)
- except ImportError:
- continue
- _all_modules.append(_module)
+import test.support
+_fname = test.support.TESTFN
#
-# Iterates over every database module supported by anydbm
-# currently available, setting anydbm to use each in turn,
-# and yielding that module
+# Iterates over every database module supported by dbm currently available,
+# setting dbm to use each in turn, and yielding that module
#
def dbm_iterator():
- old_default = anydbm._defaultmod
- for module in _all_modules:
- anydbm._defaultmod = module
+ old_default = dbm._defaultmod
+ for module in dbm._modules.values():
+ dbm._defaultmod = module
yield module
- anydbm._defaultmod = old_default
+ dbm._defaultmod = old_default
#
-# Clean up all scratch databases we might have created
-# during testing
+# Clean up all scratch databases we might have created during testing
#
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
+ test.support.unlink(f)
+
class AnyDBMTestCase(unittest.TestCase):
_dict = {'0': b'',
@@ -60,7 +44,7 @@ class AnyDBMTestCase(unittest.TestCase):
unittest.TestCase.__init__(self, *args)
def test_anydbm_creation(self):
- f = anydbm.open(_fname, 'c')
+ f = dbm.open(_fname, 'c')
self.assertEqual(list(f.keys()), [])
for key in self._dict:
f[key.encode("ascii")] = self._dict[key]
@@ -69,26 +53,26 @@ class AnyDBMTestCase(unittest.TestCase):
def test_anydbm_modification(self):
self.init_db()
- f = anydbm.open(_fname, 'c')
+ f = dbm.open(_fname, 'c')
self._dict['g'] = f[b'g'] = b"indented"
self.read_helper(f)
f.close()
def test_anydbm_read(self):
self.init_db()
- f = anydbm.open(_fname, 'r')
+ f = dbm.open(_fname, 'r')
self.read_helper(f)
f.close()
def test_anydbm_keys(self):
self.init_db()
- f = anydbm.open(_fname, 'r')
+ f = dbm.open(_fname, 'r')
keys = self.keys_helper(f)
f.close()
def test_anydbm_access(self):
self.init_db()
- f = anydbm.open(_fname, 'r')
+ f = dbm.open(_fname, 'r')
key = "a".encode("ascii")
assert(key in f)
assert(f[key] == b"Python:")
@@ -100,7 +84,7 @@ class AnyDBMTestCase(unittest.TestCase):
self.assertEqual(self._dict[key], f[key.encode("ascii")])
def init_db(self):
- f = anydbm.open(_fname, 'n')
+ f = dbm.open(_fname, 'n')
for k in self._dict:
f[k.encode("ascii")] = self._dict[k]
f.close()
@@ -118,10 +102,44 @@ class AnyDBMTestCase(unittest.TestCase):
delete_files()
+class WhichDBTestCase(unittest.TestCase):
+ # Actual test methods are added to namespace after class definition.
+ def __init__(self, *args):
+ unittest.TestCase.__init__(self, *args)
+
+ def test_whichdb(self):
+ for module in dbm_iterator():
+ # Check whether whichdb correctly guesses module name
+ # for databases opened with "module" module.
+ # Try with empty files first
+ name = module.__name__
+ if name == 'dbm.dumb':
+ continue # whichdb can't support dbm.dumb
+ test.support.unlink(_fname)
+ f = module.open(_fname, 'c')
+ f.close()
+ self.assertEqual(name, dbm.whichdb(_fname))
+ # Now add a key
+ f = module.open(_fname, 'w')
+ f[b"1"] = b"1"
+ # and test that we can find it
+ self.assertTrue(b"1" in f)
+ # and read it
+ self.assertTrue(f[b"1"] == b"1")
+ f.close()
+ self.assertEqual(name, dbm.whichdb(_fname))
+
+ def tearDown(self):
+ delete_files()
+
+ def setUp(self):
+ delete_files()
+
+
def test_main():
try:
for module in dbm_iterator():
- support.run_unittest(AnyDBMTestCase)
+ test.support.run_unittest(AnyDBMTestCase, WhichDBTestCase)
finally:
delete_files()
diff --git a/Lib/test/test_bsddb.py b/Lib/test/test_bsddb.py
index 3eb291f3a9..a722d8ca9d 100755
--- a/Lib/test/test_bsddb.py
+++ b/Lib/test/test_bsddb.py
@@ -5,7 +5,7 @@
import os, sys
import copy
import bsddb
-import dbhash # Just so we know it's imported
+import dbm.bsd # Just so we know it's imported
import unittest
from test import support
diff --git a/Lib/test/test_dumbdbm.py b/Lib/test/test_dbm_dumb.py
index 24c178ffd9..9bdc240637 100644
--- a/Lib/test/test_dumbdbm.py
+++ b/Lib/test/test_dbm_dumb.py
@@ -6,7 +6,7 @@
import io
import os
import unittest
-import dumbdbm
+import dbm.dumb as dumbdbm
from test import support
_fname = support.TESTFN
diff --git a/Lib/test/test_gdbm.py b/Lib/test/test_dbm_gnu.py
index 42cb136ac5..eddb970831 100755
--- a/Lib/test/test_gdbm.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -1,4 +1,4 @@
-import gdbm
+import dbm.gnu as gdbm
import unittest
import os
from test.support import verbose, TESTFN, run_unittest, unlink
diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm_ndbm.py
index 2c6ce9902a..74d32386ae 100755
--- a/Lib/test/test_dbm.py
+++ b/Lib/test/test_dbm_ndbm.py
@@ -2,14 +2,14 @@ from test import support
import unittest
import os
import random
-import dbm
-from dbm import error
+import dbm.ndbm
+from dbm.ndbm import error
class DbmTestCase(unittest.TestCase):
def setUp(self):
self.filename = support.TESTFN
- self.d = dbm.open(self.filename, 'c')
+ self.d = dbm.ndbm.open(self.filename, 'c')
self.d.close()
def tearDown(self):
@@ -17,7 +17,7 @@ class DbmTestCase(unittest.TestCase):
support.unlink(self.filename + suffix)
def test_keys(self):
- self.d = dbm.open(self.filename, 'c')
+ self.d = dbm.ndbm.open(self.filename, 'c')
self.assert_(self.d.keys() == [])
self.d['a'] = 'b'
self.d['12345678910'] = '019237410982340912840198242'
@@ -28,9 +28,9 @@ class DbmTestCase(unittest.TestCase):
def test_modes(self):
for mode in ['r', 'rw', 'w', 'n']:
try:
- self.d = dbm.open(self.filename, mode)
+ self.d = dbm.ndbm.open(self.filename, mode)
self.d.close()
- except dbm.error:
+ except error:
self.fail()
def test_main():
diff --git a/Lib/test/test_whichdb.py b/Lib/test/test_whichdb.py
deleted file mode 100644
index d908ac5b3b..0000000000
--- a/Lib/test/test_whichdb.py
+++ /dev/null
@@ -1,58 +0,0 @@
-#! /usr/bin/env python
-"""Test script for the whichdb module
- based on test_anydbm.py
-"""
-
-import os
-import test.support
-import unittest
-import whichdb
-import anydbm
-import glob
-from test.test_anydbm import delete_files, dbm_iterator
-
-_fname = test.support.TESTFN
-
-class WhichDBTestCase(unittest.TestCase):
- # Actual test methods are added to namespace
- # after class definition.
- def __init__(self, *args):
- unittest.TestCase.__init__(self, *args)
-
- def test_whichdb(self):
- for module in dbm_iterator():
- # Check whether whichdb correctly guesses module name
- # for databases opened with "module" module.
- # Try with empty files first
- name = module.__name__
- if name == 'dumbdbm':
- continue # whichdb can't support dumbdbm
- test.support.unlink(_fname)
- f = module.open(_fname, 'c')
- f.close()
- self.assertEqual(name, whichdb.whichdb(_fname))
- # Now add a key
- f = module.open(_fname, 'w')
- f[b"1"] = b"1"
- # and test that we can find it
- self.assertTrue(b"1" in f)
- # and read it
- self.assertTrue(f[b"1"] == b"1")
- f.close()
- self.assertEqual(name, whichdb.whichdb(_fname))
-
- def tearDown(self):
- delete_files()
-
- def setUp(self):
- delete_files()
-
-
-def test_main():
- try:
- test.support.run_unittest(WhichDBTestCase)
- finally:
- delete_files()
-
-if __name__ == "__main__":
- test_main()