diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile | 9 | ||||
-rw-r--r-- | tests/test-config.cpp (renamed from tests/unittests.cpp) | 37 | ||||
-rw-r--r-- | tests/test-storage.cpp | 178 |
3 files changed, 186 insertions, 38 deletions
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/unittests.cpp b/tests/test-config.cpp index 3b24f83..065dedf 100644 --- a/tests/unittests.cpp +++ b/tests/test-config.cpp @@ -6,7 +6,6 @@ #include "config.h" #include "file.h" -#include "storage.h" namespace fs = std::filesystem; @@ -34,6 +33,7 @@ TEST_F(ConfigTest, defaultData) { Config config{filename}; EXPECT_EQ(config.getDataPath(), "/var/lib/whiteboard"); + EXPECT_EQ(config.getMaxage(), 0); ASSERT_TRUE(!fs::exists(filename)); } @@ -52,43 +52,10 @@ TEST_F(ConfigTest, testData) { Config config{testConfigFilename}; EXPECT_EQ(config.getDataPath(), "/some/other/location"); + EXPECT_EQ(config.getMaxage(), 2592000); } std::error_code ec; fs::remove(testConfigFilename, ec); } -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); - } - - ~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)); -} - 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); +} + |