def test_container_add(): testContainer = containerTest('a', 'b') txn = db.getEnvTxn() testContainer.add('test', txn=txn) txn.commit() assert len(testContainer.get()) == 1 txn = db.getEnvTxn() testContainer.add('test2', txn=txn) txn.commit() assert len(testContainer.get()) == 2
def addTest(TestDB, keyLength=5, numLabels=100, sampleSize=100, testGet=False): byKeys = [] allFuncs = [] addTime = [] keysList = generateKeyList(keyLength, numLabels, 2) for keys in keysList: sample = ThingToStore.sample(sampleSize) startAdd = time.time() txn = db.getEnvTxn() TestDB.TestResource(*keys).put(sample, txn=txn) txn.commit() endAdd = time.time() addTime.append(endAdd - startAdd) if testGet: byKey, AllFunc = getAllValuesTime(TestDB) byKeys.append(byKey) allFuncs.append(AllFunc) return addTime, byKeys, allFuncs
def test_resource_alter(): test = ResourceToTest("a", "b") txn = db.getEnvTxn() test.alter(alterTest, txn=txn) txn.commit() assert test.get() == 'testaltered'
def test_resource_delete(): txn = db.getEnvTxn() nextTest = ResourceToTest("a", "c") nextTest.put(None, txn=txn) txn.commit() assert len(ResourceToTest.all()) == 1
def test_container_remove(): testContainer = containerTest('a', 'b') txn = db.getEnvTxn() testContainer.remove('test', txn=txn) txn.commit() assert len(testContainer.get()) == 1 assert testContainer.get()[0] == 'test2'
def test_detect_deadlock(): global runDeadlock firstTxn = db.getEnvTxn() secondTxn = db.getEnvTxn() thread = threading.Thread(target=deadlockDetector) thread.start() # Block using a write first = CursorTest('1').get(txn=firstTxn, write=True) # This should error due to deadlock with pytest.raises(berkeleydb.db.DBLockDeadlockError): second = CursorTest('1').get(txn=secondTxn, write=True) firstTxn.commit() secondTxn.commit() runDeadlock = False thread.join()
def testCursorzzPut(): txn = db.getEnvTxn() cursor = CursorTest.getCursor(txn=txn, bulk=True) current = cursor.next(flags=berkeleydb.db.DB_RMW) while current is not None: (key, ), value = current print(key, value) toPut = value - 0.5 cursor.put(key, toPut) current = cursor.next(flags=berkeleydb.db.DB_RMW) cursor.close() txn.commit() txn = db.getEnvTxn() cursor = CursorTest.getCursor(txn=txn, bulk=True) current = cursor.next(flags=berkeleydb.db.DB_RMW) # check that the changes went into effect while current is not None: # Key is a tuple of length one here, unpack it to a value (key, ), value = current assert int(key) == value + 0.5 current = cursor.next(flags=berkeleydb.db.DB_RMW) cursor.close() txn.commit()
def testCursorzDuplicate(): for i in range(3, 11): txn = db.getEnvTxn() CursorTest(str(i)).put(i, txn=txn) txn.commit() assert len(CursorTest.db_keys()) == 10 txn = db.getEnvTxn() cursor = CursorTest.getCursor(txn=txn, bulk=True) prev = None # Testing next and dup functions for i in range(1, 11): key, current = cursor.next() assert current == i if prev is None: prev = cursor.dup() else: prevKey, prevCurrent = prev.current() assert prevCurrent + 1 == current prev.close() prev = cursor.dup() if prev is not None: prev.close() cursor.close() txn.commit()
def testCursors(): for i in range(1, 3): txn = db.getEnvTxn() CursorTest(str(i)).put(i, txn=txn) txn.commit() txn = db.getEnvTxn() assert len(CursorTest.db_keys()) == 2 cursor = CursorTest.getCursor(txn=txn, bulk=True) firstKey, firstValue = cursor.get(flags=berkeleydb.db.DB_NEXT) assert firstKey == ('1', ) assert firstValue == 1 secondKey, secondValue = cursor.get(flags=berkeleydb.db.DB_NEXT) assert secondKey == ('2', ) assert secondValue == 2 # Nothing left in db out = cursor.get(flags=berkeleydb.db.DB_NEXT) assert out is None withKeyKey, withKeyValue = cursor.getWithKey(firstKey, flags=berkeleydb.db.DB_SET) assert withKeyKey == ('1', ) assert withKeyValue == 1 cursor.close() txn.commit()
def test_error_restart(): @db.txnAbortOnError def wrapped(txn=None): global hasErrored CursorTest('1').get(txn=txn, write=True) if not hasErrored: hasErrored = True raise Exception with pytest.raises(Exception): wrapped() # Check that the write lock was aborted due to the wrapper otherTxn = db.getEnvTxn() CursorTest('1').get(txn=otherTxn, write=True) otherTxn.commit()
def testCursorzzzPutLaterWithDup(): toSelect = round((len(CursorTest.db_keys()) - 1) * random.random()) + 1 txn = db.getEnvTxn() cursor = CursorTest.getCursor(txn=txn, bulk=True) randomSelected = None current = cursor.next(flags=berkeleydb.db.DB_RMW) while current is not None: (key, ), value = current if int(key) == toSelect: randomSelected = cursor.dup() current = cursor.next(flags=berkeleydb.db.DB_RMW) cursor.close() toUpdate = randomSelected.current() assert toUpdate is not None (key, ), value = toUpdate assert int(key) == toSelect value += 0.25 randomSelected.put(key, value) randomSelected.close() txn.commit() toCheck = CursorTest(str(toSelect)).get() assert value == toCheck
def getTxn(): return db.getEnvTxn()
def testPandasDf(): dfTest = pandasTest('a', 'b') otherDfTest = pandasTest('a', 'c') initial = dfTest.get() assert isinstance(initial, pd.DataFrame) assert len(initial.index) == 0 seriesToAdd = pd.Series({'test': 1, 'otherVal': 'test'}) txn = db.getEnvTxn() dfTest.add(seriesToAdd, txn=txn) txn.commit() afterSeriesAdd = dfTest.get() assert isinstance(afterSeriesAdd, pd.DataFrame) assert len(afterSeriesAdd.index) == 1 otherSeriesToAdd = pd.Series({'test': 0, 'otherVal': 'test0'}) txn = db.getEnvTxn() dfTest.add(otherSeriesToAdd, txn=txn) txn.commit() afterOtherSeriesAdd = dfTest.get() assert isinstance(afterOtherSeriesAdd, pd.DataFrame) assert len(afterOtherSeriesAdd.index) == 2 updateOtherSeries = pd.Series({'test': 0, 'otherVal': 'updatedWithSeries'}) txn = db.getEnvTxn() dfTest.add(updateOtherSeries, txn=txn) txn.commit() afterSeriesUpdate = dfTest.get() assert isinstance(afterSeriesUpdate, pd.DataFrame) assert len(afterSeriesUpdate.index) == 2 val = afterSeriesUpdate[afterSeriesUpdate['test'] == 0] assert len(val.index) == 1 assert val.iloc[0]['otherVal'] == 'updatedWithSeries' otherInit = otherDfTest.get() assert isinstance(otherInit, pd.DataFrame) assert len(otherInit.index) == 0 dfToAdd = pd.DataFrame({ 'test': [1, 2, 3], 'otherVal': ['updatedWithDf', 'test2', 'test3'] }) txn = db.getEnvTxn() otherDfTest.add(dfToAdd, txn=txn) txn.commit() afterOtherAddDf = otherDfTest.get() assert isinstance(afterOtherAddDf, pd.DataFrame) assert len(afterOtherAddDf.index) == 3 txn = db.getEnvTxn() dfTest.add(dfToAdd, txn=txn) txn.commit() afterDfAdd = dfTest.get() assert isinstance(afterDfAdd, pd.DataFrame) assert len(afterDfAdd.index) == 4 test0 = afterDfAdd[afterDfAdd['test'] == 0] assert len(test0.index) == 1 assert test0.iloc[0]['otherVal'] == 'updatedWithSeries' test1 = afterDfAdd[afterDfAdd['test'] == 1] assert len(test1.index) == 1 assert test1.iloc[0]['otherVal'] == 'updatedWithDf' test1 = afterDfAdd[afterDfAdd['test'] == 2] assert len(test1.index) == 1 assert test1.iloc[0]['otherVal'] == 'test2' toRemove = pd.Series({'test': 2}) txn = db.getEnvTxn() removed, dfAfterRemove = dfTest.remove(toRemove, txn=txn) txn.commit() assert (len(dfAfterRemove.index)) == 3