Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix collection manager tests.

Kishore Nallan431fd70fce2b
diff --git a/include/collection_manager.h b/include/collection_manager.h
index ab6f9104..6f102abb 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -19,7 +19,6 @@ private:
uint32_t next_collection_id;
static constexpr const char* COLLECTION_META_PREFIX = "$CM";
- static constexpr const char* NEXT_COLLECTION_ID_KEY = "$CI";
static constexpr const char* COLLECTION_NAME_KEY = "name";
static constexpr const char* COLLECTION_ID_KEY = "id";
@@ -51,4 +50,6 @@ public:
bool drop_collection(std::string collection_name);
uint32_t get_next_collection_id();
+
+ static constexpr const char* NEXT_COLLECTION_ID_KEY = "$CI";
};
\ No newline at end of file
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 060238e9..1af06a21 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -127,7 +127,6 @@ bool CollectionManager::drop_collection(std::string collection_name) {
store->remove(Collection::get_meta_key(collection_name));
store->remove(Collection::get_next_seq_id_key(collection_name));
- store->remove(NEXT_COLLECTION_ID_KEY);
const std::string &collection_id_str = std::to_string(collection->get_collection_id());
rocksdb::Iterator* iter = store->scan(collection_id_str);
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 7409341a..ab8e71cf 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -5,27 +5,39 @@
#include <collection_manager.h>
#include "collection.h"
-TEST(CollectionManagerTest, RestoreRecordsOnRestart) {
- const std::string state_dir_path = "/tmp/typesense_test/coll_manager_test_db";
-
- std::cout << "Truncating and creating: " << state_dir_path << std::endl;
- system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
-
+class CollectionManagerTest : public ::testing::Test {
+protected:
+ Store *store;
CollectionManager & collectionManager = CollectionManager::get_instance();
- Store* store = new Store(state_dir_path);
- collectionManager.init(store);
-
- // Summary: create a collection, add documents, destroy it, try reopening the collection to query
Collection *collection1;
- std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");
- std::vector<field> fields = {field("title", field_types::STRING), field("starring", field_types::STRING)};
- std::vector<std::string> rank_fields = {"points"};
+ std::vector<field> fields;
+ std::vector<std::string> rank_fields;
+
+ void setupCollection() {
+ std::string state_dir_path = "/tmp/typesense_test/coll_manager_test_db";
+ std::cout << "Truncating and creating: " << state_dir_path << std::endl;
+ system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
+
+ store = new Store(state_dir_path);
+ collectionManager.init(store);
- collection1 = collectionManager.get_collection("collection1");
- if(collection1 == nullptr) {
+ fields = {field("title", field_types::STRING), field("starring", field_types::STRING)};
+ rank_fields = {"points"};
collection1 = collectionManager.create_collection("collection1", fields, rank_fields);
}
+ virtual void SetUp() {
+ setupCollection();
+ }
+
+ virtual void TearDown() {
+ collectionManager.drop_collection("collection1");
+ delete store;
+ }
+};
+
+TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
+ std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");
std::string json_line;
while (std::getline(infile, json_line)) {
@@ -35,7 +47,7 @@ TEST(CollectionManagerTest, RestoreRecordsOnRestart) {
infile.close();
std::vector<std::string> search_fields = {"starring", "title"};
- nlohmann::json results = collection1->search("thomas", search_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = collection1->search("thomas", search_fields, {}, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
spp::sparse_hash_map<std::string, field> schema = collection1->get_schema();
@@ -52,33 +64,12 @@ TEST(CollectionManagerTest, RestoreRecordsOnRestart) {
ASSERT_EQ(rank_fields, collection1->get_rank_fields());
ASSERT_EQ(schema.size(), collection1->get_schema().size());
- results = collection1->search("thomas", search_fields, 0, 10, FREQUENCY, false);
+ results = collection1->search("thomas", search_fields, {}, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
-
- delete store;
}
-TEST(CollectionManagerTest, DropCollectionCleanly) {
- const std::string state_dir_path = "/tmp/typesense_test/coll_manager_test_db";
-
- std::cout << "Truncating and creating: " << state_dir_path << std::endl;
- system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
-
- CollectionManager & collectionManager = CollectionManager::get_instance();
- Store* store = new Store(state_dir_path);
- collectionManager.init(store);
-
- // Summary: create a collection, add documents, destroy it, try reopening the collection to query
- Collection *collection1;
+TEST_F(CollectionManagerTest, DropCollectionCleanly) {
std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");
- std::vector<field> fields = {field("title", field_types::STRING), field("starring", field_types::STRING)};
- std::vector<std::string> rank_fields = {"points"};
-
- collection1 = collectionManager.get_collection("collection1");
- if(collection1 == nullptr) {
- collection1 = collectionManager.create_collection("collection1", fields, rank_fields);
- }
-
std::string json_line;
while (std::getline(infile, json_line)) {
@@ -91,7 +82,7 @@ TEST(CollectionManagerTest, DropCollectionCleanly) {
rocksdb::Iterator* it = store->get_iterator();
for (it->SeekToFirst(); it->Valid(); it->Next()) {
- ASSERT_TRUE(false);
+ ASSERT_EQ(it->key().ToString(), "$CI");
}
assert(it->status().ok());
@@ -100,5 +91,4 @@ TEST(CollectionManagerTest, DropCollectionCleanly) {
ASSERT_EQ(1, collectionManager.get_next_collection_id());
delete it;
- delete store;
}
\ No newline at end of file
diff --git a/test/numeric_array_documents.jsonl b/test/numeric_array_documents.jsonl
new file mode 100644
index 00000000..81f7158a
--- /dev/null
+++ b/test/numeric_array_documents.jsonl
@@ -0,0 +1,5 @@
+{"name": "Jeremy Howard", "age": 55, "years": [2014, 2015, 2016], "timestamps": [1390354022, 1421890022, 1453426022]}
+{"name": "Jeremy Howard", "age": 24, "years": [2015, 2016], "timestamps": [1421890022, 1453426022]}
+{"name": "Jeremy Howard", "age": 21, "years": [2016], "timestamps": [1453426022]}
+{"name": "Jeremy Howard", "age": 63, "years": [1981, 1985], "timestamps": [348974822, 475205222]}
+{"name": "Jeremy Howard", "age": 32, "years": [1999, 2000, 2001, 2002], "timestamps": [916968422, 948504422, 980126822, 1011662822]}
\ No newline at end of file