[Unit Test] Complete all providers unit tests #12

This commit is contained in:
Scott E. Graves 2025-02-16 08:27:28 -06:00
parent 5cf6948c03
commit 56072da7a6
2 changed files with 53 additions and 31 deletions

View File

@ -43,9 +43,9 @@
#include "utils/time.hpp" #include "utils/time.hpp"
namespace { namespace {
[[nodiscard]] auto [[nodiscard]] auto set_request_path(auto &request,
set_request_path(auto &request, const std::string &object_name)
const std::string &object_name) -> repertory::api_error { -> repertory::api_error {
request.path = object_name; request.path = object_name;
if (request.path.substr(1U).size() > repertory::max_s3_object_name_length) { if (request.path.substr(1U).size() > repertory::max_s3_object_name_length) {
return repertory::api_error::name_too_long; return repertory::api_error::name_too_long;
@ -59,8 +59,9 @@ namespace repertory {
s3_provider::s3_provider(app_config &config, i_http_comm &comm) s3_provider::s3_provider(app_config &config, i_http_comm &comm)
: base_provider(config, comm) {} : base_provider(config, comm) {}
auto s3_provider::add_if_not_found( auto s3_provider::add_if_not_found(api_file &file,
api_file &file, const std::string &object_name) const -> api_error { const std::string &object_name) const
-> api_error {
api_meta_map meta{}; api_meta_map meta{};
auto res{get_item_meta(file.api_path, meta)}; auto res{get_item_meta(file.api_path, meta)};
if (res == api_error::item_not_found) { if (res == api_error::item_not_found) {
@ -88,7 +89,7 @@ auto s3_provider::convert_api_date(std::string_view date) -> std::uint64_t {
1000000UL, 1000000UL,
}; };
struct tm tm1 {}; struct tm tm1{};
#if defined(_WIN32) #if defined(_WIN32)
utils::time::strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1); utils::time::strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1);
return nanos + utils::time::windows_time_t_to_unix_time(_mkgmtime(&tm1)); return nanos + utils::time::windows_time_t_to_unix_time(_mkgmtime(&tm1));
@ -157,8 +158,9 @@ auto s3_provider::create_directory_impl(const std::string &api_path,
utils::path::create_api_path(is_encrypted ? meta[META_KEY] : api_path)); utils::path::create_api_path(is_encrypted ? meta[META_KEY] : api_path));
} }
auto s3_provider::create_directory_paths( auto s3_provider::create_directory_paths(const std::string &api_path,
const std::string &api_path, const std::string &key) const -> api_error { const std::string &key) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
if (api_path == "/") { if (api_path == "/") {
@ -321,8 +323,9 @@ auto s3_provider::get_directory_item_count(const std::string &api_path) const
return 0U; return 0U;
} }
auto s3_provider::get_directory_items_impl( auto s3_provider::get_directory_items_impl(const std::string &api_path,
const std::string &api_path, directory_item_list &list) const -> api_error { directory_item_list &list) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
const auto &cfg{get_s3_config()}; const auto &cfg{get_s3_config()};
@ -420,7 +423,14 @@ auto s3_provider::get_directory_items_impl(
} }
if (response_code == http_error_codes::not_found) { if (response_code == http_error_codes::not_found) {
return api_error::directory_not_found; bool exists{};
auto res = is_file(api_path, exists);
if (res != api_error::success) {
utils::error::raise_api_path_error(
function_name, api_path, res, "failed to determine if file exists");
}
return exists ? api_error::item_exists : api_error::directory_not_found;
} }
if (response_code != http_error_codes::ok) { if (response_code != http_error_codes::ok) {
@ -480,8 +490,8 @@ auto s3_provider::get_directory_items_impl(
return api_error::success; return api_error::success;
} }
auto s3_provider::get_file(const std::string &api_path, auto s3_provider::get_file(const std::string &api_path, api_file &file) const
api_file &file) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -521,8 +531,8 @@ auto s3_provider::get_file(const std::string &api_path,
return api_error::error; return api_error::error;
} }
auto s3_provider::get_file_list(api_file_list &list, auto s3_provider::get_file_list(api_file_list &list, std::string &marker) const
std::string &marker) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -612,8 +622,9 @@ auto s3_provider::get_file_list(api_file_list &list,
return api_error::error; return api_error::error;
} }
auto s3_provider::get_last_modified( auto s3_provider::get_last_modified(bool directory,
bool directory, const std::string &api_path) const -> std::uint64_t { const std::string &api_path) const
-> std::uint64_t {
bool is_encrypted{}; bool is_encrypted{};
std::string object_name; std::string object_name;
head_object_result result{}; head_object_result result{};
@ -623,9 +634,10 @@ auto s3_provider::get_last_modified(
: utils::time::get_time_now(); : utils::time::get_time_now();
} }
auto s3_provider::get_object_info( auto s3_provider::get_object_info(bool directory, const std::string &api_path,
bool directory, const std::string &api_path, bool &is_encrypted, bool &is_encrypted, std::string &object_name,
std::string &object_name, head_object_result &result) const -> api_error { head_object_result &result) const
-> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -685,10 +697,12 @@ auto s3_provider::get_object_info(
return api_error::error; return api_error::error;
} }
auto s3_provider::get_object_list( auto s3_provider::get_object_list(std::string &response_data,
std::string &response_data, long &response_code, long &response_code,
std::optional<std::string> delimiter, std::optional<std::string> prefix, std::optional<std::string> delimiter,
std::optional<std::string> token) const -> bool { std::optional<std::string> prefix,
std::optional<std::string> token) const
-> bool {
curl::requests::http_get get{}; curl::requests::http_get get{};
get.allow_timeout = true; get.allow_timeout = true;
get.aws_service = "aws:amz:" + get_s3_config().region + ":s3"; get.aws_service = "aws:amz:" + get_s3_config().region + ":s3";
@ -716,8 +730,8 @@ auto s3_provider::get_total_drive_space() const -> std::uint64_t {
return std::numeric_limits<std::int64_t>::max() / std::int64_t(2); return std::numeric_limits<std::int64_t>::max() / std::int64_t(2);
} }
auto s3_provider::is_directory(const std::string &api_path, auto s3_provider::is_directory(const std::string &api_path, bool &exists) const
bool &exists) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -745,8 +759,8 @@ auto s3_provider::is_directory(const std::string &api_path,
return api_error::error; return api_error::error;
} }
auto s3_provider::is_file(const std::string &api_path, auto s3_provider::is_file(const std::string &api_path, bool &exists) const
bool &exists) const -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
try { try {
@ -1004,8 +1018,8 @@ auto s3_provider::rename_file(const std::string & /* from_api_path */,
return api_error::not_implemented; return api_error::not_implemented;
} }
auto s3_provider::set_meta_key(const std::string &api_path, auto s3_provider::set_meta_key(const std::string &api_path, api_meta_map &meta)
api_meta_map &meta) -> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
const auto &cfg{get_s3_config()}; const auto &cfg{get_s3_config()};

View File

@ -585,6 +585,14 @@ static void get_directory_items_fails_if_item_is_file(const app_config &cfg,
provider.get_directory_items(api_path, list)); provider.get_directory_items(api_path, list));
EXPECT_TRUE(list.empty()); EXPECT_TRUE(list.empty());
} }
create_file(provider, "/pt01.txt");
directory_item_list list{};
EXPECT_EQ(api_error::item_exists,
provider.get_directory_items("/pt01.txt", list));
EXPECT_EQ(api_error::success, provider.remove_file("/pt01.txt"));
} }
static void get_file(const app_config &cfg, i_provider &provider) { static void get_file(const app_config &cfg, i_provider &provider) {
@ -691,7 +699,7 @@ static void run_tests(const app_config &cfg, i_provider &provider) {
get_directory_items(cfg, provider); get_directory_items(cfg, provider);
get_directory_items_fails_if_directory_not_found(provider); get_directory_items_fails_if_directory_not_found(provider);
// TODO: continue here // TODO continue here
get_directory_items_fails_if_item_is_file(cfg, provider); get_directory_items_fails_if_item_is_file(cfg, provider);
get_directory_item_count(cfg, provider); get_directory_item_count(cfg, provider);