Add test for bad import JSON.

This commit is contained in:
Kishore Nallan 2019-05-26 20:47:57 +05:30
parent 5856263a08
commit d524b08884
3 changed files with 17 additions and 5 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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");
}