Add unit test for tree cursors
This commit is contained in:
parent
baf368c049
commit
35c4236f27
@ -31,6 +31,8 @@ if config.get('preferences.use-bsddb3') or sys.version_info[0] >= 3:
|
|||||||
else:
|
else:
|
||||||
from bsddb import dbshelve, db
|
from bsddb import dbshelve, db
|
||||||
|
|
||||||
|
from ..read import DbBsddbTreeCursor
|
||||||
|
|
||||||
class Data(object):
|
class Data(object):
|
||||||
|
|
||||||
def __init__(self, handle,surname, name):
|
def __init__(self, handle,surname, name):
|
||||||
@ -76,6 +78,7 @@ class CursorTest(unittest.TestCase):
|
|||||||
os.mkdir(env_name)
|
os.mkdir(env_name)
|
||||||
self.env.open(env_name,env_flags)
|
self.env.open(env_name,env_flags)
|
||||||
(self.person_map,self.surnames) = self._open_tables()
|
(self.person_map,self.surnames) = self._open_tables()
|
||||||
|
(self.place_map, self.placerefs) = self._open_treetables()
|
||||||
|
|
||||||
def _open_tables(self):
|
def _open_tables(self):
|
||||||
dbmap = dbshelve.DBShelf(self.env)
|
dbmap = dbshelve.DBShelf(self.env)
|
||||||
@ -101,9 +104,35 @@ class CursorTest(unittest.TestCase):
|
|||||||
|
|
||||||
return (person_map,surnames)
|
return (person_map,surnames)
|
||||||
|
|
||||||
|
def _open_treetables(self):
|
||||||
|
dbmap = dbshelve.DBShelf(self.env)
|
||||||
|
dbmap.db.set_pagesize(16384)
|
||||||
|
dbmap.open(self.full_name, 'places', db.DB_HASH,
|
||||||
|
db.DB_CREATE|db.DB_AUTO_COMMIT, 0o666)
|
||||||
|
place_map = dbmap
|
||||||
|
|
||||||
|
table_flags = db.DB_CREATE|db.DB_AUTO_COMMIT
|
||||||
|
|
||||||
|
placerefs = db.DB(self.env)
|
||||||
|
placerefs.set_flags(db.DB_DUP|db.DB_DUPSORT)
|
||||||
|
placerefs.open(self.full_name, "placerefs", db.DB_BTREE,
|
||||||
|
flags=table_flags)
|
||||||
|
|
||||||
|
def find_placeref(key,data):
|
||||||
|
val = data[2]
|
||||||
|
if isinstance(val, UNITYPE):
|
||||||
|
val = val.encode('utf-8')
|
||||||
|
return val
|
||||||
|
|
||||||
|
place_map.associate(placerefs, find_placeref, table_flags)
|
||||||
|
|
||||||
|
return (place_map, placerefs)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.person_map.close()
|
self.person_map.close()
|
||||||
self.surnames.close()
|
self.surnames.close()
|
||||||
|
self.place_map.close()
|
||||||
|
self.placerefs.close()
|
||||||
self.env.close()
|
self.env.close()
|
||||||
shutil.rmtree(self._tmpdir)
|
shutil.rmtree(self._tmpdir)
|
||||||
|
|
||||||
@ -183,6 +212,28 @@ class CursorTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(v.handle, data.handle)
|
self.assertEqual(v.handle, data.handle)
|
||||||
|
|
||||||
|
def test_treecursor(self):
|
||||||
|
#fill with data
|
||||||
|
the_txn = self.env.txn_begin()
|
||||||
|
data = [(b'1', 'countryA', '' ),
|
||||||
|
(b'2', 'localityA', '1' ),
|
||||||
|
(b'3', 'localityB', '1' ),
|
||||||
|
(b'4', 'countryB', '' ),
|
||||||
|
(b'5', 'streetA', '2' ),
|
||||||
|
(b'6', 'countryB', '' )]
|
||||||
|
for d in data:
|
||||||
|
self.place_map.put(d[0], d, txn=the_txn)
|
||||||
|
the_txn.commit()
|
||||||
|
|
||||||
|
cursor_txn = self.env.txn_begin()
|
||||||
|
cursor = DbBsddbTreeCursor(self.placerefs, self.place_map, False,
|
||||||
|
cursor_txn)
|
||||||
|
placenames = set([d[1] for handle, d in cursor])
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
cursor_txn.commit()
|
||||||
|
pldata = set([d[1] for d in data])
|
||||||
|
self.assertEqual(placenames, pldata)
|
||||||
|
|
||||||
def testSuite():
|
def testSuite():
|
||||||
suite = unittest.makeSuite(CursorTest, 'test')
|
suite = unittest.makeSuite(CursorTest, 'test')
|
||||||
|
Loading…
Reference in New Issue
Block a user