diff --git a/repertory/librepertory/src/db/meta_db.cpp b/repertory/librepertory/src/db/meta_db.cpp index 13763791..dd38bf17 100644 --- a/repertory/librepertory/src/db/meta_db.cpp +++ b/repertory/librepertory/src/db/meta_db.cpp @@ -22,10 +22,11 @@ #include "db/meta_db.hpp" #include "app_config.hpp" +#include "db/rdb_meta_db.hpp" #include "db/sqlite_meta_db.hpp" namespace repertory { auto create_meta_db(const app_config &cfg) -> std::unique_ptr { - return std::make_unique(cfg); + return std::make_unique(cfg); } } // namespace repertory diff --git a/repertory/librepertory/src/db/rdb_meta_db.cpp b/repertory/librepertory/src/db/rdb_meta_db.cpp index 6ae00d15..3cee9336 100644 --- a/repertory/librepertory/src/db/rdb_meta_db.cpp +++ b/repertory/librepertory/src/db/rdb_meta_db.cpp @@ -26,13 +26,16 @@ #include "utils/error_utils.hpp" #include "utils/path.hpp" #include "utils/string.hpp" +#include namespace { -void create_rocksdb( - const repertory::app_config &cfg, const std::string &name, - const std::vector &families, - std::vector &handles, - std::unique_ptr &db) { +[[nodiscard]] auto +create_rocksdb(const repertory::app_config &cfg, const std::string &name, + const std::vector &families, + std::vector &handles) + -> std::unique_ptr { + REPERTORY_USES_FUNCTION_NAME(); + rocksdb::Options options{}; options.create_if_missing = true; options.create_missing_column_families = true; @@ -44,13 +47,12 @@ void create_rocksdb( options, repertory::utils::path::combine(cfg.get_data_directory(), {name}), families, &handles, &ptr); - if (status.ok()) { - db.reset(ptr); - return; + if (not status.ok()) { + repertory::utils::error::raise_error(function_name, status.ToString()); + throw repertory::startup_exception(status.ToString()); } - repertory::utils::error::raise_error(__FUNCTION__, status.ToString()); - throw repertory::startup_exception(status.ToString()); + return std::unique_ptr(ptr); } } // namespace @@ -67,7 +69,7 @@ rdb_meta_db::rdb_meta_db(const app_config &cfg) { families.emplace_back("source", rocksdb::ColumnFamilyOptions()); auto handles = std::vector(); - create_rocksdb(cfg, name, families, handles, db_); + db_ = create_rocksdb(cfg, name, families, handles); std::size_t idx{}; default_family_ = handles[idx++]; diff --git a/repertory/repertory_test/include/fixtures/meta_db_fixture.hpp b/repertory/repertory_test/include/fixtures/meta_db_fixture.hpp new file mode 100644 index 00000000..9c649fd8 --- /dev/null +++ b/repertory/repertory_test/include/fixtures/meta_db_fixture.hpp @@ -0,0 +1,64 @@ +/* + Copyright <2018-2024> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef REPERTORY_TEST_INCLUDE_FIXTURES_META_DB_FIXTURE_HPP +#define REPERTORY_TEST_INCLUDE_FIXTURES_META_DB_FIXTURE_HPP + +#include "test_common.hpp" + +#include "app_config.hpp" +#include "db/i_meta_db.hpp" +#include "db/rdb_meta_db.hpp" +#include "db/sqlite_meta_db.hpp" + +namespace repertory { +template class meta_db_test : public ::testing::Test { +protected: + static std::unique_ptr config; + static std::unique_ptr meta_db; + +protected: + static void SetUpTestCase() { + static std::uint64_t idx{}; + + auto cfg_directory = utils::path::combine(test::get_test_output_dir(), + { + "meta_db_test", + std::to_string(++idx), + }); + config = std::make_unique(provider_type::s3, cfg_directory); + meta_db = std::make_unique(*config); + } + + static void TearDownTestCase() { + meta_db.reset(); + config.reset(); + } +}; + +using meta_db_types = ::testing::Types; + +template std::unique_ptr meta_db_test::config; + +template std::unique_ptr meta_db_test::meta_db; +} // namespace repertory + +#endif // REPERTORY_TEST_INCLUDE_FIXTURES_META_DB_FIXTURE_HPP diff --git a/repertory/repertory_test/src/meta_db_test.cpp b/repertory/repertory_test/src/meta_db_test.cpp new file mode 100644 index 00000000..8d5e2876 --- /dev/null +++ b/repertory/repertory_test/src/meta_db_test.cpp @@ -0,0 +1,54 @@ +/* + Copyright <2018-2024> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#include "fixtures/meta_db_fixture.hpp" + +namespace { +[[nodiscard]] auto create_test_file() -> std::string { + static std::atomic idx{}; + return "/test" + std::to_string(++idx); +} +} // namespace + +namespace repertory { +TYPED_TEST_CASE(meta_db_test, meta_db_types); + +TYPED_TEST(meta_db_test, can_get_api_path_from_source_path) { + auto test_file = create_test_file(); + auto test_source = create_test_file(); + EXPECT_EQ(api_error::success, + this->meta_db->set_item_meta(test_file, META_SOURCE, test_source)); + + std::string api_path; + EXPECT_EQ(api_error::success, + this->meta_db->get_api_path(test_source, api_path)); + EXPECT_STREQ(test_file.c_str(), api_path.c_str()); +} + +TYPED_TEST(meta_db_test, + get_api_path_return_item_not_found_if_source_does_not_exist) { + std::string api_path; + EXPECT_EQ(api_error::item_not_found, + this->meta_db->get_api_path(create_test_file(), api_path)); + EXPECT_TRUE(api_path.empty()); +} +} // namespace repertory