diff options
Diffstat (limited to 'Lib/bsddb/test/test_recno.py')
-rw-r--r-- | Lib/bsddb/test/test_recno.py | 120 |
1 files changed, 59 insertions, 61 deletions
diff --git a/Lib/bsddb/test/test_recno.py b/Lib/bsddb/test/test_recno.py index 3bc2f2d..300f833 100644 --- a/Lib/bsddb/test/test_recno.py +++ b/Lib/bsddb/test/test_recno.py @@ -2,26 +2,11 @@ """ import os -import shutil -import sys import errno -import tempfile from pprint import pprint import unittest -from bsddb.test.test_all import verbose - -try: - # For Pythons w/distutils pybsddb - from bsddb3 import db -except ImportError: - # For Python 2.3 - from bsddb import db - -try: - from bsddb3 import test_support -except ImportError: - from test import support as test_support +from .test_all import db, test_support, verbose, get_new_environment_path, get_new_database_path letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' @@ -29,8 +14,13 @@ letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' #---------------------------------------------------------------------- class SimpleRecnoTestCase(unittest.TestCase): + import sys + if sys.version_info[:3] < (2, 4, 0): + def assertFalse(self, expr, msg=None): + self.failIf(expr,msg=msg) + def setUp(self): - self.filename = tempfile.mktemp() + self.filename = get_new_database_path() self.homeDir = None def tearDown(self): @@ -47,9 +37,9 @@ class SimpleRecnoTestCase(unittest.TestCase): d.open(self.filename, db.DB_RECNO, db.DB_CREATE) for x in letters: - recno = d.append(x.encode('ascii') * 60) - assert type(recno) == type(0) - assert recno >= 1 + recno = d.append(x * 60) + self.assertEqual(type(recno), type(0)) + self.assert_(recno >= 1) if verbose: print(recno, end=' ') @@ -64,20 +54,24 @@ class SimpleRecnoTestCase(unittest.TestCase): if verbose: print(data) - assert type(data) == bytes - assert data == d.get(recno) + self.assertEqual(type(data), type("")) + self.assertEqual(data, d.get(recno)) try: data = d[0] # This should raise a KeyError!?!?! except db.DBInvalidArgError as val: - assert val.args[0] == db.EINVAL + import sys + if sys.version_info[0] < 3 : + self.assertEqual(val[0], db.EINVAL) + else : + self.assertEqual(val.args[0], db.EINVAL) if verbose: print(val) else: self.fail("expected exception") # test that has_key raises DB exceptions (fixed in pybsddb 4.3.2) try: - d.has_key(0) + 0 in d except db.DBError as val: pass else: @@ -96,35 +90,35 @@ class SimpleRecnoTestCase(unittest.TestCase): if get_returns_none: self.fail("unexpected exception") else: - assert data == None + self.assertEqual(data, None) - keys = d.keys() + keys = list(d.keys()) if verbose: print(keys) - assert type(keys) == type([]) - assert type(keys[0]) == type(123) - assert len(keys) == len(d) + self.assertEqual(type(keys), type([])) + self.assertEqual(type(keys[0]), type(123)) + self.assertEqual(len(keys), len(d)) - items = d.items() + items = list(d.items()) if verbose: pprint(items) - assert type(items) == type([]) - assert type(items[0]) == type(()) - assert len(items[0]) == 2 - assert type(items[0][0]) == type(123) - assert type(items[0][1]) == bytes - assert len(items) == len(d) + self.assertEqual(type(items), type([])) + self.assertEqual(type(items[0]), type(())) + self.assertEqual(len(items[0]), 2) + self.assertEqual(type(items[0][0]), type(123)) + self.assertEqual(type(items[0][1]), type("")) + self.assertEqual(len(items), len(d)) - assert d.has_key(25) + self.assert_(25 in d) del d[25] - assert not d.has_key(25) + self.assertFalse(25 in d) d.delete(13) - assert not d.has_key(13) + self.assertFalse(13 in d) - data = d.get_both(26, b"z" * 60) - assert data == b"z" * 60, 'was %r' % data + data = d.get_both(26, "z" * 60) + self.assertEqual(data, "z" * 60, 'was %r' % data) if verbose: print(data) @@ -137,18 +131,18 @@ class SimpleRecnoTestCase(unittest.TestCase): while rec: if verbose: print(rec) - rec = c.next() + rec = next(c) c.set(50) rec = c.current() if verbose: print(rec) - c.put(-1, b"a replacement record", db.DB_CURRENT) + c.put(-1, "a replacement record", db.DB_CURRENT) c.set(50) rec = c.current() - assert rec == (50, b"a replacement record") + self.assertEqual(rec, (50, "a replacement record")) if verbose: print(rec) @@ -159,7 +153,7 @@ class SimpleRecnoTestCase(unittest.TestCase): # test that non-existant key lookups work (and that # DBC_set_range doesn't have a memleak under valgrind) rec = c.set_range(999999) - assert rec == None + self.assertEqual(rec, None) if verbose: print(rec) @@ -171,8 +165,8 @@ class SimpleRecnoTestCase(unittest.TestCase): c = d.cursor() # put a record beyond the consecutive end of the recno's - d[100] = b"way out there" - assert d[100] == b"way out there" + d[100] = "way out there" + self.assertEqual(d[100], "way out there") try: data = d[99] @@ -187,7 +181,7 @@ class SimpleRecnoTestCase(unittest.TestCase): if get_returns_none: self.fail("unexpected DBKeyEmptyError exception") else: - assert val.args[0] == db.DB_KEYEMPTY + self.assertEqual(val[0], db.DB_KEYEMPTY) if verbose: print(val) else: if not get_returns_none: @@ -197,7 +191,7 @@ class SimpleRecnoTestCase(unittest.TestCase): while rec: if verbose: print(rec) - rec = c.next() + rec = next(c) c.close() d.close() @@ -209,7 +203,7 @@ class SimpleRecnoTestCase(unittest.TestCase): just a line in the file, but you can set a different record delimiter if needed. """ - homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid()) + homeDir = get_new_environment_path() self.homeDir = homeDir source = os.path.join(homeDir, 'test_recno.txt') if not os.path.isdir(homeDir): @@ -226,7 +220,7 @@ class SimpleRecnoTestCase(unittest.TestCase): data = "The quick brown fox jumped over the lazy dog".split() for datum in data: - d.append(datum.encode('ascii')) + d.append(datum) d.sync() d.close() @@ -238,15 +232,15 @@ class SimpleRecnoTestCase(unittest.TestCase): print(data) print(text.split('\n')) - assert text.split('\n') == data + self.assertEqual(text.split('\n'), data) # open as a DB again d = db.DB() d.set_re_source(source) d.open(self.filename, db.DB_RECNO) - d[3] = b'reddish-brown' - d[8] = b'comatose' + d[3] = 'reddish-brown' + d[8] = 'comatose' d.sync() d.close() @@ -257,8 +251,8 @@ class SimpleRecnoTestCase(unittest.TestCase): print(text) print(text.split('\n')) - assert text.split('\n') == \ - "The quick reddish-brown fox jumped over the comatose dog".split() + self.assertEqual(text.split('\n'), + "The quick reddish-brown fox jumped over the comatose dog".split()) def test03_FixedLength(self): d = db.DB() @@ -268,14 +262,18 @@ class SimpleRecnoTestCase(unittest.TestCase): d.open(self.filename, db.DB_RECNO, db.DB_CREATE) for x in letters: - d.append(x.encode('ascii') * 35) # These will be padded + d.append(x * 35) # These will be padded - d.append(b'.' * 40) # this one will be exact + d.append('.' * 40) # this one will be exact try: # this one will fail - d.append(b'bad' * 20) + d.append('bad' * 20) except db.DBInvalidArgError as val: - assert val.args[0] == db.EINVAL + import sys + if sys.version_info[0] < 3 : + self.assertEqual(val[0], db.EINVAL) + else : + self.assertEqual(val.args[0], db.EINVAL) if verbose: print(val) else: self.fail("expected exception") @@ -285,7 +283,7 @@ class SimpleRecnoTestCase(unittest.TestCase): while rec: if verbose: print(rec) - rec = c.next() + rec = next(c) c.close() d.close() |