forked from ChelseaKR/ca-tariff-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_schema.py
More file actions
80 lines (61 loc) · 2.81 KB
/
Copy pathtest_schema.py
File metadata and controls
80 lines (61 loc) · 2.81 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
"""The published JSON Schema actually describes what `parse` emits.
`schemas/parsed-schedule-v1.schema.json` is the stable, documented shape of
`parsed-schedule/v1`. This module is what keeps it honest: every output this
suite produces, synthetic and real, is validated against it, so a field added
to the model without a matching schema update fails here rather than shipping
silently out of date.
"""
from __future__ import annotations
import json
from pathlib import Path
import jsonschema
import pytest
from ca_tariff_parse.extract import layout_from_path
from ca_tariff_parse.parser import parse_document, parse_manifest_document
from ca_tariff_parse.profiles import resolve
from ca_tariff_parse.sources import find, load_manifest
from .conftest import COMPLETE, GOLDEN, KEYWORD, REPO_ROOT, SOURCES, UNKNOWN
SCHEMA_PATH = REPO_ROOT / "schemas" / "parsed-schedule-v1.schema.json"
MANIFEST = REPO_ROOT / "sources" / "sources.toml"
@pytest.fixture(scope="module")
def schema() -> dict[str, object]:
return json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
def test_schema_is_itself_a_valid_draft_2020_12_schema(schema: dict[str, object]) -> None:
jsonschema.Draft202012Validator.check_schema(schema)
@pytest.mark.parametrize(
("fixture", "profile_name"),
[(COMPLETE, None), (UNKNOWN, None), (KEYWORD, "pge-tariff-book")],
)
def test_synthetic_output_matches_the_schema(
schema: dict[str, object], fixture: Path, profile_name: str | None
) -> None:
profile = resolve(profile_name)
doc = layout_from_path(fixture, profile=profile)
payload = parse_document(doc, profile=profile).to_json()
jsonschema.Draft202012Validator(schema).validate(payload)
def test_every_committed_golden_file_matches_the_schema(schema: dict[str, object]) -> None:
validator = jsonschema.Draft202012Validator(schema)
golden_files = sorted(GOLDEN.glob("*.json"))
assert golden_files, "expected at least one committed golden file"
for path in golden_files:
validator.validate(json.loads(path.read_text(encoding="utf-8")))
REALDOC_CASES = [
("smud-r-tod", "1-R-TOD.pdf"),
("smud-r", "1-R.pdf"),
("smud-ci-tod1", "CI-TOD1.pdf"),
("smud-ssr", "01_SSR.pdf"),
("pge-e-1", "ELEC_SCHEDS_E-1.pdf"),
("pge-e-tou-c", "ELEC_SCHEDS_E-TOU-C.pdf"),
("pge-b-1", "ELEC_SCHEDS_B-1.pdf"),
]
@pytest.mark.realdoc
@pytest.mark.parametrize(("document_id", "filename"), REALDOC_CASES)
def test_real_document_output_matches_the_schema(
schema: dict[str, object], document_id: str, filename: str
) -> None:
path = SOURCES / filename
if not path.exists():
pytest.skip(f"{document_id} ({filename}) not fetched; run `make fetch`")
entry = find(load_manifest(MANIFEST), document_id)
payload = parse_manifest_document(entry, path).to_json()
jsonschema.Draft202012Validator(schema).validate(payload)