From d524b08884c489c1ecda602bf6449e1c9cc31a55 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Sun, 26 May 2019 20:47:57 +0530 Subject: [PATCH] Add test for bad import JSON. --- include/collection.h | 2 -- src/collection.cpp | 5 ++++- test/collection_test.cpp | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) 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 Collection::to_doc(const std::string & json_str, nlohmann::json return Option(400, "Bad JSON."); } + if(!document.is_object()) { + return Option(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 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()); ASSERT_EQ(1, import_response["num_imported"].get()); - std::cout << import_response << std::endl; - ASSERT_FALSE(import_response["items"][0]["success"].get()); ASSERT_TRUE(import_response["items"][1]["success"].get()); ASSERT_STREQ("A document with id id1 already exists.", import_response["items"][0]["error"].get().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()); + ASSERT_EQ(0, import_response["num_imported"].get()); + ASSERT_EQ(1, import_response["items"].size()); + ASSERT_STREQ("Bad JSON.", import_response["items"][0]["error"].get().c_str()); + collectionManager.drop_collection("coll_mul_fields"); }