#include #include "database.h" #include "table.h" #include class DataBaseTest : public ::testing::Test { protected: DataBaseTest(): ::testing::Test(), t1(db->getTable("table1")), t2(db->getTable("table2")) {} ~DataBaseTest() {} static void SetUpTestSuite() { if (db == nullptr) { db = new DataBase("testBase"); db->addTable("table1"); db->addTable("table2"); } } static void TearDownTestSuite() { db->close(); db->removeDirectory(); delete db; db = nullptr; } static DataBase* db; DataBase::Table* t1; DataBase::Table* t2; }; 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, 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, 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"; } TEST_F(DataBaseTest, Persistence) { EXPECT_EQ(db->ready(), true); db->close(); delete db; db = new DataBase("testBase"); t1 = db->addTable("table1"); t2 = db->addTable("table2"); 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"); 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"; } TEST_F(DataBaseTest, CountAndDrop) { EXPECT_EQ(db->ready(), true); EXPECT_EQ(t1->count(), 3); EXPECT_EQ(t2->count(), 4); db->drop(); EXPECT_EQ(t1->count(), 0); EXPECT_EQ(t2->count(), 0); t1->addRecord(2, 2); t2->addRecord("sdfhga", "world"); EXPECT_EQ(t1->count(), 1); EXPECT_EQ(t2->count(), 1); }