meta db unit tests and fixes
This commit is contained in:
		| @@ -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<i_meta_db> { | ||||
|   return std::make_unique<sqlite_meta_db>(cfg); | ||||
|   return std::make_unique<rdb_meta_db>(cfg); | ||||
| } | ||||
| } // namespace repertory | ||||
|   | ||||
| @@ -26,13 +26,16 @@ | ||||
| #include "utils/error_utils.hpp" | ||||
| #include "utils/path.hpp" | ||||
| #include "utils/string.hpp" | ||||
| #include <memory> | ||||
|  | ||||
| namespace { | ||||
| void create_rocksdb( | ||||
|     const repertory::app_config &cfg, const std::string &name, | ||||
| [[nodiscard]] auto | ||||
| create_rocksdb(const repertory::app_config &cfg, const std::string &name, | ||||
|                const std::vector<rocksdb::ColumnFamilyDescriptor> &families, | ||||
|     std::vector<rocksdb::ColumnFamilyHandle *> &handles, | ||||
|     std::unique_ptr<rocksdb::DB> &db) { | ||||
|                std::vector<rocksdb::ColumnFamilyHandle *> &handles) | ||||
|     -> std::unique_ptr<rocksdb::DB> { | ||||
|   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<rocksdb::DB>(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<rocksdb::ColumnFamilyHandle *>(); | ||||
|     create_rocksdb(cfg, name, families, handles, db_); | ||||
|     db_ = create_rocksdb(cfg, name, families, handles); | ||||
|  | ||||
|     std::size_t idx{}; | ||||
|     default_family_ = handles[idx++]; | ||||
|   | ||||
| @@ -0,0 +1,64 @@ | ||||
| /* | ||||
|   Copyright <2018-2024> <scott.e.graves@protonmail.com> | ||||
|  | ||||
|   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 <typename db_t> class meta_db_test : public ::testing::Test { | ||||
| protected: | ||||
|   static std::unique_ptr<app_config> config; | ||||
|   static std::unique_ptr<db_t> 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<app_config>(provider_type::s3, cfg_directory); | ||||
|     meta_db = std::make_unique<db_t>(*config); | ||||
|   } | ||||
|  | ||||
|   static void TearDownTestCase() { | ||||
|     meta_db.reset(); | ||||
|     config.reset(); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| using meta_db_types = ::testing::Types<rdb_meta_db, sqlite_meta_db>; | ||||
|  | ||||
| template <typename db_t> std::unique_ptr<app_config> meta_db_test<db_t>::config; | ||||
|  | ||||
| template <typename db_t> std::unique_ptr<db_t> meta_db_test<db_t>::meta_db; | ||||
| } // namespace repertory | ||||
|  | ||||
| #endif // REPERTORY_TEST_INCLUDE_FIXTURES_META_DB_FIXTURE_HPP | ||||
							
								
								
									
										54
									
								
								repertory/repertory_test/src/meta_db_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								repertory/repertory_test/src/meta_db_test.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| /* | ||||
|   Copyright <2018-2024> <scott.e.graves@protonmail.com> | ||||
|  | ||||
|   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<std::uint64_t> 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 | ||||
		Reference in New Issue
	
	Block a user