Add simple unit test for optional values
This commit is contained in:
parent
2d724ec4e3
commit
e9a8568373
@ -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/")
|
||||
|
||||
100
test/issue-25-default-values.cpp
Normal file
100
test/issue-25-default-values.cpp
Normal file
@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json-schema.hpp>
|
||||
|
||||
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<std::string>() != "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<std::string>();
|
||||
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<std::string>() != "Abbey Road") {
|
||||
std::cerr << "Patch with defaults contains wrong value" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user