1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
import json
SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema",
"properties": {
"title": {
"type": "string",
"default": "Untitled",
},
},
"required": ["title"],
}
VALID_DOC = {
"title": "doc one",
}
INVALID_DOC = {"title": {"foo": "bar"}}
MISSING_FIELD_DOC = {}
def test_run_with_fill_defaults_does_not_make_valid_doc_invalid(
run_line_simple, tmp_path
):
schemafile = tmp_path / "schema.json"
schemafile.write_text(json.dumps(SCHEMA))
doc = tmp_path / "instance.json"
doc.write_text(json.dumps(VALID_DOC))
run_line_simple(["--fill-defaults", "--schemafile", str(schemafile), str(doc)])
def test_run_with_fill_defaults_does_not_make_invalid_doc_valid(run_line, tmp_path):
schemafile = tmp_path / "schema.json"
schemafile.write_text(json.dumps(SCHEMA))
doc = tmp_path / "instance.json"
doc.write_text(json.dumps(INVALID_DOC))
res = run_line(
[
"check-jsonschema",
"--fill-defaults",
"--schemafile",
str(schemafile),
str(doc),
]
)
assert res.exit_code == 1
def test_run_with_fill_defaults_adds_required_field(run_line, tmp_path):
schemafile = tmp_path / "schema.json"
schemafile.write_text(json.dumps(SCHEMA))
doc = tmp_path / "instance.json"
doc.write_text(json.dumps(MISSING_FIELD_DOC))
# step 1: run without '--fill-defaults' and confirm failure
result_without_fill_defaults = run_line(
[
"check-jsonschema",
"--schemafile",
str(schemafile),
str(doc),
]
)
assert result_without_fill_defaults.exit_code == 1
# step 2: run with '--fill-defaults' and confirm success
result_with_fill_defaults = run_line(
[
"check-jsonschema",
"--fill-defaults",
"--schemafile",
str(schemafile),
str(doc),
]
)
assert result_with_fill_defaults.exit_code == 0
|