|
1 | 1 | import json
|
2 | 2 |
|
3 | 3 | import pytest
|
| 4 | +import responses |
4 | 5 |
|
5 | 6 | from check_jsonschema.parsers.json5 import ENABLED as JSON5_ENABLED
|
| 7 | +from check_jsonschema.schema_loader.resolver import ref_url_to_cache_filename |
6 | 8 |
|
7 | 9 | SIMPLE_SCHEMA = {
|
8 | 10 | "$schema": "http://json-schema.org/draft-07/schema",
|
@@ -87,3 +89,79 @@ def test_can_load_json5_schema(run_line, tmp_path, passing_data):
|
87 | 89 | ["check-jsonschema", "--schemafile", str(main_schemafile), str(doc)]
|
88 | 90 | )
|
89 | 91 | 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