summaryrefslogtreecommitdiffhomepage
path: root/tests/test-storage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-storage.cpp')
-rw-r--r--tests/test-storage.cpp283
1 files changed, 283 insertions, 0 deletions
diff --git a/tests/test-storage.cpp b/tests/test-storage.cpp
new file mode 100644
index 0000000..6239f8f
--- /dev/null
+++ b/tests/test-storage.cpp
@@ -0,0 +1,283 @@
+#include <gtest/gtest.h>
+
+#include <filesystem>
+#include <memory>
+#include <string>
+#include <system_error>
+
+#include "libreichwein/file.h"
+
+#include "config.h"
+#include "storage.h"
+
+namespace fs = std::filesystem;
+using namespace Reichwein;
+using namespace std::string_literals;
+
+namespace {
+ const std::string testConfigFilename{"./test.conf"};
+ const std::string testDbFilename{"./whiteboard.db3"};
+}
+
+class StorageTest: public ::testing::Test
+{
+protected:
+ StorageTest(){
+ }
+
+ ~StorageTest() override{
+ }
+
+ void SetUp() override
+ {
+ File::setFile(testConfigFilename, R"CONFIG(
+<config>
+ <datapath>.</datapath>
+ <maxage>2592000</maxage>
+</config>
+)CONFIG");
+ std::error_code ec;
+ fs::remove(testDbFilename, ec);
+
+ m_config = std::make_shared<Config>(testConfigFilename);
+ }
+
+ void TearDown() override
+ {
+ std::error_code ec;
+ fs::remove(testDbFilename, ec);
+ fs::remove(testConfigFilename, ec);
+ }
+
+ std::shared_ptr<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(), 0UL);
+ storage.setDocument("123", "abc");
+ EXPECT_EQ(storage.getNumberOfDocuments(), 1UL);
+ storage.setDocument("def", "xyz");
+ EXPECT_EQ(storage.getNumberOfDocuments(), 2UL);
+}
+
+TEST_F(StorageTest, cleanup_empty)
+{
+ Storage storage(*m_config);
+ EXPECT_EQ(storage.getNumberOfDocuments(), 0UL);
+ storage.cleanup();
+ EXPECT_EQ(storage.getNumberOfDocuments(), 0UL);
+}
+
+TEST_F(StorageTest, cleanup)
+{
+ Storage storage(*m_config);
+ EXPECT_EQ(storage.getNumberOfDocuments(), 0UL);
+ storage.setDocument("123", "abc");
+ EXPECT_EQ(storage.getNumberOfDocuments(), 1UL);
+ storage.cleanup();
+ EXPECT_EQ(storage.getNumberOfDocuments(), 1UL);
+}
+
+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(), 1UL);
+ EXPECT_EQ(storage.getDocument("0"), "abc");
+}
+
+TEST_F(StorageTest, touchDocument)
+{
+ Storage storage(*m_config);
+ EXPECT_THROW(storage.touchDocument("0"), std::exception);
+ storage.setDocument("0", "abc");
+ storage.touchDocument("0");
+ EXPECT_EQ(storage.getNumberOfDocuments(), 1UL);
+ 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(), 1UL);
+ EXPECT_EQ(storage.getRevision("0"), 123);
+
+ try {
+ storage.setRevision("1", 123);
+ FAIL();
+ } catch(const std::exception& ex) {
+ EXPECT_EQ("Unable to set revision for id 1"s, ex.what());
+ } catch(...) {
+ FAIL();
+ }
+}
+
+TEST_F(StorageTest, setCursorPos)
+{
+ Storage storage(*m_config);
+ storage.setDocument("0", "abc");
+ storage.setCursorPos("0", 1234);
+
+ EXPECT_EQ(storage.getNumberOfDocuments(), 1UL);
+ EXPECT_EQ(storage.getCursorPos("0"), 1234);
+
+ try {
+ storage.setCursorPos("1", 12345);
+ FAIL();
+ } catch(const std::exception& ex) {
+ EXPECT_EQ("Unable to set cursor position for id 1"s, ex.what());
+ } catch(...) {
+ FAIL();
+ }
+}
+
+TEST_F(StorageTest, setRow)
+{
+ Storage storage(*m_config);
+ storage.setRow("0", "abc", 56, 67);
+
+ EXPECT_EQ(storage.getNumberOfDocuments(), 1UL);
+ 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);
+}
+
+TEST_F(StorageTest, revision_increment)
+{
+ Storage storage(*m_config);
+ storage.setDocument("0", "xyz");
+ storage.setDocument("0bc", "xyz2");
+ storage.setDocument("iabc", "xyz3");
+
+ EXPECT_EQ(storage.getRevision("0"), 0);
+ EXPECT_EQ(storage.getRevision("0bc"), 0);
+ EXPECT_EQ(storage.getRevision("iabc"), 0);
+
+ storage.setDocument("0bc", "xyz234");
+ EXPECT_EQ(storage.getRevision("0bc"), 1);
+
+ storage.setDocument("0bc", "xyz2345");
+ EXPECT_EQ(storage.getRevision("0"), 0);
+ EXPECT_EQ(storage.getRevision("0bc"), 2);
+ EXPECT_EQ(storage.getRevision("iabc"), 0);
+}
+
+TEST_F(StorageTest, generate_id)
+{
+ Storage storage(*m_config);
+ for (int i = 0; i < 100; i++) {
+ std::string a{storage.generate_id()};
+ std::string b{storage.generate_id()};
+
+ EXPECT_NE(a, b);
+ EXPECT_NE(a, "");
+ EXPECT_NE(b, "");
+
+ EXPECT_GE(a.size(), 6);
+
+ for (char c: a + b) {
+ EXPECT_TRUE((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'));
+ }
+ }
+}
+
+TEST_F(StorageTest, checksum32)
+{
+ EXPECT_EQ(checksum32(""), 0);
+ EXPECT_EQ(checksum32("0"), 48);
+ EXPECT_EQ(checksum32("\x00"), 0);
+ EXPECT_EQ(checksum32("123"), 1073741862);
+ EXPECT_EQ(checksum32("a"), 97);
+ EXPECT_EQ(checksum32("ab"), 82);
+ EXPECT_EQ(checksum32("abc"), 1073741898);
+}
+
+TEST_F(StorageTest, db_size)
+{
+ Storage storage(*m_config);
+ auto dbsize_gross{storage.dbsize_gross()};
+ auto dbsize_net{storage.dbsize_net()};
+
+ EXPECT_LE(0, storage.dbsize_net());
+ EXPECT_LE(storage.dbsize_net(), storage.dbsize_gross());
+
+ storage.setDocument("0", "xyz");
+
+ EXPECT_LE(dbsize_net, storage.dbsize_net());
+ EXPECT_LE(dbsize_gross, storage.dbsize_gross());
+}
+