From af1c4ee4d74ff7afc997372802d851d11daad418 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 30 Dec 2022 15:07:39 +0100 Subject: Added tests, added sqlite-backed storage (WIP!) --- tests/Makefile | 26 +++++++++++++++ tests/unittests.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/unittests.cpp (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..78097ce --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,26 @@ +include ../common.mk + +CXXFLAGS+=\ + -I/usr/src/googletest/googletest/include \ + -I/usr/src/googletest/googlemock/include \ + -I/usr/src/googletest/googletest \ + -I/usr/src/googletest/googlemock \ + -I.. + +test: unittests + ./unittests + +unittests: libgmock.a unittests.o ../config.o ../file.o ../storage.o + $(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@ + +unittests.o: unittests.cpp + $(CXX) $(CXXFLAGS) -o $@ -c unittests.cpp + +libgmock.a: + $(CXX) $(CXXFLAGS) -c /usr/src/googletest/googletest/src/gtest-all.cc + $(CXX) $(CXXFLAGS) -c /usr/src/googletest/googlemock/src/gmock-all.cc + $(CXX) $(CXXFLAGS) -c /usr/src/googletest/googlemock/src/gmock_main.cc + ar -rv libgmock.a gmock-all.o gtest-all.o gmock_main.o + +clean: + -rm -f *.o *.a unittests diff --git a/tests/unittests.cpp b/tests/unittests.cpp new file mode 100644 index 0000000..3b24f83 --- /dev/null +++ b/tests/unittests.cpp @@ -0,0 +1,94 @@ +#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