2002-12-30 16:44:16 -04:00
|
|
|
"""TestCases for using the DB.join and DBCursor.join_item methods.
|
2002-11-19 13:47:07 -04:00
|
|
|
"""
|
|
|
|
|
2008-02-23 13:40:11 -04:00
|
|
|
import os
|
2003-07-09 01:45:59 -03:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
import unittest
|
2003-07-09 01:45:59 -03:00
|
|
|
from test_all import verbose
|
|
|
|
|
|
|
|
try:
|
2003-09-20 21:08:14 -03:00
|
|
|
# For Pythons w/distutils pybsddb
|
|
|
|
from bsddb3 import db, dbshelve
|
|
|
|
except ImportError:
|
2003-07-09 01:45:59 -03:00
|
|
|
# For Python 2.3
|
|
|
|
from bsddb import db, dbshelve
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
from test_all import get_new_environment_path, get_new_database_path
|
|
|
|
|
2008-03-02 16:00:53 -04:00
|
|
|
try:
|
|
|
|
from bsddb3 import test_support
|
|
|
|
except ImportError:
|
|
|
|
from test import test_support
|
2003-07-09 01:45:59 -03:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
|
2003-07-09 01:45:59 -03:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
ProductIndex = [
|
|
|
|
('apple', "Convenience Store"),
|
|
|
|
('blueberry', "Farmer's Market"),
|
|
|
|
('shotgun', "S-Mart"), # Aisle 12
|
|
|
|
('pear', "Farmer's Market"),
|
|
|
|
('chainsaw', "S-Mart"), # "Shop smart. Shop S-Mart!"
|
|
|
|
('strawberry', "Farmer's Market"),
|
|
|
|
]
|
|
|
|
|
|
|
|
ColorIndex = [
|
|
|
|
('blue', "blueberry"),
|
|
|
|
('red', "apple"),
|
|
|
|
('red', "chainsaw"),
|
|
|
|
('red', "strawberry"),
|
|
|
|
('yellow', "peach"),
|
|
|
|
('yellow', "pear"),
|
|
|
|
('black', "shotgun"),
|
|
|
|
]
|
|
|
|
|
|
|
|
class JoinTestCase(unittest.TestCase):
|
|
|
|
keytype = ''
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.filename = self.__class__.__name__ + '.db'
|
2008-05-13 17:57:59 -03:00
|
|
|
self.homeDir = get_new_environment_path()
|
2003-07-09 01:45:59 -03:00
|
|
|
self.env = db.DBEnv()
|
2008-05-13 17:57:59 -03:00
|
|
|
self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL | db.DB_INIT_LOCK )
|
2003-07-09 01:45:59 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.env.close()
|
2008-02-24 14:47:03 -04:00
|
|
|
test_support.rmtree(self.homeDir)
|
2003-07-09 01:45:59 -03:00
|
|
|
|
|
|
|
def test01_join(self):
|
|
|
|
if verbose:
|
|
|
|
print '\n', '-=' * 30
|
|
|
|
print "Running %s.test01_join..." % \
|
|
|
|
self.__class__.__name__
|
|
|
|
|
|
|
|
# create and populate primary index
|
|
|
|
priDB = db.DB(self.env)
|
|
|
|
priDB.open(self.filename, "primary", db.DB_BTREE, db.DB_CREATE)
|
2003-07-09 02:33:14 -03:00
|
|
|
map(lambda t, priDB=priDB: apply(priDB.put, t), ProductIndex)
|
2003-07-09 01:45:59 -03:00
|
|
|
|
|
|
|
# create and populate secondary index
|
|
|
|
secDB = db.DB(self.env)
|
|
|
|
secDB.set_flags(db.DB_DUP | db.DB_DUPSORT)
|
|
|
|
secDB.open(self.filename, "secondary", db.DB_BTREE, db.DB_CREATE)
|
2003-07-09 02:33:14 -03:00
|
|
|
map(lambda t, secDB=secDB: apply(secDB.put, t), ColorIndex)
|
2003-07-09 01:45:59 -03:00
|
|
|
|
|
|
|
sCursor = None
|
|
|
|
jCursor = None
|
|
|
|
try:
|
|
|
|
# lets look up all of the red Products
|
|
|
|
sCursor = secDB.cursor()
|
2003-07-21 15:43:33 -03:00
|
|
|
# Don't do the .set() in an assert, or you can get a bogus failure
|
|
|
|
# when running python -O
|
|
|
|
tmp = sCursor.set('red')
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assert_(tmp)
|
2003-07-09 01:45:59 -03:00
|
|
|
|
|
|
|
# FIXME: jCursor doesn't properly hold a reference to its
|
|
|
|
# cursors, if they are closed before jcursor is used it
|
|
|
|
# can cause a crash.
|
|
|
|
jCursor = priDB.join([sCursor])
|
|
|
|
|
|
|
|
if jCursor.get(0) != ('apple', "Convenience Store"):
|
|
|
|
self.fail("join cursor positioned wrong")
|
|
|
|
if jCursor.join_item() != 'chainsaw':
|
|
|
|
self.fail("DBCursor.join_item returned wrong item")
|
|
|
|
if jCursor.get(0)[0] != 'strawberry':
|
|
|
|
self.fail("join cursor returned wrong thing")
|
|
|
|
if jCursor.get(0): # there were only three red items to return
|
|
|
|
self.fail("join cursor returned too many items")
|
|
|
|
finally:
|
|
|
|
if jCursor:
|
|
|
|
jCursor.close()
|
|
|
|
if sCursor:
|
|
|
|
sCursor.close()
|
|
|
|
priDB.close()
|
|
|
|
secDB.close()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
2002-12-30 16:44:16 -04:00
|
|
|
def test_suite():
|
|
|
|
suite = unittest.TestSuite()
|
2003-07-09 01:45:59 -03:00
|
|
|
|
|
|
|
suite.addTest(unittest.makeSuite(JoinTestCase))
|
|
|
|
|
2002-12-30 16:44:16 -04:00
|
|
|
return suite
|