Files
paperspec/tests/test_resources.py
T
alexmandClaude 4fb8b4f5a7 Add paperspec correct, check math forms and datasets, and give the tools public names
The work the other paper tools planned for paperspec (paperdesk PLAN M5.4,
paperform PLAN 4.2):

- paperspec correct and paperspec.revision apply proposed corrections to a
  package, folder or zip, and write it only once it validates.
- doc.math.form is checked: label and style commands always, the form's own
  spelling when papermath is installed or forms= is passed.
- paperspec.datasets and seven doc.dataset.* rules check a package's datasets.
- fixtures/invalid gains suspect-witness and math-form-label (33 in all).
- Canonical JSON reads numbers as doubles, so 1e20 round-trips.
- The registries and the JSON-LD context ship in the wheel.
- Public names for what paperdesk and paperform imported as private ones,
  with the old names kept as aliases.
- The text and lossy-projection functions are about three times faster
  through the gate, tested equal to the old code on every code point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013XwDhn4LkrKQfw4ArDEr2E
2026-10-20 22:10:17 -04:00

118 lines
3.4 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Scienting
#
# Source Code: https://git.scient.ing/papers/paperspec
# See the LICENSE.md file for full license terms.
"""Where the schemas, SQL, vectors, registries and context are on disk (paperspec.resources)."""
import json
import pathlib
import pytest
from paperspec import resources
MARKERS = {
"schemas": "annotation.schema.json",
"sql": "doc.sql",
"vectors": "proofs.jsonl",
"registry": "stamps/scienting-stamps-1.json",
"context": "scienting-1.jsonld",
"fixtures": "packages/hale2026harmonic-damb/MANIFEST.json",
}
def test_the_folders_are_the_six_the_package_names():
assert resources.FOLDERS == (
"schemas",
"sql",
"vectors",
"registry",
"context",
"fixtures",
)
@pytest.mark.parametrize("name", resources.FOLDERS)
def test_each_folder_is_found_and_holds_its_files(name):
folder = resources.path(name)
assert pathlib.Path(folder).is_dir()
assert (pathlib.Path(folder) / MARKERS[name]).is_file()
@pytest.mark.parametrize("name", ["tests", "", "SCHEMAS", "schemas/", "../schemas"])
def test_any_other_name_is_refused_and_the_folders_are_listed(name):
with pytest.raises(
ValueError, match="there are schemas, sql, vectors, registry, context, fixtures"
):
resources.path(name)
@pytest.fixture
def layout(tmp_path, monkeypatch):
"""A fake install under `tmp_path/root/src/paperspec`, with the checkout root `tmp_path/root`."""
package = tmp_path / "root" / "src" / "paperspec"
package.mkdir(parents=True)
monkeypatch.setattr(resources, "_PACKAGE", package)
return tmp_path / "root"
def test_a_folder_inside_the_package_is_found_first(layout):
inside = layout / "src" / "paperspec" / "sql"
inside.mkdir()
(layout / "sql").mkdir()
assert resources.path("sql") == str(inside)
def test_a_folder_at_the_root_of_the_checkout_is_found_when_the_package_has_none(
layout,
):
(layout / "vectors").mkdir()
assert resources.path("vectors") == str(layout / "vectors")
def test_a_folder_found_in_neither_place_is_an_error_naming_it(layout):
(layout / "sql").mkdir()
with pytest.raises(
FileNotFoundError, match="the schemas folder is neither in the package"
):
resources.path("schemas")
def test_a_file_in_place_of_a_folder_is_not_a_folder(layout):
(layout / "schemas").write_text("not a folder", encoding="utf-8")
with pytest.raises(FileNotFoundError):
resources.path("schemas")
@pytest.mark.parametrize(
("name", "fmt"),
[
("stamps/scienting-stamps-1.json", "scienting-stamps/1"),
("stages/scienting-stages-default.json", "scienting-stages/1"),
],
)
def test_a_registry_file_is_read_by_its_path_under_registry(name, fmt):
assert resources.registry(name)["format"] == fmt
def test_the_rules_registry_lists_every_rule_by_id():
rules = resources.registry("rules.json")
assert "doc.suspect.witness" in json.dumps(rules)
@pytest.mark.parametrize(
"name",
["", "README.md", "../pyproject.toml", "stamps/../rules.json", "/rules.json"],
)
def test_a_registry_name_that_is_not_a_plain_json_path_is_refused(name):
with pytest.raises(ValueError, match="no registry file"):
resources.registry(name)
def test_a_missing_registry_file_is_an_error(layout):
(layout / "registry").mkdir()
with pytest.raises(FileNotFoundError):
resources.registry("rules.json")