1
0
forked from blue/lmdbal
lmdbal/test/storagetransaction.cpp

112 lines
3.0 KiB
C++

#include <gtest/gtest.h>
#include "base.h"
#include "storage.h"
class StorageTransactionsTest : public testing::Test {
protected:
StorageTransactionsTest():
testing::Test(),
t1(db->getStorage<int16_t, int64_t>("table1")),
t2(db->getStorage<std::string, float>("table2")) {}
~StorageTransactionsTest() {}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("storageTrnansactionsTestBase");
db->addStorage<int16_t, int64_t>("table1");
db->addStorage<std::string, float>("table2");
}
db->open();
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
LMDBAL::Storage<int16_t, int64_t>* t1;
LMDBAL::Storage<std::string, float>* t2;
};
LMDBAL::Base* StorageTransactionsTest::db = nullptr;
TEST_F(StorageTransactionsTest, Adding) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0);
LMDBAL::TransactionID txn = db->beginTransaction();
t1->addRecord(5, 13, txn);
t1->addRecord(-53, 782, txn);
t1->addRecord(5892, -37829, txn);
t2->addRecord("lorem", 481, txn);
t2->addRecord("decallence", 8532.48, txn);
t2->addRecord("prevent recovery", -64.64, txn);
EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0);
db->commitTransaction(txn);
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t1->getRecord(5), 13);
EXPECT_EQ(t1->getRecord(-53), 782);
EXPECT_EQ(t1->getRecord(5892), -37829);
EXPECT_EQ(t2->count(), 3);
EXPECT_FLOAT_EQ(t2->getRecord("lorem"), 481);
EXPECT_FLOAT_EQ(t2->getRecord("decallence"), 8532.48);
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery"), -64.64);
}
TEST_F(StorageTransactionsTest, Aborting) {
EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType s1 = t1->count();
LMDBAL::SizeType s2 = t2->count();
LMDBAL::TransactionID txn = db->beginTransaction();
t1->addRecord(18, 40, txn);
t1->addRecord(85, -4, txn);
t1->addRecord(-5, -3, txn);
t2->addRecord("tapestry", .053, txn);
t2->addRecord("pepper plants are beautifull", -7, txn);
t2->addRecord("horrots", -23.976, txn);
EXPECT_EQ(t1->count(), s1);
EXPECT_EQ(t2->count(), s2);
db->abortTransaction(txn);
EXPECT_EQ(t1->count(), s1);
EXPECT_EQ(t2->count(), s2);
}
TEST_F(StorageTransactionsTest, Reading) {
EXPECT_EQ(db->ready(), true);
LMDBAL::TransactionID txn = db->beginReadOnlyTransaction();
EXPECT_EQ(t1->count(txn), 3);
EXPECT_EQ(t1->getRecord(5, txn), 13);
EXPECT_EQ(t1->getRecord(-53, txn), 782);
EXPECT_EQ(t1->getRecord(5892, txn), -37829);
EXPECT_EQ(t2->count(txn), 3);
EXPECT_FLOAT_EQ(t2->getRecord("lorem", txn), 481);
EXPECT_FLOAT_EQ(t2->getRecord("decallence", txn), 8532.48);
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery", txn), -64.64);
db->abortTransaction(txn);
}