From 61765bd621da3deaa2b18f899078232655b0409a Mon Sep 17 00:00:00 2001 From: Justin Hiemstra Date: Wed, 21 Feb 2024 21:54:21 +0000 Subject: [PATCH] Update paths object to return array of path objects --- src/lotman_internal.cpp | 16 +++++++++------- test/main.cpp | 28 +++++++++++++++++++++------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/lotman_internal.cpp b/src/lotman_internal.cpp index 3516128..7db293c 100644 --- a/src/lotman_internal.cpp +++ b/src/lotman_internal.cpp @@ -564,7 +564,7 @@ std::pair lotman::Lot::get_restricting_attribute(const std::s } std::pair lotman::Lot::get_lot_dirs(const bool recursive) { - json path_obj; + json path_arr = json::array(); std::string path_query = "SELECT path, recursive FROM paths WHERE lot_name = ?;"; //std::string recursive_query = "SELECT recursive FROM paths WHERE path = ? AND lot_name = ?;"; std::map> path_query_str_map{{lot_name, {1}}}; @@ -572,14 +572,15 @@ std::pair lotman::Lot::get_lot_dirs(const bool recursive) { if (!rp.second.empty()) { // There was an error std::string int_err = rp.second; std::string ext_err = "Failure on call to SQL_get_matches_multi_col: "; - return std::make_pair(path_obj, ext_err + int_err); + return std::make_pair(path_arr, ext_err + int_err); } for (const auto &path_rec : rp.first) { // the path is at index 0, and the recursion of the path is at index 1 json path_obj_internal; path_obj_internal["lot_name"] = this->lot_name; path_obj_internal["recursive"] = static_cast(std::stoi(path_rec[1])); - path_obj[path_rec[0]] = path_obj_internal; + path_obj_internal["path"] = path_rec[0]; + path_arr.push_back(path_obj_internal); } if (recursive) { // Not recursion of path, but recursion of dirs associated to a lot, ie the directories associated with lot proper's children @@ -587,7 +588,7 @@ std::pair lotman::Lot::get_lot_dirs(const bool recursive) { if (!rp_vec_str.second.empty()) { // There was an error std::string int_err = rp_vec_str.second; std::string ext_err = "Failure to get children."; - return std::make_pair(json(), ext_err+int_err); + return std::make_pair(json::array(), ext_err+int_err); } for (const auto &child : recursive_children) { std::map> child_path_query_str_map{{child.lot_name, {1}}}; @@ -595,19 +596,20 @@ std::pair lotman::Lot::get_lot_dirs(const bool recursive) { if (!rp.second.empty()) { // There was an error std::string int_err = rp.second; std::string ext_err = "Failure on call to SQL_get_matches_multi_col: "; - return std::make_pair(path_obj, ext_err + int_err); + return std::make_pair(path_arr, ext_err + int_err); } for (const auto &path_rec : rp.first) { json path_obj_internal; path_obj_internal["lot_name"] = child.lot_name; path_obj_internal["recursive"] = static_cast(std::stoi(path_rec[1])); - path_obj[path_rec[0]] = path_obj_internal; + path_obj_internal["path"] = path_rec[0]; + path_arr.push_back(path_obj_internal); } } } - return std::make_pair(path_obj, ""); + return std::make_pair(path_arr, ""); } std::pair lotman::Lot::get_lot_from_dir(const std::string dir_path) { diff --git a/test/main.cpp b/test/main.cpp index 40e1785..4abac34 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -461,12 +461,25 @@ namespace { rv = lotman_get_lot_dirs(lot_name, recursive, &output, &err_msg2); ASSERT_TRUE(rv == 0); + std::unique_ptr output_ptr(output, &free); // Wrap output with a unique_ptr json json_out = json::parse(output); - ASSERT_TRUE(json_out["/1/2/3/4"]["lot_name"] == "lot4" && json_out["/1/2/3/4"]["recursive"] == true && - json_out["/345"]["lot_name"] == "lot4" && json_out["/345"]["recursive"] == true && - json_out["/456"]["lot_name"] == "lot5" && json_out["/456"]["recursive"] == false && - json_out["/567"]["lot_name"] == "lot5" && json_out["/567"]["recursive"] == true); - free(output); + for (const auto &path_obj : json_out) { + if (path_obj["path"] == "/1/2/3/4" && path_obj["recursive"] == true && path_obj["lot_name"] == "lot4") { + continue; + } + else if (path_obj["path"] == "/345" && path_obj["recursive"] == true && path_obj["lot_name"] == "lot4") { + continue; + } + else if (path_obj["path"] == "/456" && path_obj["recursive"] == false && path_obj["lot_name"] == "lot5") { + continue; + } + else if (path_obj["path"] == "/567" && path_obj["recursive"] == true && path_obj["lot_name"] == "lot5") { + continue; + } + else { + ASSERT_TRUE(false) << "Unexpected path object: " << output; + } + } } TEST_F(LotManTest, ContextTest) { @@ -603,10 +616,11 @@ namespace { char *err_msg2; rv = lotman_get_lot_as_json("lot3", true, &output, &err_msg); ASSERT_TRUE(rv == 0); + json output_JSON = json::parse(output); free(output); - json expected_output = R"({"children":["lot4","lot5"],"lot_name":"lot3","management_policy_attrs":{"creation_time":{"lot_name":"lot3","value":123.0},"dedicated_GB":{"lot_name":"sep_node","value":3.0},"deletion_time":{"lot_name":"lot3","value":333.0},"expiration_time":{"lot_name":"lot3","value":222.0},"max_num_objects":{"lot_name":"sep_node","value":10.0},"opportunistic_GB":{"lot_name":"lot2","value":1.5}},"owners":["not owner1","owner1"],"parents":["lot1","lot2","sep_node"],"paths":{"/1/2/3/4":{"lot_name":"lot4","recursive":true},"/345":{"lot_name":"lot4","recursive":true},"/456":{"lot_name":"lot5","recursive":false},"/567":{"lot_name":"lot5","recursive":true},"/another/path":{"lot_name":"lot3","recursive":true},"/foo/barr":{"lot_name":"lot3","recursive":true},"/updated/path":{"lot_name":"lot3","recursive":false}},"usage":{"GB_being_written":{"children_contrib":3.4,"self_contrib":0.0},"dedicated_GB":{"children_contrib":8.64,"self_contrib":0.0,"total":8.64},"num_objects":{"children_contrib":10.0,"self_contrib":0.0},"objects_being_written":{"children_contrib":7.0,"self_contrib":0.0},"opportunistic_GB":{"children_contrib":0.0,"self_contrib":0.0,"total":0.0},"total_GB":{"children_contrib":8.64,"self_contrib":0.0}}})"_json; - ASSERT_TRUE(output_JSON == expected_output); + json expected_output = R"({"children":["lot4","lot5"],"lot_name":"lot3","management_policy_attrs":{"creation_time":{"lot_name":"lot3","value":123.0},"dedicated_GB":{"lot_name":"sep_node","value":3.0},"deletion_time":{"lot_name":"lot3","value":333.0},"expiration_time":{"lot_name":"lot3","value":222.0},"max_num_objects":{"lot_name":"sep_node","value":10.0},"opportunistic_GB":{"lot_name":"lot2","value":1.5}},"owners":["not owner1","owner1"],"parents":["lot1","lot2","sep_node"],"paths":[{"lot_name":"lot3","path":"/another/path","recursive":true},{"lot_name":"lot3","path":"/updated/path","recursive":false},{"lot_name":"lot3","path":"/foo/barr","recursive":true},{"lot_name":"lot4","path":"/1/2/3/4","recursive":true},{"lot_name":"lot4","path":"/345","recursive":true},{"lot_name":"lot5","path":"/456","recursive":false},{"lot_name":"lot5","path":"/567","recursive":true}],"usage":{"GB_being_written":{"children_contrib":3.4,"self_contrib":0.0},"dedicated_GB":{"children_contrib":8.64,"self_contrib":0.0,"total":8.64},"num_objects":{"children_contrib":10.0,"self_contrib":0.0},"objects_being_written":{"children_contrib":7.0,"self_contrib":0.0},"opportunistic_GB":{"children_contrib":0.0,"self_contrib":0.0,"total":0.0},"total_GB":{"children_contrib":8.64,"self_contrib":0.0}}})"_json; + ASSERT_TRUE(output_JSON == expected_output) << output_JSON; } TEST_F(LotManTest, LotsFromDirTest) {