mirror of
https://github.com/typesense/typesense.git
synced 2025-05-17 04:02:36 +08:00
Add test for bad import JSON.
This commit is contained in:
parent
5856263a08
commit
d524b08884
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user