From d62c4fd5213a89ca8a116c5993685c95f11e8a8a Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 22 Nov 2021 18:24:25 +0400 Subject: [PATCH] Testing of HumanReadableNameMapper --- test/meson.build | 1 + test/name_mapper.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 test/name_mapper.cpp diff --git a/test/meson.build b/test/meson.build index 1502c12fb..30b19b8e6 100644 --- a/test/meson.build +++ b/test/meson.build @@ -8,6 +8,7 @@ tests = [ 'kiwixserve', 'book', 'manager', + 'name_mapper', 'opds_catalog', 'reader', 'searcher' diff --git a/test/name_mapper.cpp b/test/name_mapper.cpp new file mode 100644 index 000000000..c6b6d6c3f --- /dev/null +++ b/test/name_mapper.cpp @@ -0,0 +1,109 @@ +#include "../include/name_mapper.h" + +#include "../include/library.h" +#include "../include/manager.h" +#include "gtest/gtest.h" + +namespace +{ + +const char libraryXML[] = R"( + + + + + + + +)"; + +class NameMapperTest : public ::testing::Test { + protected: + void SetUp() override { + kiwix::Manager manager(&lib); + manager.readXml(libraryXML, true, "./library.xml", true); + for ( const std::string& id : lib.getBooksIds() ) { + lib.getBookById(id).setPathValid(true); + } + } + + kiwix::Library lib; +}; + +class CapturedStderr +{ + std::ostringstream buffer; + std::streambuf* const sbuf; +public: + CapturedStderr() + : sbuf(std::cerr.rdbuf()) + { + std::cerr.rdbuf(buffer.rdbuf()); + } + + CapturedStderr(const CapturedStderr&) = delete; + + ~CapturedStderr() + { + std::cerr.rdbuf(sbuf); + } + + operator std::string() const { return buffer.str(); } +}; + +} // unnamed namespace + +TEST_F(NameMapperTest, HumanReadableNameMapperWithoutAliases) +{ + CapturedStderr stderror; + kiwix::HumanReadableNameMapper nm(lib, false); + EXPECT_EQ("", std::string(stderror)); + + EXPECT_EQ("zero_one", nm.getNameForId("01")); + EXPECT_EQ("zero_two", nm.getNameForId("02")); + EXPECT_EQ("zero_three", nm.getNameForId("03")); + EXPECT_EQ("zero_four_2021-10", nm.getNameForId("04-2021-10")); + EXPECT_EQ("zero_four_2021-11", nm.getNameForId("04-2021-11")); + + EXPECT_EQ("01", nm.getIdForName("zero_one")); + EXPECT_EQ("02", nm.getIdForName("zero_two")); + EXPECT_EQ("03", nm.getIdForName("zero_three")); + EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four_2021-10")); + EXPECT_EQ("04-2021-11", nm.getIdForName("zero_four_2021-11")); + EXPECT_THROW(nm.getIdForName("zero_four"), std::out_of_range); + + lib.removeBookById("04-2021-10"); + EXPECT_EQ("zero_four_2021-10", nm.getNameForId("04-2021-10")); + EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four_2021-10")); + EXPECT_THROW(nm.getIdForName("zero_four"), std::out_of_range); +} + +TEST_F(NameMapperTest, HumanReadableNameMapperWithAliases) +{ + CapturedStderr stderror; + kiwix::HumanReadableNameMapper nm(lib, true); + EXPECT_EQ( + "Path collision: /data/zero_four_2021-10.zim and" + " /data/zero_four_2021-11.zim can't share the same URL path 'zero_four'." + " Therefore, only /data/zero_four_2021-10.zim will be served.\n" + , std::string(stderror) + ); + + EXPECT_EQ("zero_one", nm.getNameForId("01")); + EXPECT_EQ("zero_two", nm.getNameForId("02")); + EXPECT_EQ("zero_three", nm.getNameForId("03")); + EXPECT_EQ("zero_four_2021-10", nm.getNameForId("04-2021-10")); + EXPECT_EQ("zero_four_2021-11", nm.getNameForId("04-2021-11")); + + EXPECT_EQ("01", nm.getIdForName("zero_one")); + EXPECT_EQ("02", nm.getIdForName("zero_two")); + EXPECT_EQ("03", nm.getIdForName("zero_three")); + EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four_2021-10")); + EXPECT_EQ("04-2021-11", nm.getIdForName("zero_four_2021-11")); + EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four")); + + lib.removeBookById("04-2021-10"); + EXPECT_EQ("zero_four_2021-10", nm.getNameForId("04-2021-10")); + EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four_2021-10")); + EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four")); +}