From 97c74006896421f3b51c9475eb2782815ec3d176 Mon Sep 17 00:00:00 2001 From: krunal Date: Wed, 19 Jul 2023 20:43:38 +0530 Subject: [PATCH] adding test for reload on restart --- test/core_api_utils_test.cpp | 4 --- test/stopwords_manager_test.cpp | 64 ++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp index 29ea703a..acb597fd 100644 --- a/test/core_api_utils_test.cpp +++ b/test/core_api_utils_test.cpp @@ -4,13 +4,11 @@ #include #include #include "core_api_utils.h" -#include "stopwords_manager.h" class CoreAPIUtilsTest : public ::testing::Test { protected: Store *store; CollectionManager & collectionManager = CollectionManager::get_instance(); - StopwordsManager& stopwordsManager = StopwordsManager::get_instance(); std::atomic quit = false; std::vector query_fields; @@ -24,8 +22,6 @@ protected: store = new Store(state_dir_path); collectionManager.init(store, 1.0, "auth_key", quit); collectionManager.load(8, 1000); - - stopwordsManager.init(store); } virtual void SetUp() { diff --git a/test/stopwords_manager_test.cpp b/test/stopwords_manager_test.cpp index 784e88c3..7324dade 100644 --- a/test/stopwords_manager_test.cpp +++ b/test/stopwords_manager_test.cpp @@ -379,4 +379,66 @@ TEST_F(StopwordsManagerTest, StopwordsValidation) { ASSERT_STREQ("{\"message\": \"Parameter `stopwords` is required as string array value\"}", res->body.c_str()); collectionManager.drop_collection("coll1"); -} \ No newline at end of file +} + +TEST_F(StopwordsManagerTest, ReloadStopwordsOnRestart) { + nlohmann::json schema = R"({ + "name": "coll1", + "fields": [ + {"name": "title", "type": "string" }, + {"name": "points", "type": "int32" } + ] + })"_json; + + auto op = collectionManager.create_collection(schema); + ASSERT_TRUE(op.ok()); + Collection *coll1 = op.get(); + + auto stopword_value = R"( + {"stopwords": ["Pop", "Indie", "Rock", "Metal", "Folk"], "locale": "en"} + )"_json; + + std::shared_ptr req = std::make_shared(); + std::shared_ptr res = std::make_shared(nullptr); + req->params["collection"] = "coll1"; + req->params["name"] = "genre"; + req->body = stopword_value.dump(); + + auto result = put_upsert_stopword(req, res); + if(!result) { + LOG(ERROR) << res->body; + FAIL(); + } + + auto stopword_config = stopwordsManager.get_stopwords(); + ASSERT_TRUE(stopword_config.find("genre") != stopword_config.end()); + + ASSERT_EQ(5, stopword_config["genre"].size()); + ASSERT_TRUE(stopword_config["genre"].find("pop") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("indie") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("rock") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("metal") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("folk") != stopword_config["genre"].end()); + + //dispose collection manager and reload all stopwords + collectionManager.dispose(); + delete store; + stopword_config.clear(); + + std::string state_dir_path = "/tmp/typesense_test/stopwords_manager"; + system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str()); + store = new Store(state_dir_path); + + collectionManager.init(store, 1.0, "auth_key", quit); + collectionManager.load(8, 1000); + + stopword_config = stopwordsManager.get_stopwords(); + ASSERT_TRUE(stopword_config.find("genre") != stopword_config.end()); + + ASSERT_EQ(5, stopword_config["genre"].size()); + ASSERT_TRUE(stopword_config["genre"].find("pop") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("indie") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("rock") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("metal") != stopword_config["genre"].end()); + ASSERT_TRUE(stopword_config["genre"].find("folk") != stopword_config["genre"].end()); +}