summaryrefslogtreecommitdiffhomepage
path: root/tests/test-storage.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2022-12-31 22:00:11 +0100
committerRoland Reichwein <mail@reichwein.it>2022-12-31 22:00:11 +0100
commit9465fd744cc2117190bafc1a3e2da9f10ca29bf9 (patch)
tree7d94bdaaa37cabb58cede695b03082b8360167bd /tests/test-storage.cpp
parentaf1c4ee4d74ff7afc997372802d851d11daad418 (diff)
Storage via SQLite, Added tests (WIP)
Diffstat (limited to 'tests/test-storage.cpp')
-rw-r--r--tests/test-storage.cpp178
1 files changed, 178 insertions, 0 deletions
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 <gtest/gtest.h>
+
+#include <filesystem>
+#include <string>
+#include <system_error>
+
+#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(
+<config>
+ <datapath>.</datapath>
+ <maxage>2592000</maxage>
+</config>
+)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);
+}
+