-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtest_nonjson_schema_handling.py
More file actions
173 lines (143 loc) · 5.55 KB
/
Copy pathtest_nonjson_schema_handling.py
File metadata and controls
173 lines (143 loc) · 5.55 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
import pytest
import responses
from check_jsonschema.parsers.json5 import ENABLED as JSON5_ENABLED
SIMPLE_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema",
"properties": {
"title": {"type": "string"},
},
"additionalProperties": False,
}
YAML_REF_MAIN_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema",
"properties": {
"title": {"$ref": "./title_schema.yaml"},
},
"additionalProperties": False,
}
JSON5_REF_MAIN_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema",
"properties": {
"title": {"$ref": "./title_schema.json5"},
},
"additionalProperties": False,
}
TITLE_SCHEMA = {"type": "string"}
PASSING_DOCUMENT = {"title": "doc one"}
FAILING_DOCUMENT = {"title": 2}
@pytest.mark.parametrize("passing_data", [True, False])
def test_yaml_reference(run_line, tmp_path, passing_data):
main_schemafile = tmp_path / "main_schema.json"
main_schemafile.write_text(json.dumps(YAML_REF_MAIN_SCHEMA))
# JSON is a subset of YAML, so this works for generated YAML
ref_schema = tmp_path / "title_schema.yaml"
ref_schema.write_text(json.dumps(TITLE_SCHEMA))
doc = tmp_path / "doc.json"
if passing_data:
doc.write_text(json.dumps(PASSING_DOCUMENT))
else:
doc.write_text(json.dumps(FAILING_DOCUMENT))
result = run_line(
["check-jsonschema", "--schemafile", str(main_schemafile), str(doc)]
)
assert result.exit_code == (0 if passing_data else 1)
@pytest.mark.skipif(not JSON5_ENABLED, reason="test requires json5")
@pytest.mark.parametrize("passing_data", [True, False])
def test_json5_reference(run_line, tmp_path, passing_data):
main_schemafile = tmp_path / "main_schema.json"
main_schemafile.write_text(json.dumps(JSON5_REF_MAIN_SCHEMA))
ref_schema = tmp_path / "title_schema.json5"
ref_schema.write_text(json.dumps(TITLE_SCHEMA))
doc = tmp_path / "doc.json"
if passing_data:
doc.write_text(json.dumps(PASSING_DOCUMENT))
else:
doc.write_text(json.dumps(FAILING_DOCUMENT))
result = run_line(
["check-jsonschema", "--schemafile", str(main_schemafile), str(doc)]
)
assert result.exit_code == (0 if passing_data else 1)
@pytest.mark.skipif(not JSON5_ENABLED, reason="test requires json5")
@pytest.mark.parametrize("passing_data", [True, False])
def test_can_load_json5_schema(run_line, tmp_path, passing_data):
# dump JSON to the JSON5 file, this is fine
main_schemafile = tmp_path / "main_schema.json5"
main_schemafile.write_text(json.dumps(SIMPLE_SCHEMA))
doc = tmp_path / "doc.json"
if passing_data:
doc.write_text(json.dumps(PASSING_DOCUMENT))
else:
doc.write_text(json.dumps(FAILING_DOCUMENT))
result = run_line(
["check-jsonschema", "--schemafile", str(main_schemafile), str(doc)]
)
assert result.exit_code == (0 if passing_data else 1)
@pytest.mark.parametrize("passing_data", [True, False])
def test_can_load_remote_yaml_schema(run_line, tmp_path, passing_data):
retrieval_uri = "https://example.org/retrieval/schemas/main.yaml"
responses.add(
"GET",
retrieval_uri,
body="""\
"$schema": "http://json-schema.org/draft-07/schema"
properties:
title: {"type": "string"}
additionalProperties: false
""",
)
doc = tmp_path / "doc.json"
doc.write_text(json.dumps(PASSING_DOCUMENT if passing_data else FAILING_DOCUMENT))
result = run_line(["check-jsonschema", "--schemafile", retrieval_uri, str(doc)])
assert result.exit_code == (0 if passing_data else 1)
@pytest.mark.parametrize("passing_data", [True, False])
def test_can_load_remote_yaml_schema_ref(run_line, tmp_path, passing_data):
retrieval_uri = "https://example.org/retrieval/schemas/main.yaml"
responses.add(
"GET",
retrieval_uri,
body="""\
"$schema": "http://json-schema.org/draft-07/schema"
properties:
"title": {"$ref": "./title_schema.yaml"}
additionalProperties: false
""",
)
responses.add(
"GET",
"https://example.org/retrieval/schemas/title_schema.yaml",
body="type: string",
)
doc = tmp_path / "doc.json"
doc.write_text(json.dumps(PASSING_DOCUMENT if passing_data else FAILING_DOCUMENT))
result = run_line(["check-jsonschema", "--schemafile", retrieval_uri, str(doc)])
assert result.exit_code == (0 if passing_data else 1)
def test_can_load_remote_yaml_schema_ref_from_cache(
run_line, tmp_path, cacheable_headers
):
retrieval_uri = "https://example.org/retrieval/schemas/main.yaml"
ref_loc = "https://example.org/retrieval/schemas/title_schema.yaml"
# First: add good responses with cache headers
responses.add(
"GET",
retrieval_uri,
body="""\
"$schema": "http://json-schema.org/draft-07/schema"
properties:
"title": {"$ref": "./title_schema.yaml"}
additionalProperties: false
""",
headers=cacheable_headers,
)
responses.add("GET", ref_loc, body="type: string", headers=cacheable_headers)
# Then: add bad responses (used if cache doesn't work)
responses.add("GET", retrieval_uri, body="error", status=500)
responses.add("GET", ref_loc, body="error", status=500)
doc = tmp_path / "doc.json"
doc.write_text(json.dumps(PASSING_DOCUMENT))
# First run: populates cache
result1 = run_line(["check-jsonschema", "--schemafile", retrieval_uri, str(doc)])
assert result1.exit_code == 0
# Second run: should use cached data (not the 500 errors)
result2 = run_line(["check-jsonschema", "--schemafile", retrieval_uri, str(doc)])
assert result2.exit_code == 0