add collection check and test

This commit is contained in:
krunal 2023-12-12 15:58:04 +05:30
parent 2ecd42795e
commit 54ac166ee1
4 changed files with 62 additions and 3 deletions

View File

@ -396,6 +396,8 @@ public:
std::vector<field> get_fields();
bool contains_field(const std::string&);
std::unordered_map<std::string, field> get_dynamic_fields();
tsl::htrie_map<char, field> get_schema();

View File

@ -113,9 +113,13 @@ Option<bool> AnalyticsManager::create_queries_index(nlohmann::json &payload, boo
return Option<bool>(400, "There's already another configuration for this destination collection.");
}
auto coll_schema = CollectionManager::get_instance().get_collection(suggestion_collection)->get_schema();
if(coll_schema.find(counter_field) == coll_schema.end()) {
return Option<bool>(404, "counter_field not found in destination collection.");
auto coll = CollectionManager::get_instance().get_collection(suggestion_collection).get();
if(coll != nullptr) {
if (!coll->contains_field(counter_field)) {
return Option<bool>(404, "counter_field `" + counter_field + "` not found in destination collection.");
}
} else {
return Option<bool>(404, "Collection `" + suggestion_collection + "` not found.");
}
}

View File

@ -4336,6 +4336,11 @@ std::vector<field> Collection::get_fields() {
return fields;
}
bool Collection::contains_field(const std::string &field) {
std::shared_lock lock(mutex);
return search_schema.find(field) != search_schema.end();
}
std::unordered_map<std::string, field> Collection::get_dynamic_fields() {
std::shared_lock lock(mutex);
return dynamic_fields;

View File

@ -893,4 +893,52 @@ TEST_F(AnalyticsManagerTest, PopularityScore) {
ASSERT_EQ("1", results["hits"][1]["document"]["id"]);
ASSERT_EQ(1, results["hits"][1]["document"]["popularity"]);
ASSERT_EQ("Funky trousers", results["hits"][1]["document"]["title"]);
}
TEST_F(AnalyticsManagerTest, PopularityScoreValidation) {
nlohmann::json products_schema = R"({
"name": "books",
"fields": [
{"name": "title", "type": "string"},
{"name": "popularity", "type": "int32"}
]
})"_json;
Collection* products_coll = collectionManager.create_collection(products_schema).get();
nlohmann::json analytics_rule = R"({
"name": "books_popularity",
"type": "popular_clicks",
"params": {
"source": {
"collections": ["books"]
},
"destination": {
"collection": "popular_books",
"counter_field": "popularity"
}
}
})"_json;
auto create_op = analyticsManager.create_rule(analytics_rule, false, true);
ASSERT_FALSE(create_op.ok());
ASSERT_EQ("Collection `popular_books` not found.", create_op.error());
analytics_rule = R"({
"name": "books_popularity",
"type": "popular_clicks",
"params": {
"source": {
"collections": ["books"]
},
"destination": {
"collection": "books",
"counter_field": "popularity_score"
}
}
})"_json;
create_op = analyticsManager.create_rule(analytics_rule, false, true);
ASSERT_FALSE(create_op.ok());
ASSERT_EQ("counter_field `popularity_score` not found in destination collection.", create_op.error());
}