2006-06-05 15:20:07 -03:00
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
from test_all import db, test_support, get_new_environment_path, get_new_database_path
|
2008-05-13 17:57:59 -03:00
|
|
|
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
class DBSequenceTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.int_32_max = 0x100000000
|
2008-05-13 17:57:59 -03:00
|
|
|
self.homeDir = get_new_environment_path()
|
|
|
|
self.filename = "test"
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
self.dbenv = db.DBEnv()
|
|
|
|
self.dbenv.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL, 0666)
|
|
|
|
self.d = db.DB(self.dbenv)
|
|
|
|
self.d.open(self.filename, db.DB_BTREE, db.DB_CREATE, 0666)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if hasattr(self, 'seq'):
|
|
|
|
self.seq.close()
|
|
|
|
del self.seq
|
|
|
|
if hasattr(self, 'd'):
|
|
|
|
self.d.close()
|
|
|
|
del self.d
|
|
|
|
if hasattr(self, 'dbenv'):
|
|
|
|
self.dbenv.close()
|
|
|
|
del self.dbenv
|
|
|
|
|
2008-02-24 14:47:03 -04:00
|
|
|
test_support.rmtree(self.homeDir)
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
|
|
|
start_value = 10 * self.int_32_max
|
|
|
|
self.assertEqual(0xA00000000, start_value)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.initial_value(start_value))
|
|
|
|
self.assertEqual(None, self.seq.open(key='id', txn=None, flags=db.DB_CREATE))
|
|
|
|
self.assertEqual(start_value, self.seq.get(5))
|
|
|
|
self.assertEqual(start_value + 5, self.seq.get())
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
def test_remove(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE))
|
|
|
|
self.assertEqual(None, self.seq.remove(txn=None, flags=0))
|
2006-06-05 15:20:07 -03:00
|
|
|
del self.seq
|
|
|
|
|
|
|
|
def test_get_key(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
|
|
|
key = 'foo'
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.open(key=key, txn=None, flags=db.DB_CREATE))
|
|
|
|
self.assertEqual(key, self.seq.get_key())
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
def test_get_dbp(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE))
|
|
|
|
self.assertEqual(self.d, self.seq.get_dbp())
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
def test_cachesize(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
|
|
|
cashe_size = 10
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.set_cachesize(cashe_size))
|
|
|
|
self.assertEqual(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE))
|
|
|
|
self.assertEqual(cashe_size, self.seq.get_cachesize())
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
def test_flags(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
|
|
|
flag = db.DB_SEQ_WRAP;
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.set_flags(flag))
|
|
|
|
self.assertEqual(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE))
|
|
|
|
self.assertEqual(flag, self.seq.get_flags() & flag)
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
def test_range(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
|
|
|
seq_range = (10 * self.int_32_max, 11 * self.int_32_max - 1)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.set_range(seq_range))
|
2010-03-22 11:22:26 -03:00
|
|
|
self.seq.initial_value(seq_range[0])
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE))
|
|
|
|
self.assertEqual(seq_range, self.seq.get_range())
|
2006-06-05 15:20:07 -03:00
|
|
|
|
|
|
|
def test_stat(self):
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE))
|
2006-06-05 15:20:07 -03:00
|
|
|
stat = self.seq.stat()
|
|
|
|
for param in ('nowait', 'min', 'max', 'value', 'current',
|
|
|
|
'flags', 'cache_size', 'last_value', 'wait'):
|
|
|
|
self.assertTrue(param in stat, "parameter %s isn't in stat info" % param)
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
if db.version() >= (4,7) :
|
|
|
|
# This code checks a crash solved in Berkeley DB 4.7
|
|
|
|
def test_stat_crash(self) :
|
|
|
|
d=db.DB()
|
|
|
|
d.open(None,dbtype=db.DB_HASH,flags=db.DB_CREATE) # In RAM
|
|
|
|
seq = db.DBSequence(d, flags=0)
|
|
|
|
|
|
|
|
self.assertRaises(db.DBNotFoundError, seq.open,
|
|
|
|
key='id', txn=None, flags=0)
|
|
|
|
|
2008-05-22 12:27:38 -03:00
|
|
|
self.assertRaises(db.DBInvalidArgError, seq.stat)
|
2008-05-13 17:57:59 -03:00
|
|
|
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
def test_64bits(self) :
|
2008-05-19 12:55:11 -03:00
|
|
|
# We don't use both extremes because they are problematic
|
|
|
|
value_plus=(1L<<63)-2
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(9223372036854775806L,value_plus)
|
2008-05-19 12:55:11 -03:00
|
|
|
value_minus=(-1L<<63)+1 # Two complement
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(-9223372036854775807L,value_minus)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.initial_value(value_plus-1))
|
|
|
|
self.assertEqual(None, self.seq.open(key='id', txn=None,
|
2008-05-13 17:57:59 -03:00
|
|
|
flags=db.DB_CREATE))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(value_plus-1, self.seq.get(1))
|
|
|
|
self.assertEqual(value_plus, self.seq.get(1))
|
2008-05-13 17:57:59 -03:00
|
|
|
|
|
|
|
self.seq.remove(txn=None, flags=0)
|
|
|
|
|
|
|
|
self.seq = db.DBSequence(self.d, flags=0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(None, self.seq.initial_value(value_minus))
|
|
|
|
self.assertEqual(None, self.seq.open(key='id', txn=None,
|
2008-05-13 17:57:59 -03:00
|
|
|
flags=db.DB_CREATE))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(value_minus, self.seq.get(1))
|
|
|
|
self.assertEqual(value_minus+1, self.seq.get(1))
|
2008-05-13 17:57:59 -03:00
|
|
|
|
|
|
|
def test_multiple_close(self):
|
|
|
|
self.seq = db.DBSequence(self.d)
|
|
|
|
self.seq.close() # You can close a Sequence multiple times
|
|
|
|
self.seq.close()
|
|
|
|
self.seq.close()
|
|
|
|
|
2006-06-05 15:20:07 -03:00
|
|
|
def test_suite():
|
|
|
|
suite = unittest.TestSuite()
|
2013-03-19 18:46:29 -03:00
|
|
|
suite.addTest(unittest.makeSuite(DBSequenceTest))
|
2006-06-05 15:20:07 -03:00
|
|
|
return suite
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(defaultTest='test_suite')
|