2002-11-19 13:47:07 -04:00
|
|
|
"""
|
|
|
|
TestCases for checking set_get_returns_none.
|
|
|
|
"""
|
|
|
|
|
2008-02-23 13:40:11 -04:00
|
|
|
import os, string
|
2002-11-19 13:47:07 -04:00
|
|
|
import unittest
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
from test_all import db, verbose, get_new_database_path
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class GetReturnsNoneTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2008-05-13 17:57:59 -03:00
|
|
|
self.filename = get_new_database_path()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
|
|
|
os.remove(self.filename)
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test01_get_returns_none(self):
|
|
|
|
d = db.DB()
|
|
|
|
d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
|
|
|
|
d.set_get_returns_none(1)
|
|
|
|
|
2014-12-01 04:34:23 -04:00
|
|
|
for x in string.ascii_letters:
|
2002-11-19 13:47:07 -04:00
|
|
|
d.put(x, x * 40)
|
|
|
|
|
|
|
|
data = d.get('bad key')
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(data, None)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2014-12-01 04:34:23 -04:00
|
|
|
data = d.get(string.ascii_letters[0])
|
|
|
|
self.assertEqual(data, string.ascii_letters[0]*40)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
count = count + 1
|
|
|
|
rec = c.next()
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(rec, None)
|
2014-12-01 04:34:23 -04:00
|
|
|
self.assertEqual(count, len(string.ascii_letters))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
c.close()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
|
|
|
|
def test02_get_raises_exception(self):
|
|
|
|
d = db.DB()
|
|
|
|
d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
|
|
|
|
d.set_get_returns_none(0)
|
|
|
|
|
2014-12-01 04:34:23 -04:00
|
|
|
for x in string.ascii_letters:
|
2002-11-19 13:47:07 -04:00
|
|
|
d.put(x, x * 40)
|
|
|
|
|
|
|
|
self.assertRaises(db.DBNotFoundError, d.get, 'bad key')
|
|
|
|
self.assertRaises(KeyError, d.get, 'bad key')
|
|
|
|
|
2014-12-01 04:34:23 -04:00
|
|
|
data = d.get(string.ascii_letters[0])
|
|
|
|
self.assertEqual(data, string.ascii_letters[0]*40)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
exceptionHappened = 0
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
count = count + 1
|
|
|
|
try:
|
|
|
|
rec = c.next()
|
|
|
|
except db.DBNotFoundError: # end of the records
|
|
|
|
exceptionHappened = 1
|
|
|
|
break
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertNotEqual(rec, None)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertTrue(exceptionHappened)
|
2014-12-01 04:34:23 -04:00
|
|
|
self.assertEqual(count, len(string.ascii_letters))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
c.close()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def test_suite():
|
2002-11-19 13:47:07 -04:00
|
|
|
return unittest.makeSuite(GetReturnsNoneTestCase)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2002-12-30 16:53:52 -04:00
|
|
|
unittest.main(defaultTest='test_suite')
|