lmdbal/test/basic.cpp

365 lines
10 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
2023-03-21 11:05:54 +00:00
#include "base.h"
2023-03-20 15:37:13 +00:00
#include "storage.h"
2022-09-20 17:16:48 +00:00
#include "cache.h"
#include <QString>
2023-03-21 11:05:54 +00:00
class BaseTest : public ::testing::Test {
protected:
2023-03-21 11:05:54 +00:00
BaseTest():
::testing::Test(),
2023-03-26 18:42:52 +00:00
t1(db->getStorage<uint32_t, uint32_t>("table1")),
t2(db->getStorage<QString, QString>("table2")),
2022-09-20 17:16:48 +00:00
c1(db->getCache<int8_t, std::string>("cache1")) {}
2023-03-21 11:05:54 +00:00
~BaseTest() {}
static void SetUpTestSuite() {
if (db == nullptr) {
2023-03-21 11:05:54 +00:00
db = new LMDBAL::Base("testBase");
2023-03-26 18:42:52 +00:00
db->addStorage<uint32_t, uint32_t>("table1");
db->addStorage<QString, QString>("table2");
2022-09-20 17:16:48 +00:00
db->addCache<int8_t, std::string>("cache1");
}
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
2023-03-21 11:05:54 +00:00
static LMDBAL::Base* db;
2023-03-21 11:05:54 +00:00
LMDBAL::Storage<uint32_t, uint32_t>* t1;
LMDBAL::Storage<QString, QString>* t2;
LMDBAL::Cache<int8_t, std::string>* c1;
};
2023-03-21 11:05:54 +00:00
LMDBAL::Base* BaseTest::db = nullptr;
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, RemovingDirectory) {
EXPECT_EQ(db->removeDirectory(), true);
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, 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);
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, AddingIntegerKey) {
EXPECT_EQ(db->ready(), true);
t1->addRecord(1, 2);
t1->addRecord(2, 2);
t1->addRecord(3, 15);
EXPECT_EQ(t1->getRecord(1), 2);
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, 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");
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, 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");
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, AddingRepeatingIntegerKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
t1->addRecord(3, 24);
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::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);
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, AddingRepeatingStringKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
t2->addRecord("sdfhga", "world");
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::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");
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, AddingRepeatingCacheKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
c1->addRecord(-4, "world");
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::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");
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, GettingNotExistingKeys) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
QString wrong = t2->getRecord("almonds");
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::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);
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::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);
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, Persistence) {
EXPECT_EQ(db->ready(), true);
db->close();
delete db;
2023-03-21 11:05:54 +00:00
db = new LMDBAL::Base("testBase");
2023-03-26 18:42:52 +00:00
t1 = db->addStorage<uint32_t, uint32_t>("table1");
t2 = db->addStorage<QString, QString>("table2");
2022-09-20 17:16:48 +00:00
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");
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::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);
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::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);
2023-03-21 11:05:54 +00:00
} catch (const LMDBAL::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, 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);
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, 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);
}
2023-03-21 11:05:54 +00:00
TEST_F(BaseTest, Force) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->forceRecord(58, 35), false); //changing
EXPECT_EQ(t1->forceRecord(68, 36), true); //adding
EXPECT_EQ(t2->forceRecord("prophecy", "dumpling"), true); //adding
EXPECT_EQ(t2->forceRecord("lawfirm", "paracetamol"), false); //changing
EXPECT_EQ(c1->forceRecord(89, "canine"), false); //changing
EXPECT_EQ(c1->forceRecord(98, "duration"), true); //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);
}
TEST_F(BaseTest, ReadAll) {
EXPECT_EQ(db->ready(), true);
std::map<uint32_t, uint32_t> m1 = t1->readAll();
std::map<QString, QString> m2 = t2->readAll();
std::map<int8_t, std::string> m3 = c1->readAll();
EXPECT_EQ(m1.at(2), 49);
EXPECT_EQ(m1.at(58), 35);
EXPECT_EQ(m1.at(68), 36);
EXPECT_EQ(m2.at("sdfhga"), "void");
EXPECT_EQ(m2.at("prophecy"), "dumpling");
EXPECT_EQ(m2.at("lawfirm"), "paracetamol");
EXPECT_EQ(m3.at(15), "recording");
EXPECT_EQ(m3.at(12), "thermal");
EXPECT_EQ(m3.at(89), "canine");
EXPECT_EQ(m3.at(98), "duration");
EXPECT_EQ(m1.size(), 3);
EXPECT_EQ(m2.size(), 3);
EXPECT_EQ(m3.size(), 4);
}
TEST_F(BaseTest, ReplaceAll) {
EXPECT_EQ(db->ready(), true);
t1->replaceAll({
{7, 48},
{194, 582},
{857, 39},
{9717, 8}
});
t2->replaceAll({
{"bringin", "keyboard"},
{"cluster", "throttle"},
{"ronin", "cheese"}
});
c1->replaceAll({});
EXPECT_EQ(t1->count(), 4);
EXPECT_EQ(t2->count(), 3);
EXPECT_EQ(c1->count(), 0);
EXPECT_FALSE(t1->checkRecord(2));
EXPECT_FALSE(t1->checkRecord(58));
EXPECT_FALSE(t1->checkRecord(68));
EXPECT_FALSE(t2->checkRecord("sdfhga"));
EXPECT_FALSE(t2->checkRecord("prophecy"));
EXPECT_FALSE(t2->checkRecord("lawfirm"));
EXPECT_FALSE(c1->checkRecord(15));
EXPECT_FALSE(c1->checkRecord(12));
EXPECT_FALSE(c1->checkRecord(89));
EXPECT_FALSE(c1->checkRecord(98));
EXPECT_EQ(t1->getRecord(7), 48);
EXPECT_EQ(t1->getRecord(194), 582);
EXPECT_EQ(t1->getRecord(857), 39);
EXPECT_EQ(t1->getRecord(9717), 8);
EXPECT_EQ(t2->getRecord("bringin"), "keyboard");
EXPECT_EQ(t2->getRecord("cluster"), "throttle");
EXPECT_EQ(t2->getRecord("ronin"), "cheese");
c1->replaceAll({
{68, "quality"},
{31, "ridgid body"},
{16, "fermentation on your kind"},
{22, "pseudo"},
{-117, "lance of Michael"},
});
EXPECT_EQ(c1->count(), 5);
EXPECT_EQ(c1->getRecord(68), "quality");
EXPECT_EQ(c1->getRecord(31), "ridgid body");
EXPECT_EQ(c1->getRecord(16), "fermentation on your kind");
EXPECT_EQ(c1->getRecord(22), "pseudo");
EXPECT_EQ(c1->getRecord(-117), "lance of Michael");
}