From 987fca8f797d47c9a8c718d0a7224507fa4be754 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 16 Feb 2025 09:30:12 -0600 Subject: [PATCH] [Unit Test] Complete all providers unit tests #12 --- .../src/providers/s3/s3_provider.cpp | 14 +++++++++- .../src/providers/sia/sia_provider.cpp | 28 ++++++++++++++----- .../repertory_test/src/providers_test.cpp | 26 ++++++++++++++++- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/repertory/librepertory/src/providers/s3/s3_provider.cpp b/repertory/librepertory/src/providers/s3/s3_provider.cpp index 73c77ff6..6307b8fb 100644 --- a/repertory/librepertory/src/providers/s3/s3_provider.cpp +++ b/repertory/librepertory/src/providers/s3/s3_provider.cpp @@ -495,7 +495,19 @@ auto s3_provider::get_file(const std::string &api_path, api_file &file) const get_object_info(false, api_path, is_encrypted, object_name, result), }; if (res != api_error::success) { - return res; + if (res != api_error::item_not_found) { + return res; + } + + bool exists{}; + res = is_directory(api_path, exists); + if (res != api_error::success) { + utils::error::raise_api_path_error( + function_name, api_path, res, + "failed to determine if directory exists"); + } + + return exists ? api_error::directory_exists : api_error::item_not_found; } file.api_path = api_path; diff --git a/repertory/librepertory/src/providers/sia/sia_provider.cpp b/repertory/librepertory/src/providers/sia/sia_provider.cpp index 91b4ef27..a3a6db1d 100644 --- a/repertory/librepertory/src/providers/sia/sia_provider.cpp +++ b/repertory/librepertory/src/providers/sia/sia_provider.cpp @@ -206,16 +206,30 @@ auto sia_provider::get_file(const std::string &api_path, api_file &file) const json file_data{}; auto res{get_object_info(api_path, file_data)}; if (res != api_error::success) { - return res; + if (res != api_error::item_not_found) { + return res; + } + + bool exists{}; + res = is_directory(api_path, exists); + if (res != api_error::success) { + utils::error::raise_api_path_error( + function_name, api_path, res, + "failed to determine if directory exists"); + } + + return exists ? api_error::directory_exists : api_error::item_not_found; } - auto slabs{file_data["object"]["Slabs"]}; auto size{ - std::accumulate( - slabs.begin(), slabs.end(), std::uint64_t(0U), - [](auto &&total_size, const json &slab) -> std::uint64_t { - return total_size + slab["Length"].get(); - }), + file_data["object"].contains("Slabs") + ? std::accumulate( + file_data["object"]["Slabs"].begin(), + file_data["object"]["Slabs"].end(), std::uint64_t(0U), + [](auto &&total_size, const json &slab) -> std::uint64_t { + return total_size + slab["Length"].get(); + }) + : file_data["object"]["size"].get(), }; api_meta_map meta{}; diff --git a/repertory/repertory_test/src/providers_test.cpp b/repertory/repertory_test/src/providers_test.cpp index 4088ebd1..e99ebd29 100644 --- a/repertory/repertory_test/src/providers_test.cpp +++ b/repertory/repertory_test/src/providers_test.cpp @@ -640,6 +640,24 @@ static void get_file(const app_config &cfg, i_provider &provider) { EXPECT_STREQ(source_path.c_str(), file.source_path.c_str()); return; } + + create_file(provider, "/pt01.txt"); + + api_file file{}; + EXPECT_EQ(api_error::success, provider.get_file("/pt01.txt", file)); + + EXPECT_STREQ("/pt01.txt", file.api_path.c_str()); + EXPECT_STREQ("/", file.api_parent.c_str()); + EXPECT_LT(utils::time::get_time_now() - (utils::time::NANOS_PER_SECOND * 5U), + file.accessed_date); + EXPECT_LT(utils::time::get_time_now() - (utils::time::NANOS_PER_SECOND * 5U), + file.changed_date); + EXPECT_LT(utils::time::get_time_now() - (utils::time::NANOS_PER_SECOND * 5U), + file.creation_date); + EXPECT_LT(utils::time::get_time_now() - (utils::time::NANOS_PER_SECOND * 5U), + file.modified_date); + + EXPECT_EQ(api_error::success, provider.remove_file("/pt01.txt")); } static void get_file_fails_if_file_not_found(i_provider &provider) { @@ -667,6 +685,13 @@ static void get_file_fails_if_item_is_directory(const app_config &cfg, EXPECT_EQ(api_error::directory_exists, provider.get_file(api_path, file)); return; } + + create_directory(provider, "/dir01"); + + api_file file{}; + EXPECT_EQ(api_error::directory_exists, provider.get_file("/dir01", file)); + + EXPECT_EQ(api_error::success, provider.remove_directory("/dir01")); } static void get_file_list(const app_config &cfg, i_provider &provider) { @@ -723,7 +748,6 @@ static void run_tests(const app_config &cfg, i_provider &provider) { get_directory_item_count(cfg, provider); - // TODO continue here get_file(cfg, provider); get_file_fails_if_file_not_found(provider); get_file_fails_if_item_is_directory(cfg, provider);