Source code for timetoshare.fixtures
"""The bundled cases of the current version: fixtures, rejections and warnings.
The emitted manifests under ``fixtures/<version>/`` (drawings excluded), the
canonical N-Quads expected of the statement fixtures under
``fixtures/rdf/<version>/``, the hand-authored rejection cases under
``fixtures/rejections/<version>/`` and the hand-authored warning cases under
``fixtures/warnings/<version>/``, found beside the schema. The cases are
written in canonical YAML (``.yaml``); a rejection or warning case about JSON
text itself would be written in JSON (``.json``); none is at present. Every
case is read with :func:`~timetoshare.serialization.loads`, as the JavaScript
package reads it.
"""
from __future__ import annotations
import math
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from .serialization import loads
from .validate import SCHEMA_VERSION, data_root
from .validation.diagnostics import InstancePath
[docs]
def fixture_paths() -> list[Path]:
"""Return every bundled manifest fixture, excluding drawings."""
directory = data_root() / "fixtures" / SCHEMA_VERSION
return sorted(
path
for path in directory.rglob("*.yaml")
if "drawings" not in path.relative_to(directory).parts
)
[docs]
def rdf_expectation(path: Path) -> Path:
"""Return the canonical N-Quads file expected of a statement fixture.
``fixtures/rdf/<version>/<name>.nq`` for the fixture
``statements/<name>.yaml`` and for its minimal twin
``statements/<name>.minimal.yaml`` alike: both read to the same graph.
The file may not exist yet; the caller reads it.
"""
name = path.name.removesuffix(".yaml").removesuffix(".minimal")
return data_root() / "fixtures" / "rdf" / SCHEMA_VERSION / f"{name}.nq"
def _cases(directory: Path) -> list[Path]:
return sorted(
path for path in directory.iterdir() if path.suffix in (".yaml", ".json")
)
[docs]
def rejection_paths() -> list[Path]:
"""Return the shared rejection cases, sorted by name."""
return _cases(data_root() / "fixtures" / "rejections" / SCHEMA_VERSION)
[docs]
@dataclass(frozen=True)
class RejectionCase:
"""One shared rejection case, read for validation.
Attributes:
manifest: The value to validate, with any non-finite number in place.
path: Where the validator reports the problem.
code: The rule's code, or ``None`` where the case raises an error
without one.
error: The name of the error class where it is not
``SchemaValidationError``, else ``None``.
reason: The rule the case breaks, in words.
"""
manifest: Any
path: InstancePath
code: str | None
error: str | None
reason: str
_NON_FINITE = {"NaN": math.nan, "Infinity": math.inf, "-Infinity": -math.inf}
[docs]
def rejection_case(path: Path) -> RejectionCase:
"""Read one shared rejection case from ``path``.
A case holds no NaN and no infinity, which no serialization writes, so a
case about the finite rule stores a finite placeholder at its path and
names the value to put there under ``non_finite``; the reader puts it in
place.
Raises:
ValueError: If the file cannot be read or names an unknown non-finite
value.
"""
case = loads(path.read_text(encoding="utf-8"))
location = tuple(case["path"])
manifest = case["manifest"]
if "non_finite" in case:
name = case["non_finite"]
if name not in _NON_FINITE:
raise ValueError(f"{path.name}: {name!r} names no non-finite number")
owner = manifest
for segment in location[:-1]:
owner = owner[segment]
owner[location[-1]] = _NON_FINITE[name]
return RejectionCase(
manifest=manifest,
path=location,
code=case["code"],
error=case.get("error"),
reason=case["reason"],
)
[docs]
def warning_paths() -> list[Path]:
"""Return the shared warning cases, sorted by name."""
return _cases(data_root() / "fixtures" / "warnings" / SCHEMA_VERSION)