|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +import responses |
| 5 | + |
| 6 | +from check_jsonschema import cachedownloader |
| 7 | + |
| 8 | +CASES = { |
| 9 | + "case1": { |
| 10 | + "main_schema": { |
| 11 | + "$schema": "http://json-schema.org/draft-07/schema", |
| 12 | + "properties": { |
| 13 | + "title": {"$ref": "./title_schema.json"}, |
| 14 | + }, |
| 15 | + "additionalProperties": False, |
| 16 | + }, |
| 17 | + "other_schemas": {"title_schema": {"type": "string"}}, |
| 18 | + "passing_document": {"title": "doc one"}, |
| 19 | + "failing_document": {"title": 2}, |
| 20 | + }, |
| 21 | + "case2": { |
| 22 | + "main_schema": { |
| 23 | + "$schema": "http://json-schema.org/draft-07/schema", |
| 24 | + "type": "object", |
| 25 | + "required": ["test"], |
| 26 | + "properties": {"test": {"$ref": "./values.json#/$defs/test"}}, |
| 27 | + }, |
| 28 | + "other_schemas": { |
| 29 | + "values": { |
| 30 | + "$schema": "http://json-schema.org/draft-07/schema", |
| 31 | + "$defs": {"test": {"type": "string"}}, |
| 32 | + } |
| 33 | + }, |
| 34 | + "passing_document": {"test": "some data"}, |
| 35 | + "failing_document": {"test": {"foo": "bar"}}, |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("check_passes", (True, False)) |
| 41 | +@pytest.mark.parametrize("casename", ("case1", "case2")) |
| 42 | +def test_remote_ref_resolution_simple_case( |
| 43 | + run_line, check_passes, casename, tmp_path, monkeypatch |
| 44 | +): |
| 45 | + def _fake_compute_default_cache_dir(self): |
| 46 | + return str(tmp_path) |
| 47 | + |
| 48 | + monkeypatch.setattr( |
| 49 | + cachedownloader.CacheDownloader, |
| 50 | + "_compute_default_cache_dir", |
| 51 | + _fake_compute_default_cache_dir, |
| 52 | + ) |
| 53 | + |
| 54 | + main_schema_loc = "https://example.com/main.json" |
| 55 | + responses.add("GET", main_schema_loc, json=CASES[casename]["main_schema"]) |
| 56 | + for name, subschema in CASES[casename]["other_schemas"].items(): |
| 57 | + other_schema_loc = f"https://example.com/{name}.json" |
| 58 | + responses.add("GET", other_schema_loc, json=subschema) |
| 59 | + |
| 60 | + instance_path = tmp_path / "instance.json" |
| 61 | + instance_path.write_text( |
| 62 | + json.dumps( |
| 63 | + CASES[casename]["passing_document"] |
| 64 | + if check_passes |
| 65 | + else CASES[casename]["failing_document"] |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + result = run_line( |
| 70 | + ["check-jsonschema", "--schemafile", main_schema_loc, str(instance_path)] |
| 71 | + ) |
| 72 | + output = f"\nstdout:\n{result.stdout}\n\nstderr:\n{result.stderr}" |
| 73 | + if check_passes: |
| 74 | + assert result.exit_code == 0, output |
| 75 | + else: |
| 76 | + assert result.exit_code == 1, output |
0 commit comments