|
| 1 | +import hashlib |
1 | 2 | import json
|
2 | 3 |
|
3 | 4 | import pytest
|
|
35 | 36 | }
|
36 | 37 |
|
37 | 38 |
|
| 39 | +def _md5(s): |
| 40 | + return hashlib.md5(s.encode()).hexdigest() |
| 41 | + |
| 42 | + |
38 | 43 | @pytest.mark.parametrize("check_passes", (True, False))
|
39 | 44 | @pytest.mark.parametrize("casename", ("case1", "case2"))
|
40 | 45 | def test_remote_ref_resolution_simple_case(run_line, check_passes, casename, tmp_path):
|
@@ -63,6 +68,88 @@ def test_remote_ref_resolution_simple_case(run_line, check_passes, casename, tmp
|
63 | 68 | assert result.exit_code == 1, output
|
64 | 69 |
|
65 | 70 |
|
| 71 | +@pytest.mark.parametrize("casename", ("case1", "case2")) |
| 72 | +@pytest.mark.parametrize("disable_cache", (True, False)) |
| 73 | +def test_remote_ref_resolution_cache_control( |
| 74 | + run_line, tmp_path, cache_dir, casename, disable_cache |
| 75 | +): |
| 76 | + main_schema_loc = "https://example.com/main.json" |
| 77 | + responses.add("GET", main_schema_loc, json=CASES[casename]["main_schema"]) |
| 78 | + |
| 79 | + ref_locs = [] |
| 80 | + for name, subschema in CASES[casename]["other_schemas"].items(): |
| 81 | + other_schema_loc = f"https://example.com/{name}.json" |
| 82 | + responses.add("GET", other_schema_loc, json=subschema) |
| 83 | + ref_locs.append(other_schema_loc) |
| 84 | + |
| 85 | + instance_path = tmp_path / "instance.json" |
| 86 | + instance_path.write_text(json.dumps(CASES[casename]["passing_document"])) |
| 87 | + |
| 88 | + # run the command |
| 89 | + result = run_line( |
| 90 | + ["check-jsonschema", "--schemafile", main_schema_loc, str(instance_path)] |
| 91 | + + (["--no-cache"] if disable_cache else []) |
| 92 | + ) |
| 93 | + output = f"\nstdout:\n{result.stdout}\n\nstderr:\n{result.stderr}" |
| 94 | + assert result.exit_code == 0, output |
| 95 | + |
| 96 | + cache_locs = [] |
| 97 | + for ref_loc in ref_locs: |
| 98 | + cache_locs.append(cache_dir / "check_jsonschema" / "refs" / _md5(ref_loc)) |
| 99 | + assert cache_locs # sanity check |
| 100 | + if disable_cache: |
| 101 | + for loc in cache_locs: |
| 102 | + assert not loc.exists() |
| 103 | + else: |
| 104 | + for loc in cache_locs: |
| 105 | + assert loc.exists() |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize("casename", ("case1", "case2")) |
| 109 | +@pytest.mark.parametrize("check_passes", (True, False)) |
| 110 | +def test_remote_ref_resolution_loads_from_cache( |
| 111 | + run_line, tmp_path, cache_dir, casename, check_passes |
| 112 | +): |
| 113 | + main_schema_loc = "https://example.com/main.json" |
| 114 | + responses.add("GET", main_schema_loc, json=CASES[casename]["main_schema"]) |
| 115 | + |
| 116 | + # ensure the ref cache dir exists |
| 117 | + ref_cache_dir = cache_loc = cache_dir / "check_jsonschema" / "refs" |
| 118 | + ref_cache_dir.mkdir(parents=True) |
| 119 | + |
| 120 | + ref_locs = [] |
| 121 | + cache_locs = [] |
| 122 | + for name, subschema in CASES[casename]["other_schemas"].items(): |
| 123 | + other_schema_loc = f"https://example.com/{name}.json" |
| 124 | + # intentionally populate the HTTP location with "bad data" |
| 125 | + responses.add("GET", other_schema_loc, json="{}") |
| 126 | + ref_locs.append(other_schema_loc) |
| 127 | + |
| 128 | + # but populate the cache with "good data" |
| 129 | + cache_loc = ref_cache_dir / _md5(other_schema_loc) |
| 130 | + cache_locs.append(cache_loc) |
| 131 | + cache_loc.write_text(json.dumps(subschema)) |
| 132 | + |
| 133 | + instance_path = tmp_path / "instance.json" |
| 134 | + instance_path.write_text( |
| 135 | + json.dumps( |
| 136 | + CASES[casename]["passing_document"] |
| 137 | + if check_passes |
| 138 | + else CASES[casename]["failing_document"] |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + # run the command |
| 143 | + result = run_line( |
| 144 | + ["check-jsonschema", "--schemafile", main_schema_loc, str(instance_path)] |
| 145 | + ) |
| 146 | + output = f"\nstdout:\n{result.stdout}\n\nstderr:\n{result.stderr}" |
| 147 | + if check_passes: |
| 148 | + assert result.exit_code == 0, output |
| 149 | + else: |
| 150 | + assert result.exit_code == 1, output |
| 151 | + |
| 152 | + |
66 | 153 | # this test ensures that `$id` is preferred for the base URI over
|
67 | 154 | # the retrieval URI
|
68 | 155 | @pytest.mark.parametrize("check_passes", (True, False))
|
|
0 commit comments