From 9465fd744cc2117190bafc1a3e2da9f10ca29bf9 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 31 Dec 2022 22:00:11 +0100 Subject: Storage via SQLite, Added tests (WIP) --- tests/test-storage.cpp | 178 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 tests/test-storage.cpp (limited to 'tests/test-storage.cpp') diff --git a/tests/test-storage.cpp b/tests/test-storage.cpp new file mode 100644 index 0000000..67d7236 --- /dev/null +++ b/tests/test-storage.cpp @@ -0,0 +1,178 @@ +#include + +#include +#include +#include + +#include "config.h" +#include "file.h" +#include "storage.h" + +namespace fs = std::filesystem; + +namespace { + const std::string testConfigFilename{"./test.conf"}; + const std::string testDbFilename{"./whiteboard.db3"}; +} + +class StorageTest: public ::testing::Test +{ +protected: + StorageTest(){ + File::setFile(testConfigFilename, R"CONFIG( + + . + 2592000 + +)CONFIG"); + std::error_code ec; + fs::remove(testDbFilename, ec); + + m_config = Config{testConfigFilename}; + } + + ~StorageTest(){ + std::error_code ec; + fs::remove(testDbFilename, ec); + fs::remove(testConfigFilename, ec); + } + + Config m_config; +}; + +TEST_F(StorageTest, create) +{ + ASSERT_TRUE(!fs::exists(testDbFilename)); + + { + ASSERT_EQ(m_config.getDataPath(), "."); + ASSERT_TRUE(!fs::exists(testDbFilename)); + Storage storage(m_config); + } + + ASSERT_TRUE(fs::exists(testDbFilename)); +} + +TEST_F(StorageTest, getNumberOfDocuments) +{ + Storage storage(m_config); + EXPECT_EQ(storage.getNumberOfDocuments(), 0); + storage.setDocument("123", "abc"); + EXPECT_EQ(storage.getNumberOfDocuments(), 1); + storage.setDocument("def", "xyz"); + EXPECT_EQ(storage.getNumberOfDocuments(), 2); +} + +TEST_F(StorageTest, cleanup_empty) +{ + Storage storage(m_config); + EXPECT_EQ(storage.getNumberOfDocuments(), 0); + storage.cleanup(); + EXPECT_EQ(storage.getNumberOfDocuments(), 0); +} + +TEST_F(StorageTest, cleanup) +{ + Storage storage(m_config); + EXPECT_EQ(storage.getNumberOfDocuments(), 0); + storage.setDocument("123", "abc"); + EXPECT_EQ(storage.getNumberOfDocuments(), 1); + storage.cleanup(); + EXPECT_EQ(storage.getNumberOfDocuments(), 1); +} + +TEST_F(StorageTest, exists) +{ + Storage storage(m_config); + EXPECT_EQ(storage.exists(""), false); + EXPECT_EQ(storage.exists("0"), false); + EXPECT_EQ(storage.exists("123"), false); + EXPECT_EQ(storage.exists("abcdz"), false); + + storage.setDocument("", "abc"); + EXPECT_EQ(storage.exists(""), true); + storage.setDocument("0", "abc"); + EXPECT_EQ(storage.exists("0"), true); + storage.setDocument("123", "abc"); + EXPECT_EQ(storage.exists("123"), true); + storage.setDocument("abcdz", "abc"); + EXPECT_EQ(storage.exists("abcdz"), true); +} + +TEST_F(StorageTest, setDocument) +{ + Storage storage(m_config); + storage.setDocument("0", "abc"); + EXPECT_EQ(storage.getNumberOfDocuments(), 1); + EXPECT_EQ(storage.getDocument("0"), "abc"); +} + +TEST_F(StorageTest, setRevision) +{ + Storage storage(m_config); + storage.setDocument("0", "abc"); + storage.setRevision("0", 123); + + EXPECT_EQ(storage.getNumberOfDocuments(), 1); + EXPECT_EQ(storage.getRevision("0"), 123); +} + +TEST_F(StorageTest, setCursorPos) +{ + Storage storage(m_config); + storage.setDocument("0", "abc"); + storage.setCursorPos("0", 1234); + + EXPECT_EQ(storage.getNumberOfDocuments(), 1); + EXPECT_EQ(storage.getCursorPos("0"), 1234); +} + +TEST_F(StorageTest, setRow) +{ + Storage storage(m_config); + storage.setRow("0", "abc", 56, 67); + + EXPECT_EQ(storage.getNumberOfDocuments(), 1); + EXPECT_EQ(storage.getDocument("0"), "abc"); + EXPECT_EQ(storage.getRevision("0"), 56); + EXPECT_EQ(storage.getCursorPos("0"), 67); +} + +TEST_F(StorageTest, getDocument) +{ + Storage storage(m_config); + storage.setDocument("0", "xyz"); + storage.setDocument("0bc", "xyz2"); + storage.setDocument("iabc", "xyz3"); + storage.setDocument("zxy", "xyz4"); + + EXPECT_EQ(storage.getDocument("0"), "xyz"); +} + +TEST_F(StorageTest, getRevision) +{ + Storage storage(m_config); + storage.setRow("0", "abc", 123, 456); + + EXPECT_EQ(storage.getRevision("0"), 123); +} + +TEST_F(StorageTest, getCursorPos) +{ + Storage storage(m_config); + storage.setRow("0", "abc", 123, 456); + + EXPECT_EQ(storage.getCursorPos("0"), 456); +} + +TEST_F(StorageTest, getRow) +{ + Storage storage(m_config); + storage.setRow("0", "abc", 123, 456); + + auto row{storage.getRow("0")}; + EXPECT_EQ(std::get<0>(row), "abc"); + EXPECT_EQ(std::get<1>(row), 123); + EXPECT_EQ(std::get<2>(row), 456); +} + -- cgit v1.2.3