@@ -282,11 +282,11 @@ def test02_DictionaryMethods(self):
282282
283283 #----------------------------------------
284284
285- def test03_SimpleCursorStuff (self ):
285+ def test03_SimpleCursorStuff (self , get_raises_error = 0 , set_raises_error = 1 ):
286286 if verbose :
287287 print '\n ' , '-=' * 30
288- print "Running %s.test03_SimpleCursorStuff..." % \
289- self .__class__ .__name__
288+ print "Running %s.test03_SimpleCursorStuff (get_error %s, set_error %s) ..." % \
289+ ( self .__class__ .__name__ , get_raises_error , set_raises_error )
290290
291291 if self .env and self .dbopenflags & db .DB_AUTO_COMMIT :
292292 txn = self .env .txn_begin ()
@@ -300,7 +300,15 @@ def test03_SimpleCursorStuff(self):
300300 count = count + 1
301301 if verbose and count % 100 == 0 :
302302 print rec
303- rec = c .next ()
303+ try :
304+ rec = c .next ()
305+ except db .DBNotFoundError , val :
306+ if get_raises_error :
307+ assert val [0 ] == db .DB_NOTFOUND
308+ if verbose : print val
309+ rec = None
310+ else :
311+ self .fail ("unexpected DBNotFoundError" )
304312
305313 assert count == 1000
306314
@@ -311,7 +319,15 @@ def test03_SimpleCursorStuff(self):
311319 count = count + 1
312320 if verbose and count % 100 == 0 :
313321 print rec
314- rec = c .prev ()
322+ try :
323+ rec = c .prev ()
324+ except db .DBNotFoundError , val :
325+ if get_raises_error :
326+ assert val [0 ] == db .DB_NOTFOUND
327+ if verbose : print val
328+ rec = None
329+ else :
330+ self .fail ("unexpected DBNotFoundError" )
315331
316332 assert count == 1000
317333
@@ -322,23 +338,29 @@ def test03_SimpleCursorStuff(self):
322338 assert rec [1 ] == self .makeData ('0505' )
323339
324340 try :
325- c .set ('bad key' )
341+ n = c .set ('bad key' )
326342 except db .DBNotFoundError , val :
327343 assert val [0 ] == db .DB_NOTFOUND
328344 if verbose : print val
329345 else :
330- self .fail ("expected exception" )
346+ if set_raises_error :
347+ self .fail ("expected exception" )
348+ if n != None :
349+ self .fail ("expected None: " + `n` )
331350
332351 rec = c .get_both ('0404' , self .makeData ('0404' ))
333352 assert rec == ('0404' , self .makeData ('0404' ))
334353
335354 try :
336- c .get_both ('0404' , 'bad data' )
355+ n = c .get_both ('0404' , 'bad data' )
337356 except db .DBNotFoundError , val :
338357 assert val [0 ] == db .DB_NOTFOUND
339358 if verbose : print val
340359 else :
341- self .fail ("expected exception" )
360+ if get_raises_error :
361+ self .fail ("expected exception" )
362+ if n != None :
363+ self .fail ("expected None: " + `n` )
342364
343365 if self .d .get_type () == db .DB_BTREE :
344366 rec = c .set_range ('011' )
@@ -414,6 +436,29 @@ def test03_SimpleCursorStuff(self):
414436 # SF pybsddb bug id 667343
415437 del oldcursor
416438
439+ def test03b_SimpleCursorWithoutGetReturnsNone0 (self ):
440+ # same test but raise exceptions instead of returning None
441+ if verbose :
442+ print '\n ' , '-=' * 30
443+ print "Running %s.test03b_SimpleCursorStuffWithoutGetReturnsNone..." % \
444+ self .__class__ .__name__
445+
446+ old = self .d .set_get_returns_none (0 )
447+ assert old == 1
448+ self .test03_SimpleCursorStuff (get_raises_error = 1 , set_raises_error = 1 )
449+
450+ def test03c_SimpleCursorGetReturnsNone2 (self ):
451+ # same test but raise exceptions instead of returning None
452+ if verbose :
453+ print '\n ' , '-=' * 30
454+ print "Running %s.test03c_SimpleCursorStuffWithoutSetReturnsNone..." % \
455+ self .__class__ .__name__
456+
457+ old = self .d .set_get_returns_none (2 )
458+ assert old == 1
459+ old = self .d .set_get_returns_none (2 )
460+ assert old == 2
461+ self .test03_SimpleCursorStuff (get_raises_error = 0 , set_raises_error = 0 )
417462
418463 #----------------------------------------
419464
0 commit comments