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

Add test for bad import JSON.

Kishore Nalland524b08884c4
diff --git a/include/collection.h b/include/collection.h
index 29b26e07..2128f402 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -81,8 +81,6 @@ public:
~Collection();
- long long int micros = 0;
-
static std::string get_next_seq_id_key(const std::string & collection_name);
static std::string get_meta_key(const std::string & collection_name);
diff --git a/src/collection.cpp b/src/collection.cpp
index 074f52fd..9014c9ff 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -95,6 +95,10 @@ Option<uint32_t> Collection::to_doc(const std::string & json_str, nlohmann::json
return Option<uint32_t>(400, "Bad JSON.");
}
+ if(!document.is_object()) {
+ return Option<uint32_t>(400, "Bad JSON.");
+ }
+
uint32_t seq_id = get_next_seq_id();
std::string seq_id_str = std::to_string(seq_id);
@@ -202,7 +206,6 @@ Option<nlohmann::json> Collection::add_many(const std::string & json_lines_str)
resp["num_imported"] = result.num_indexed;
resp["items"] = nlohmann::json::array();
-
for(const index_result & item: result.items) {
nlohmann::json item_obj;
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 90d528f8..62f71bf9 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -1203,13 +1203,24 @@ TEST_F(CollectionTest, ImportDocuments) {
ASSERT_FALSE(import_response["success"].get<bool>());
ASSERT_EQ(1, import_response["num_imported"].get<int>());
- std::cout << import_response << std::endl;
-
ASSERT_FALSE(import_response["items"][0]["success"].get<bool>());
ASSERT_TRUE(import_response["items"][1]["success"].get<bool>());
ASSERT_STREQ("A document with id id1 already exists.", import_response["items"][0]["error"].get<std::string>().c_str());
+ // handle bad import json
+
+ more_records = std::string("[]");
+ import_res = coll_mul_fields->add_many(more_records);
+ ASSERT_TRUE(import_res.ok());
+
+ import_response = import_res.get();
+
+ ASSERT_FALSE(import_response["success"].get<bool>());
+ ASSERT_EQ(0, import_response["num_imported"].get<int>());
+ ASSERT_EQ(1, import_response["items"].size());
+ ASSERT_STREQ("Bad JSON.", import_response["items"][0]["error"].get<std::string>().c_str());
+
collectionManager.drop_collection("coll_mul_fields");
}