From 038405824c663beeef1becbf9b4fcf2b1e4661ee Mon Sep 17 00:00:00 2001 From: Harpreet Sangar Date: Mon, 31 Jul 2023 14:37:01 +0530 Subject: [PATCH] Fix `Collection::get_reference_field`, return field name of current schema. --- src/collection.cpp | 2 +- test/collection_join_test.cpp | 100 +++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/src/collection.cpp b/src/collection.cpp index 26d93944..f7b8eea1 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -2711,7 +2711,7 @@ Option Collection::get_reference_field(const std::string & collecti for (auto const& pair: reference_fields) { auto reference_pair = pair.second; if (reference_pair.collection == collection_name) { - reference_field_name = reference_pair.field; + reference_field_name = pair.first; break; } } diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp index 0d1cd5b5..68722bac 100644 --- a/test/collection_join_test.cpp +++ b/test/collection_join_test.cpp @@ -1436,19 +1436,19 @@ TEST_F(CollectionJoinTest, CascadeDeletion) { R"({ "name": "Products", "fields": [ - {"name": "product_id", "type": "string"}, + {"name": "product_idx", "type": "string"}, {"name": "product_name", "type": "string", "infix": true}, {"name": "product_description", "type": "string"} ] })"_json; std::vector documents = { R"({ - "product_id": "product_a", + "product_idx": "product_a", "product_name": "shampoo", "product_description": "Our new moisturizing shampoo is perfect for those with dry or damaged hair." })"_json, R"({ - "product_id": "product_b", + "product_idx": "product_b", "product_name": "soap", "product_description": "Introducing our all-natural, organic soap bar made with essential oils and botanical ingredients." })"_json @@ -1463,36 +1463,56 @@ TEST_F(CollectionJoinTest, CascadeDeletion) { schema_json = R"({ - "name": "Customers", + "name": "Users", "fields": [ - {"name": "customer_id", "type": "string"}, - {"name": "customer_name", "type": "string"}, - {"name": "product_price", "type": "float"}, - {"name": "product_id", "type": "string", "reference": "Products.product_id"} + {"name": "user_id", "type": "string"}, + {"name": "user_name", "type": "string"} ] })"_json; documents = { R"({ - "customer_id": "customer_a", - "customer_name": "Joe", + "user_id": "user_a", + "user_name": "Joe" + })"_json, + R"({ + "user_id": "user_b", + "user_name": "Dan" + })"_json, + }; + collection_create_op = collectionManager.create_collection(schema_json); + ASSERT_TRUE(collection_create_op.ok()); + for (auto const &json: documents) { + auto add_op = collection_create_op.get()->add(json.dump()); + ASSERT_TRUE(add_op.ok()); + } + + schema_json = + R"({ + "name": "CustomerProductPrices", + "fields": [ + {"name": "product_price", "type": "float"}, + {"name": "user_id", "type": "string", "reference": "Users.user_id"}, + {"name": "product_id", "type": "string", "reference": "Products.product_idx"} + ] + })"_json; + documents = { + R"({ + "user_id": "user_a", "product_price": 143, "product_id": "product_a" })"_json, R"({ - "customer_id": "customer_a", - "customer_name": "Joe", + "user_id": "user_a", "product_price": 73.5, "product_id": "product_b" })"_json, R"({ - "customer_id": "customer_b", - "customer_name": "Dan", + "user_id": "user_b", "product_price": 75, "product_id": "product_a" })"_json, R"({ - "customer_id": "customer_b", - "customer_name": "Dan", + "user_id": "user_b", "product_price": 140, "product_id": "product_b" })"_json @@ -1503,9 +1523,12 @@ TEST_F(CollectionJoinTest, CascadeDeletion) { auto add_op = collection_create_op.get()->add(json.dump()); ASSERT_TRUE(add_op.ok()); } + std::map req_params = { - {"collection", "Customers"}, + {"collection", "Products"}, {"q", "*"}, + {"filter_by", "$CustomerProductPrices(user_id:= user_a)"}, + {"include_fields", "$CustomerProductPrices(product_price)"} }; nlohmann::json embedded_params; std::string json_res; @@ -1513,8 +1536,20 @@ TEST_F(CollectionJoinTest, CascadeDeletion) { std::chrono::system_clock::now().time_since_epoch()).count(); auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); + ASSERT_TRUE(search_op.ok()); nlohmann::json res_obj = nlohmann::json::parse(json_res); + ASSERT_EQ(2, res_obj["found"].get()); + ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_idx")); + ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_idx")); + + req_params = { + {"collection", "CustomerProductPrices"}, + {"q", "*"}, + }; + search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); + + res_obj = nlohmann::json::parse(json_res); ASSERT_EQ(4, res_obj["found"].get()); ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id")); ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_id")); @@ -1529,8 +1564,8 @@ TEST_F(CollectionJoinTest, CascadeDeletion) { res_obj = nlohmann::json::parse(json_res); ASSERT_EQ(2, res_obj["found"].get()); - ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id")); - ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_id")); + ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_idx")); + ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_idx")); collectionManager.get_collection_unsafe("Products")->remove("0"); @@ -1542,10 +1577,10 @@ TEST_F(CollectionJoinTest, CascadeDeletion) { res_obj = nlohmann::json::parse(json_res); ASSERT_EQ(1, res_obj["found"].get()); - ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id")); + ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_idx")); req_params = { - {"collection", "Customers"}, + {"collection", "CustomerProductPrices"}, {"q", "*"}, }; search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); @@ -1554,4 +1589,27 @@ TEST_F(CollectionJoinTest, CascadeDeletion) { ASSERT_EQ(2, res_obj["found"].get()); ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id")); ASSERT_EQ("product_b", res_obj["hits"][1]["document"].at("product_id")); + + collectionManager.get_collection_unsafe("Users")->remove("1"); + + req_params = { + {"collection", "Users"}, + {"q", "*"}, + }; + search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); + + res_obj = nlohmann::json::parse(json_res); + ASSERT_EQ(1, res_obj["found"].get()); + ASSERT_EQ("user_a", res_obj["hits"][0]["document"].at("user_id")); + + req_params = { + {"collection", "CustomerProductPrices"}, + {"q", "*"}, + }; + search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); + + res_obj = nlohmann::json::parse(json_res); + ASSERT_EQ(1, res_obj["found"].get()); + ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id")); + ASSERT_EQ("user_a", res_obj["hits"][0]["document"].at("user_id")); }