From e9a8568373c5908e21d76f64fe31ca881fdfb0f3 Mon Sep 17 00:00:00 2001 From: Sven Fink Date: Tue, 3 Mar 2020 12:46:50 +0100 Subject: [PATCH] Add simple unit test for optional values --- test/CMakeLists.txt | 4 ++ test/issue-25-default-values.cpp | 100 +++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 test/issue-25-default-values.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9ca65d7..3371cb9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -35,6 +35,10 @@ add_executable(issue-70-root-schema-constructor issue-70-root-schema-constructor target_link_libraries(issue-70-root-schema-constructor nlohmann_json_schema_validator) add_test(NAME issue-70-root-schema-constructor COMMAND issue-70-root-schema-constructor) +add_executable(issue-25-default-values issue-25-default-values.cpp) +target_link_libraries(issue-25-default-values nlohmann_json_schema_validator) +add_test(NAME issue-25-default-values COMMAND issue-25-default-values) + # Unit test for string format checks add_executable("string-format-check-test" "string-format-check-test.cpp") target_include_directories("string-format-check-test" PRIVATE "${PROJECT_SOURCE_DIR}/src/") diff --git a/test/issue-25-default-values.cpp b/test/issue-25-default-values.cpp new file mode 100644 index 0000000..f775281 --- /dev/null +++ b/test/issue-25-default-values.cpp @@ -0,0 +1,100 @@ +#include +#include + +using nlohmann::json; +using nlohmann::json_schema::json_validator; + +static const json person_schema = R"( +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "A person", + "properties": { + "name": { + "description": "Name", + "type": "string" + }, + "age": { + "description": "Age of the person", + "type": "number", + "minimum": 2, + "maximum": 200 + }, + "address":{ + "type": "object", + "properties":{ + "street":{ + "type": "string", + "default": "Abbey Road" + } + } + } + }, + "required": [ + "name", + "age" + ], + "additionalProperties": false, + "type": "object" +})"_json; + +int main(void) +{ + json_validator validator{}; + + // add address which is optional that should generate a diff containing a default street + json person_missing_address = R"({ + "name": "Hans", + "age": 69, + "address": {} +})"_json; + + validator.set_root_schema(person_schema); + + const auto default_patch = validator.validate(person_missing_address); + + if (!default_patch.is_array()) { + std::cerr << "Patch with defaults is expected to be an array" << std::endl; + return 1; + } + + if (default_patch.size() != 1) { + std::cerr << "Patch with defaults is expected to contain one opperation" << std::endl; + return 1; + } + + const auto &single_op = default_patch[0]; + + if (!single_op.contains("op")) { + std::cerr << "Patch with defaults is expected to contain opperation entry" << std::endl; + return 1; + } + + if (single_op["op"].get() != "add") { + std::cerr << "Patch with defaults is expected to contain add opperation" << std::endl; + return 1; + } + + if (!single_op.contains("path")) { + std::cerr << "Patch with defaults is expected to contain a path" << std::endl; + return 1; + } + + const auto &readPath = single_op["path"].get(); + if (readPath != "/address/street") { + std::cerr << "Patch with defaults contains wrong path. It is " << readPath << " and should be " + << "/address/street" << std::endl; + return 1; + } + + if (!single_op.contains("value")) { + std::cerr << "Patch with defaults is expected to contain a value" << std::endl; + return 1; + } + + if (single_op["value"].get() != "Abbey Road") { + std::cerr << "Patch with defaults contains wrong value" << std::endl; + return 1; + } + + return 0; +}