Skip to content

Commit f60971f

Browse files
committed
Add tests for remote YAML ref loading and caching
With any non-`.json` suffix, e.g., `.yaml`, we need the cache to preserve extensions in order to be certain that the cache loader will choose the same parser when loading the cached ref as the one it used when the ref was remote. The `.yaml` ref + cache population trickery allows us to test this codepath in an acceptance test.
1 parent 53a1592 commit f60971f

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tests/acceptance/test_nonjson_schema_handling.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
22

33
import pytest
4+
import responses
45

56
from check_jsonschema.parsers.json5 import ENABLED as JSON5_ENABLED
7+
from check_jsonschema.schema_loader.resolver import ref_url_to_cache_filename
68

79
SIMPLE_SCHEMA = {
810
"$schema": "http://json-schema.org/draft-07/schema",
@@ -87,3 +89,79 @@ def test_can_load_json5_schema(run_line, tmp_path, passing_data):
8789
["check-jsonschema", "--schemafile", str(main_schemafile), str(doc)]
8890
)
8991
assert result.exit_code == (0 if passing_data else 1)
92+
93+
94+
@pytest.mark.parametrize("passing_data", [True, False])
95+
def test_can_load_remote_yaml_schema(run_line, tmp_path, passing_data):
96+
retrieval_uri = "https://example.org/retrieval/schemas/main.yaml"
97+
responses.add(
98+
"GET",
99+
retrieval_uri,
100+
body="""\
101+
"$schema": "http://json-schema.org/draft-07/schema"
102+
properties:
103+
title: {"type": "string"}
104+
additionalProperties: false
105+
""",
106+
)
107+
108+
doc = tmp_path / "doc.json"
109+
doc.write_text(json.dumps(PASSING_DOCUMENT if passing_data else FAILING_DOCUMENT))
110+
111+
result = run_line(["check-jsonschema", "--schemafile", retrieval_uri, str(doc)])
112+
assert result.exit_code == (0 if passing_data else 1)
113+
114+
115+
@pytest.mark.parametrize("passing_data", [True, False])
116+
def test_can_load_remote_yaml_schema_ref(run_line, tmp_path, passing_data):
117+
retrieval_uri = "https://example.org/retrieval/schemas/main.yaml"
118+
responses.add(
119+
"GET",
120+
retrieval_uri,
121+
body="""\
122+
"$schema": "http://json-schema.org/draft-07/schema"
123+
properties:
124+
"title": {"$ref": "./title_schema.yaml"}
125+
additionalProperties: false
126+
""",
127+
)
128+
responses.add(
129+
"GET",
130+
"https://example.org/retrieval/schemas/title_schema.yaml",
131+
body="type: string",
132+
)
133+
134+
doc = tmp_path / "doc.json"
135+
doc.write_text(json.dumps(PASSING_DOCUMENT if passing_data else FAILING_DOCUMENT))
136+
137+
result = run_line(["check-jsonschema", "--schemafile", retrieval_uri, str(doc)])
138+
assert result.exit_code == (0 if passing_data else 1)
139+
140+
141+
def test_can_load_remote_yaml_schema_ref_from_cache(run_line, cache_dir, tmp_path):
142+
retrieval_uri = "https://example.org/retrieval/schemas/main.yaml"
143+
responses.add(
144+
"GET",
145+
retrieval_uri,
146+
body="""\
147+
"$schema": "http://json-schema.org/draft-07/schema"
148+
properties:
149+
"title": {"$ref": "./title_schema.yaml"}
150+
additionalProperties: false
151+
""",
152+
)
153+
154+
ref_loc = "https://example.org/retrieval/schemas/title_schema.yaml"
155+
# populate a bad schema, but then "override" that with a good cache value
156+
# this can only pass (in the success case) if the cache loading really works
157+
responses.add("GET", ref_loc, body="false")
158+
ref_cache_dir = cache_dir / "check_jsonschema" / "refs"
159+
ref_cache_dir.mkdir(parents=True)
160+
ref_path = ref_cache_dir / ref_url_to_cache_filename(ref_loc)
161+
ref_path.write_text("type: string")
162+
163+
doc = tmp_path / "doc.json"
164+
doc.write_text(json.dumps(PASSING_DOCUMENT))
165+
166+
result = run_line(["check-jsonschema", "--schemafile", retrieval_uri, str(doc)])
167+
assert result.exit_code == 0

0 commit comments

Comments
 (0)