1 """TestCases for using the DB.join and DBCursor.join_item methods.
8 from test_all
import db
, dbshelve
, test_support
, verbose
, \
9 get_new_environment_path
, get_new_database_path
11 #----------------------------------------------------------------------
14 ('apple', "Convenience Store"),
15 ('blueberry', "Farmer's Market"),
16 ('shotgun', "S-Mart"), # Aisle 12
17 ('pear', "Farmer's Market"),
18 ('chainsaw', "S-Mart"), # "Shop smart. Shop S-Mart!"
19 ('strawberry', "Farmer's Market"),
23 ('blue', "blueberry"),
26 ('red', "strawberry"),
32 class JoinTestCase(unittest
.TestCase
):
36 self
.filename
= self
.__class
__.__name
__ + '.db'
37 self
.homeDir
= get_new_environment_path()
39 self
.env
.open(self
.homeDir
, db
.DB_CREATE | db
.DB_INIT_MPOOL | db
.DB_INIT_LOCK
)
43 test_support
.rmtree(self
.homeDir
)
45 def test01_join(self
):
48 print "Running %s.test01_join..." % \
49 self
.__class
__.__name
__
51 # create and populate primary index
52 priDB
= db
.DB(self
.env
)
53 priDB
.open(self
.filename
, "primary", db
.DB_BTREE
, db
.DB_CREATE
)
54 map(lambda t
, priDB
=priDB
: apply(priDB
.put
, t
), ProductIndex
)
56 # create and populate secondary index
57 secDB
= db
.DB(self
.env
)
58 secDB
.set_flags(db
.DB_DUP | db
.DB_DUPSORT
)
59 secDB
.open(self
.filename
, "secondary", db
.DB_BTREE
, db
.DB_CREATE
)
60 map(lambda t
, secDB
=secDB
: apply(secDB
.put
, t
), ColorIndex
)
65 # lets look up all of the red Products
66 sCursor
= secDB
.cursor()
67 # Don't do the .set() in an assert, or you can get a bogus failure
68 # when running python -O
69 tmp
= sCursor
.set('red')
72 # FIXME: jCursor doesn't properly hold a reference to its
73 # cursors, if they are closed before jcursor is used it
75 jCursor
= priDB
.join([sCursor
])
77 if jCursor
.get(0) != ('apple', "Convenience Store"):
78 self
.fail("join cursor positioned wrong")
79 if jCursor
.join_item() != 'chainsaw':
80 self
.fail("DBCursor.join_item returned wrong item")
81 if jCursor
.get(0)[0] != 'strawberry':
82 self
.fail("join cursor returned wrong thing")
83 if jCursor
.get(0): # there were only three red items to return
84 self
.fail("join cursor returned too many items")
95 suite
= unittest
.TestSuite()
97 suite
.addTest(unittest
.makeSuite(JoinTestCase
))