lmdbal/test/basic.cpp

281 lines
8.2 KiB
C++

#include <gtest/gtest.h>
#include "database.h"
#include "table.h"
#include "cache.h"
#include <QString>
class DataBaseTest : public ::testing::Test {
protected:
DataBaseTest():
::testing::Test(),
t1(db->getTable<uint32_t, uint32_t>("table1")),
t2(db->getTable<QString, QString>("table2")),
c1(db->getCache<int8_t, std::string>("cache1")) {}
~DataBaseTest() {}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new DataBase("testBase");
db->addTable<uint32_t, uint32_t>("table1");
db->addTable<QString, QString>("table2");
db->addCache<int8_t, std::string>("cache1");
}
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static DataBase* db;
DataBase::Table<uint32_t, uint32_t>* t1;
DataBase::Table<QString, QString>* t2;
DataBase::Cache<int8_t, std::string>* c1;
};
DataBase* DataBaseTest::db = nullptr;
TEST_F(DataBaseTest, RemovingDirectory) {
EXPECT_EQ(db->removeDirectory(), true);
}
TEST_F(DataBaseTest, OpeningClosingDatabase) {
EXPECT_EQ(db->ready(), false);
db->open();
EXPECT_EQ(db->ready(), true);
db->close();
EXPECT_EQ(db->ready(), false);
db->open();
EXPECT_EQ(db->ready(), true);
}
TEST_F(DataBaseTest, AddingIntegerKey) {
EXPECT_EQ(db->ready(), true);
t1->addRecord(1, 2);
t1->addRecord(2, 2);
t1->addRecord(3, 15);
EXPECT_EQ(t1->getRecord(1), 2);
}
TEST_F(DataBaseTest, AddingQStringKey) {
EXPECT_EQ(db->ready(), true);
t2->addRecord("hello", "world");
t2->addRecord("aaa", "gagdfsdf");
t2->addRecord("sdfhga", "DSFFDG");
t2->addRecord("sdfsda", "shgsdgfa");
EXPECT_EQ(t2->getRecord("hello"), "world");
}
TEST_F(DataBaseTest, AddingKeysToCache) {
EXPECT_EQ(db->ready(), true);
c1->addRecord(2, "blah balah");
c1->addRecord(-4, "testing goes brrr");
c1->addRecord(140, "whatever");
c1->addRecord(-37, "aaaaa tss tsss tsss tsss aaaaaaa");
EXPECT_EQ(c1->getRecord(140), "whatever");
EXPECT_EQ(c1->getRecord(-116), "whatever");
}
TEST_F(DataBaseTest, AddingRepeatingIntegerKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
t1->addRecord(3, 24);
} catch (const DataBase::Exist e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
EXPECT_EQ(t1->getRecord(3), 15);
}
TEST_F(DataBaseTest, AddingRepeatingStringKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
t2->addRecord("sdfhga", "world");
} catch (const DataBase::Exist e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
}
TEST_F(DataBaseTest, AddingRepeatingCacheKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
c1->addRecord(-4, "world");
} catch (const DataBase::Exist e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
}
TEST_F(DataBaseTest, GettingNotExistingKeys) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
QString wrong = t2->getRecord("almonds");
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
thrown = false;
try {
uint32_t wrong = t1->getRecord(64);
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
thrown = false;
try {
std::string wrong = c1->getRecord(21);
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
}
TEST_F(DataBaseTest, Persistence) {
EXPECT_EQ(db->ready(), true);
db->close();
delete db;
db = new DataBase("testBase");
t1 = db->addTable<uint32_t, uint32_t>("table1");
t2 = db->addTable<QString, QString>("table2");
c1 = db->addCache<int8_t, std::string>("cache1");
db->open();
EXPECT_EQ(t1->getRecord(3), 15);
EXPECT_EQ(t1->getRecord(1), 2);
EXPECT_EQ(t1->getRecord(2), 2);
EXPECT_EQ(t2->getRecord("hello"), "world");
EXPECT_EQ(t2->getRecord("aaa"), "gagdfsdf");
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
EXPECT_EQ(t2->getRecord("sdfsda"), "shgsdgfa");
EXPECT_EQ(c1->getRecord(-116), "whatever");
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->getRecord(-37), "aaaaa tss tsss tsss tsss aaaaaaa");
EXPECT_EQ(c1->getRecord(2), "blah balah");
bool thrown = false;
try {
QString wrong = t2->getRecord("cats");
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
thrown = false;
try {
uint32_t wrong = t1->getRecord(7893);
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
thrown = false;
try {
std::string wrong = c1->getRecord(89);
} catch (const DataBase::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
}
TEST_F(DataBaseTest, CountAndDrop) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t2->count(), 4);
EXPECT_EQ(c1->count(), 4);
db->drop();
EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0);
EXPECT_EQ(c1->count(), 0);
t1->addRecord(2, 2);
t2->addRecord("sdfhga", "world");
c1->addRecord(15, "world");
c1->addRecord(12, "grr grr");
EXPECT_EQ(t1->count(), 1);
EXPECT_EQ(t2->count(), 1);
EXPECT_EQ(c1->count(), 2);
}
TEST_F(DataBaseTest, Change) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 1);
EXPECT_EQ(t2->count(), 1);
EXPECT_EQ(c1->count(), 2);
EXPECT_EQ(t1->getRecord(2), 2);
EXPECT_EQ(t2->getRecord("sdfhga"), "world");
EXPECT_EQ(c1->getRecord(15), "world");
EXPECT_EQ(c1->getRecord(12), "grr grr");
t1->addRecord(58, 39);
t2->addRecord("lawfirm", "stumble");
c1->addRecord(89, "answer");
t1->changeRecord(2, 49);
t2->changeRecord("sdfhga", "void");
c1->changeRecord(15, "recording");
c1->changeRecord(12, "thermal");
EXPECT_EQ(t1->getRecord(2), 49);
EXPECT_EQ(t2->getRecord("sdfhga"), "void");
EXPECT_EQ(c1->getRecord(15), "recording");
EXPECT_EQ(c1->getRecord(12), "thermal");
EXPECT_EQ(t1->getRecord(58), 39);
EXPECT_EQ(t2->getRecord("lawfirm"), "stumble");
EXPECT_EQ(c1->getRecord(89), "answer");
EXPECT_EQ(t1->count(), 2);
EXPECT_EQ(t2->count(), 2);
EXPECT_EQ(c1->count(), 3);
}
TEST_F(DataBaseTest, Force) {
EXPECT_EQ(db->ready(), true);
t1->forceRecord(58, 35); //changing
t1->forceRecord(68, 36); //adding
t2->forceRecord("prophecy", "dumpling"); //adding
t2->forceRecord("lawfirm", "paracetamol"); //changing
c1->forceRecord(89, "canine"); //changing
c1->forceRecord(98, "duration"); //adding
EXPECT_EQ(t1->getRecord(2), 49);
EXPECT_EQ(t1->getRecord(58), 35);
EXPECT_EQ(t1->getRecord(68), 36);
EXPECT_EQ(t2->getRecord("sdfhga"), "void");
EXPECT_EQ(t2->getRecord("prophecy"), "dumpling");
EXPECT_EQ(t2->getRecord("lawfirm"), "paracetamol");
EXPECT_EQ(c1->getRecord(15), "recording");
EXPECT_EQ(c1->getRecord(12), "thermal");
EXPECT_EQ(c1->getRecord(89), "canine");
EXPECT_EQ(c1->getRecord(98), "duration");
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t2->count(), 3);
EXPECT_EQ(c1->count(), 4);
}