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/Makefile | 9 ++- tests/test-config.cpp | 61 +++++++++++++++++ tests/test-storage.cpp | 178 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/unittests.cpp | 94 -------------------------- 4 files changed, 245 insertions(+), 97 deletions(-) create mode 100644 tests/test-config.cpp create mode 100644 tests/test-storage.cpp delete mode 100644 tests/unittests.cpp (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index 78097ce..1f912c3 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,8 @@ include ../common.mk +UNITTESTS=test-config.cpp \ + test-storage.cpp + CXXFLAGS+=\ -I/usr/src/googletest/googletest/include \ -I/usr/src/googletest/googlemock/include \ @@ -10,11 +13,11 @@ CXXFLAGS+=\ test: unittests ./unittests -unittests: libgmock.a unittests.o ../config.o ../file.o ../storage.o +unittests: libgmock.a $(UNITTESTS:.cpp=.o) ../config.o ../file.o ../storage.o $(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@ -unittests.o: unittests.cpp - $(CXX) $(CXXFLAGS) -o $@ -c unittests.cpp +%.o: %.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< libgmock.a: $(CXX) $(CXXFLAGS) -c /usr/src/googletest/googletest/src/gtest-all.cc diff --git a/tests/test-config.cpp b/tests/test-config.cpp new file mode 100644 index 0000000..065dedf --- /dev/null +++ b/tests/test-config.cpp @@ -0,0 +1,61 @@ +#include + +#include +#include +#include + +#include "config.h" +#include "file.h" + +namespace fs = std::filesystem; + +namespace { + const std::string testConfigFilename{"./test.conf"}; + const std::string testDbFilename{"./whiteboard.db3"}; +} + +class ConfigTest: public ::testing::Test +{ +protected: + ConfigTest(){ + } + + ~ConfigTest(){ + } +}; + +TEST_F(ConfigTest, defaultData) +{ + std::string filename{testConfigFilename + "doesntexist"}; + std::error_code ec; + fs::remove(filename, ec); + ASSERT_TRUE(!fs::exists(filename)); + { + Config config{filename}; + EXPECT_EQ(config.getDataPath(), "/var/lib/whiteboard"); + EXPECT_EQ(config.getMaxage(), 0); + ASSERT_TRUE(!fs::exists(filename)); + } + + ASSERT_TRUE(!fs::exists(filename)); +} + +TEST_F(ConfigTest, testData) +{ + File::setFile(testConfigFilename, R"CONFIG( + + /some/other/location + 2592000 + +)CONFIG"); + + { + Config config{testConfigFilename}; + EXPECT_EQ(config.getDataPath(), "/some/other/location"); + EXPECT_EQ(config.getMaxage(), 2592000); + } + + std::error_code ec; + fs::remove(testConfigFilename, ec); +} + 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); +} + diff --git a/tests/unittests.cpp b/tests/unittests.cpp deleted file mode 100644 index 3b24f83..0000000 --- a/tests/unittests.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#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 ConfigTest: public ::testing::Test -{ -protected: - ConfigTest(){ - } - - ~ConfigTest(){ - } -}; - -TEST_F(ConfigTest, defaultData) -{ - std::string filename{testConfigFilename + "doesntexist"}; - std::error_code ec; - fs::remove(filename, ec); - ASSERT_TRUE(!fs::exists(filename)); - { - Config config{filename}; - EXPECT_EQ(config.getDataPath(), "/var/lib/whiteboard"); - ASSERT_TRUE(!fs::exists(filename)); - } - - ASSERT_TRUE(!fs::exists(filename)); -} - -TEST_F(ConfigTest, testData) -{ - File::setFile(testConfigFilename, R"CONFIG( - - /some/other/location - 2592000 - -)CONFIG"); - - { - Config config{testConfigFilename}; - EXPECT_EQ(config.getDataPath(), "/some/other/location"); - } - - std::error_code ec; - fs::remove(testConfigFilename, ec); -} - -class StorageTest: public ::testing::Test -{ -protected: - StorageTest(){ - File::setFile(testConfigFilename, R"CONFIG( - - . - 2592000 - -)CONFIG"); - std::error_code ec; - fs::remove(testDbFilename, ec); - } - - ~StorageTest(){ - std::error_code ec; - fs::remove(testDbFilename, ec); - fs::remove(testConfigFilename, ec); - } -}; - -TEST_F(StorageTest, create) -{ - ASSERT_TRUE(!fs::exists(testDbFilename)); - - { - Config config(testConfigFilename); - ASSERT_EQ(config.getDataPath(), "."); - Storage storage(config); - } - - ASSERT_TRUE(fs::exists(testDbFilename)); -} - -- cgit v1.2.3