# 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")