From a037380ed45cbcbdef0ccda6700571cd1f7906f7 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Thu, 26 Nov 2026 15:07:55 +0100 Subject: [PATCH 1/2] Copy project structure validation --- dlccore/project/schemas/base/finding_codes.py | 41 ++ dlccore/project/validation/__init__.py | 16 + dlccore/project/validation/requests.py | 36 ++ dlccore/project/validation/results.py | 23 ++ dlccore/project/validation/structure.py | 132 +++++++ tests/project/validation/test_requests.py | 52 +++ tests/project/validation/test_structure.py | 355 ++++++++++++++++++ 7 files changed, 655 insertions(+) create mode 100644 dlccore/project/validation/__init__.py create mode 100644 dlccore/project/validation/requests.py create mode 100644 dlccore/project/validation/results.py create mode 100644 dlccore/project/validation/structure.py create mode 100644 tests/project/validation/test_requests.py create mode 100644 tests/project/validation/test_structure.py diff --git a/dlccore/project/schemas/base/finding_codes.py b/dlccore/project/schemas/base/finding_codes.py index 5c786e8..31b6c4d 100644 --- a/dlccore/project/schemas/base/finding_codes.py +++ b/dlccore/project/schemas/base/finding_codes.py @@ -56,6 +56,47 @@ def __new__( return member +class StructureFindingCode(FindingCode): + """Findings about the project's schema-defined layout.""" + + PROJECT_REQUIRED_NODE_MISSING = ( + "PROJECT_REQUIRED_NODE_MISSING", + "A required project-layout node is missing.", + Severity.ERROR, + ("resource_kind",), + ) + PROJECT_SYMLINK_TARGET_MISSING = ( + "PROJECT_SYMLINK_TARGET_MISSING", + "A project resource is a symlink whose target does not exist.", + Severity.ERROR, + ("resource_kind", "raw_target", "absolute_target"), + ) + PROJECT_SYMLINK_TARGET_WRONG_TYPE = ( + "PROJECT_SYMLINK_TARGET_WRONG_TYPE", + "The symbolic-link target has an unexpected filesystem type.", + Severity.ERROR, + ("resource_kind", "raw_target", "absolute_target", "target_kind"), + ) + PROJECT_NODE_WRONG_TYPE = ( + "PROJECT_NODE_WRONG_TYPE", + "A project resource exists at the expected location but has the wrong filesystem type.", + Severity.ERROR, + ("resource_kind", "expected_type"), + ) + PROJECT_NODE_UNREADABLE = ( + "PROJECT_NODE_UNREADABLE", + "A project-layout node could not be read.", + Severity.ERROR, + ("resource_kind", "error_type", "error_message"), + ) + PROJECT_RESOURCE_PATH_COLLISION = ( + "PROJECT_RESOURCE_PATH_COLLISION", + "This project resource resolves to the same filesystem path as another; each one is reported.", + Severity.ERROR, + ("resource_kind", "collision_path", "collision_group"), + ) + + def iter_finding_codes() -> tuple[FindingCode, ...]: """Return every declared finding code, namespace by namespace.""" diff --git a/dlccore/project/validation/__init__.py b/dlccore/project/validation/__init__.py new file mode 100644 index 0000000..ac51098 --- /dev/null +++ b/dlccore/project/validation/__init__.py @@ -0,0 +1,16 @@ +from .context import ValidationContext, build_validation_context +from .project import validate_project +from .requests import ( + ProjectValidationRequest, + ResourceValidationRequest, +) +from .structure import validate_project_structure + +__all__ = [ + "ProjectValidationRequest", + "ResourceValidationRequest", + "ValidationContext", + "build_validation_context", + "validate_project", + "validate_project_structure", +] diff --git a/dlccore/project/validation/requests.py b/dlccore/project/validation/requests.py new file mode 100644 index 0000000..b21dbe7 --- /dev/null +++ b/dlccore/project/validation/requests.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +from pydantic import model_validator +from typing_extensions import Self + +from dlc_octopus.schemas.base.core import FrozenModel +from dlc_octopus.schemas.base.resources import ( + ResourceKind, +) +from dlc_octopus.schemas.base.validation import ValidationDepth + + +class ResourceValidationRequest(FrozenModel): + """Validation requested for one semantic resource kind.""" + + kind: ResourceKind + depth: ValidationDepth + + +class ProjectValidationRequest(FrozenModel): + """Explicit resource validation requested for one project inspection.""" + + resources: tuple[ResourceValidationRequest, ...] = () + + @model_validator(mode="after") + def validate_unique_resource_kinds( + self, + ) -> Self: + """Reject duplicate requests for the same resource kind.""" + + kinds = [request.kind for request in self.resources] + + if len(kinds) != len(set(kinds)): + raise ValueError("Each resource kind may be requested at most once.") + + return self diff --git a/dlccore/project/validation/results.py b/dlccore/project/validation/results.py new file mode 100644 index 0000000..e7632b5 --- /dev/null +++ b/dlccore/project/validation/results.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +from dlc_octopus.schemas.base.core import FrozenModel +from dlc_octopus.schemas.base.findings import Finding +from dlc_octopus.schemas.base.validation import ValidationCheck + + +class ValidationResult(FrozenModel): + """Findings and execution records produced by validation.""" + + findings: tuple[Finding, ...] = () + checks: tuple[ValidationCheck, ...] = () + + def merge( + self, + other: ValidationResult, + ) -> ValidationResult: + """Combine two immutable validation results.""" + + return ValidationResult( + findings=(self.findings + other.findings), + checks=(self.checks + other.checks), + ) diff --git a/dlccore/project/validation/structure.py b/dlccore/project/validation/structure.py new file mode 100644 index 0000000..3b2b012 --- /dev/null +++ b/dlccore/project/validation/structure.py @@ -0,0 +1,132 @@ +from __future__ import annotations + +from pathlib import Path + +from dlc_octopus.schemas.base.filesystem import ( + DirectorySchema, + LayoutIndex, + NodeSchema, +) +from dlc_octopus.schemas.base.finding_codes import StructureFindingCode +from dlc_octopus.schemas.base.findings import Finding +from dlc_octopus.schemas.base.observations import NodeStatus, ObservedNode, walk_observed_nodes +from dlc_octopus.schemas.base.resources import ResourceManagement +from dlc_octopus.schemas.base.symlinks import SymlinkTargetKind +from dlc_octopus.schemas.locations import Location, node_location +from dlc_octopus.schemas.project_layout.project import PROJECT_INDEX + + +def validate_project_structure( + project: ObservedNode, + *, + project_root: Path, + index: LayoutIndex = PROJECT_INDEX, +) -> tuple[Finding, ...]: + """Validate an observed project against its layout schema. + + Structural validation reports missing required nodes, nodes with the wrong + physical type, and nodes that could not be read. It does not interpret + config fields, validate file contents, or report unknown resources. A + known-unmanaged node is observed, not validated: it yields no finding here. + """ + + findings: list[Finding] = [] + + for node in walk_observed_nodes(project): + node_schema = index.schemas.get(node.schema_id) + + if node_schema is None: + raise ValueError(f"Observed node references unknown schema ID {node.schema_id!r}.") + + if node_schema.management == ResourceManagement.KNOWN_UNMANAGED: + continue + + finding = _finding_for_node( + node, + schema=node_schema, + project_root=project_root, + location=node_location(node.schema_id, node.project_relative_path, index=index), + ) + + if finding is not None: + findings.append(finding) + + return tuple(findings) + + +def _finding_for_node( + node: ObservedNode, + *, + schema: NodeSchema, + project_root: Path, + location: Location, +) -> Finding | None: + """Return a structural finding for one observed node, if needed.""" + + path = project_root / node.project_relative_path + evidence = { + "resource_kind": node.kind.value, + } + + if node.status == NodeStatus.MISSING: + if not schema.required: + return None + + return Finding( + code=StructureFindingCode.PROJECT_REQUIRED_NODE_MISSING, + **location, + path=path, + evidence=evidence, + ) + + if node.symlink is not None and node.symlink.target_kind == SymlinkTargetKind.MISSING: + return Finding( + code=StructureFindingCode.PROJECT_SYMLINK_TARGET_MISSING, + **location, + path=path, + evidence={ + **evidence, + "raw_target": str(node.symlink.raw_target), + "absolute_target": str(node.symlink.absolute_target), + }, + ) + + if node.symlink is not None and node.status == NodeStatus.WRONG_TYPE: + return Finding( + code=StructureFindingCode.PROJECT_SYMLINK_TARGET_WRONG_TYPE, + **location, + path=path, + evidence={ + **evidence, + "raw_target": str(node.symlink.raw_target), + "absolute_target": str(node.symlink.absolute_target), + "target_kind": (node.symlink.target_kind.value), + }, + ) + + if node.status == NodeStatus.WRONG_TYPE: + expected_type = "directory" if isinstance(schema, DirectorySchema) else "file" + + return Finding( + code=StructureFindingCode.PROJECT_NODE_WRONG_TYPE, + **location, + path=path, + evidence={ + **evidence, + "expected_type": expected_type, + }, + ) + + if node.status == NodeStatus.UNREADABLE: + return Finding( + code=StructureFindingCode.PROJECT_NODE_UNREADABLE, + **location, + path=path, + evidence={ + **evidence, + "error_type": node.traversal_error_type or node.probe.error_type, + "error_message": node.traversal_error_message or node.probe.error_message, + }, + ) + + return None diff --git a/tests/project/validation/test_requests.py b/tests/project/validation/test_requests.py new file mode 100644 index 0000000..c934e37 --- /dev/null +++ b/tests/project/validation/test_requests.py @@ -0,0 +1,52 @@ +from __future__ import annotations + +import pytest +from pydantic import ValidationError + +from dlc_octopus.schemas.base.resources import ( + DirectoryResourceKind, +) +from dlc_octopus.schemas.base.validation import ( + ValidationDepth, +) +from dlc_octopus.validation import ( + ProjectValidationRequest, + ResourceValidationRequest, +) + + +def test_validation_request_targets_resource_kinds() -> None: + request = ProjectValidationRequest( + resources=( + ResourceValidationRequest( + kind=DirectoryResourceKind.VIDEOS_DIRECTORY, + depth=ValidationDepth.FILENAME_ONLY, + ), + ResourceValidationRequest( + kind=(DirectoryResourceKind.LABELED_DATA_DIRECTORY), + depth=ValidationDepth.FULL, + ), + ) + ) + + assert request.resources[0].kind == (DirectoryResourceKind.VIDEOS_DIRECTORY) + assert request.resources[1].depth == ValidationDepth.FULL + + +def test_validation_request_rejects_duplicate_resource_kinds() -> None: + with pytest.raises( + ValidationError, + match=("Each resource kind may be requested at most once"), + ): + ProjectValidationRequest( + resources=( + ResourceValidationRequest( + kind=(DirectoryResourceKind.VIDEOS_DIRECTORY), + depth=ValidationDepth.FILENAME_ONLY, + ), + ResourceValidationRequest( + kind=(DirectoryResourceKind.VIDEOS_DIRECTORY), + depth=ValidationDepth.FULL, + ), + ) + ) diff --git a/tests/project/validation/test_structure.py b/tests/project/validation/test_structure.py new file mode 100644 index 0000000..1c4bb52 --- /dev/null +++ b/tests/project/validation/test_structure.py @@ -0,0 +1,355 @@ +from __future__ import annotations + +from pathlib import Path + +from dlc_octopus.discovery.observer import ( + ExpectedEntryKind, + discover_project, + status_for_expected_entry, +) +from dlc_octopus.schemas.base.filesystem import ( + DirectorySchema, + FileSchema, + NamePattern, + RepeatedNodeSchema, + index_layout, +) +from dlc_octopus.schemas.base.findings import ( + Severity, +) +from dlc_octopus.schemas.base.observations import ( + NodeStatus, + ObservedNode, +) +from dlc_octopus.schemas.base.probes import ( + FilesystemEntryKind, + PathProbe, + PathProbeStatus, +) +from dlc_octopus.schemas.base.resources import ( + DirectoryResourceKind, + FileResourceKind, +) +from dlc_octopus.schemas.base.symlinks import ( + SymlinkObservation, + SymlinkTargetKind, +) +from dlc_octopus.validation.structure import ( + validate_project_structure, +) + +_RAW_TARGET = Path("../external.mp4") + + +def _observed_directory( + *, + schema_id: str, + kind: DirectoryResourceKind, + project_relative_path: Path, + schema_children: tuple[ObservedNode, ...], +) -> ObservedNode: + """Build a present directory node with the given schema children.""" + + return ObservedNode( + schema_id=schema_id, + kind=kind, + project_relative_path=project_relative_path, + probe=PathProbe( + status=PathProbeStatus.OBSERVED, + entry_kind=FilesystemEntryKind.DIRECTORY, + ), + status=NodeStatus.PRESENT, + schema_children=schema_children, + ) + + +def _project_with_symlinked_video( + *, + target_kind: SymlinkTargetKind, + project_root: Path, +) -> ObservedNode: + """Build an observed project whose only source video is a symlink. + + The node status is derived through ``status_for_expected_entry`` rather + than assigned, so these tests exercise the real derivation. + + Args: + target_kind: Observed type of the symlink target. + project_root: Project root the absolute target is resolved against. + + Returns: + The observed project root. + """ + + probe = PathProbe( + status=PathProbeStatus.OBSERVED, + entry_kind=FilesystemEntryKind.SYMLINK, + symlink=SymlinkObservation( + raw_target=_RAW_TARGET, + absolute_target=project_root / "external.mp4", + target_kind=target_kind, + ), + ) + + video = ObservedNode( + schema_id="dlc.videos.source_video", + kind=FileResourceKind.SOURCE_VIDEO, + project_relative_path=Path("videos") / "session-01.mp4", + probe=probe, + status=status_for_expected_entry( + probe, + expected_kind=ExpectedEntryKind.FILE, + ), + ) + + videos = _observed_directory( + schema_id="dlc.videos", + kind=DirectoryResourceKind.VIDEOS_DIRECTORY, + project_relative_path=Path("videos"), + schema_children=(video,), + ) + + return _observed_directory( + schema_id="dlc.project", + kind=DirectoryResourceKind.PROJECT_ROOT, + project_relative_path=Path(), + schema_children=(videos,), + ) + + +def test_missing_required_config_produces_finding( + tmp_path: Path, +) -> None: + project = discover_project(tmp_path) + + findings = validate_project_structure( + project, + project_root=tmp_path, + ) + + missing = [finding for finding in findings if finding.code == "PROJECT_REQUIRED_NODE_MISSING"] + + assert len(missing) == 1 + assert missing[0].severity == Severity.ERROR + assert missing[0].path == tmp_path / "config.yaml" + assert missing[0].schema_id == "dlc.project.config" + assert "schema_id" not in missing[0].evidence + assert missing[0].instance == () + + +def test_a_finding_on_a_repeated_node_names_its_instance( + tmp_path: Path, +) -> None: + project = _project_with_symlinked_video( + target_kind=SymlinkTargetKind.MISSING, + project_root=tmp_path, + ) + + (finding,) = validate_project_structure(project, project_root=tmp_path) + + assert finding.schema_id == "dlc.videos.source_video" + assert finding.instance == ("videos", "session-01.mp4") + + +def test_a_node_inside_a_repeated_instance_carries_that_instance( + tmp_path: Path, +) -> None: + # A managed plain child of a repeated dataset belongs to one dataset, so a finding + # about it must not reach the other datasets. The shipped layout's only such child + # is known-unmanaged and yields no findings, hence a layout of its own. + manifest_schema = FileSchema( + schema_id="dlc.test.dataset.manifest", + kind=FileResourceKind.NAPARI_DLC_METADATA, + name=NamePattern(exact="manifest.json"), + ) + layout = DirectorySchema( + schema_id="dlc.test", + kind=DirectoryResourceKind.PROJECT_ROOT, + name=NamePattern(), + repeated_children=( + RepeatedNodeSchema( + child=DirectorySchema( + schema_id="dlc.test.dataset", + kind=DirectoryResourceKind.LABELED_DATASET, + name=NamePattern(), + children=(manifest_schema,), + ), + ), + ), + ) + manifest = ObservedNode( + schema_id="dlc.test.dataset.manifest", + kind=FileResourceKind.NAPARI_DLC_METADATA, + project_relative_path=Path("ds1") / "manifest.json", + probe=PathProbe( + status=PathProbeStatus.INACCESSIBLE, + error_type="PermissionError", + error_message="denied", + ), + status=NodeStatus.UNREADABLE, + ) + dataset = _observed_directory( + schema_id="dlc.test.dataset", + kind=DirectoryResourceKind.LABELED_DATASET, + project_relative_path=Path("ds1"), + schema_children=(manifest,), + ) + project = _observed_directory( + schema_id="dlc.test", + kind=DirectoryResourceKind.PROJECT_ROOT, + project_relative_path=Path(), + schema_children=(dataset,), + ) + + index = index_layout(layout) + (finding,) = validate_project_structure(project, project_root=tmp_path, index=index) + + assert finding.code == "PROJECT_NODE_UNREADABLE" + assert finding.instance == ("ds1", "manifest.json") + + +def test_missing_optional_directories_do_not_produce_findings( + valid_project: Path, +) -> None: + (valid_project / "videos").rmdir() + (valid_project / "labeled-data").rmdir() + + project = discover_project(valid_project) + + findings = validate_project_structure( + project, + project_root=valid_project, + ) + + assert not any( + finding.code == "PROJECT_REQUIRED_NODE_MISSING" + and finding.path + in { + valid_project / "videos", + valid_project / "labeled-data", + } + for finding in findings + ) + + +def test_wrong_type_produces_finding( + valid_project: Path, +) -> None: + videos = valid_project / "videos" + videos.rmdir() + videos.write_text( + "not a directory", + encoding="utf-8", + ) + + project = discover_project(valid_project) + + findings = validate_project_structure( + project, + project_root=valid_project, + ) + + wrong_type = next(finding for finding in findings if finding.code == "PROJECT_NODE_WRONG_TYPE") + + assert wrong_type.path == videos + assert wrong_type.evidence["expected_type"] == "directory" + + +def test_broken_symlink_produces_symlink_target_missing_finding( + tmp_path: Path, +) -> None: + project = _project_with_symlinked_video( + target_kind=SymlinkTargetKind.MISSING, + project_root=tmp_path, + ) + + findings = validate_project_structure( + project, + project_root=tmp_path, + ) + + assert len(findings) == 1 + + finding = findings[0] + + assert finding.code == "PROJECT_SYMLINK_TARGET_MISSING" + assert finding.severity == Severity.ERROR + assert finding.path == tmp_path / "videos" / "session-01.mp4" + assert finding.evidence["raw_target"] == str(_RAW_TARGET) + assert finding.evidence["absolute_target"] == str(tmp_path / "external.mp4") + + +def test_symlink_to_directory_produces_symlink_target_wrong_type_finding( + tmp_path: Path, +) -> None: + project = _project_with_symlinked_video( + target_kind=SymlinkTargetKind.DIRECTORY, + project_root=tmp_path, + ) + + findings = validate_project_structure( + project, + project_root=tmp_path, + ) + + assert len(findings) == 1 + + finding = findings[0] + + assert finding.code == "PROJECT_SYMLINK_TARGET_WRONG_TYPE" + assert finding.severity == Severity.ERROR + assert finding.path == tmp_path / "videos" / "session-01.mp4" + assert finding.evidence["target_kind"] == SymlinkTargetKind.DIRECTORY.value + + +def test_symlink_to_file_produces_no_structural_finding( + tmp_path: Path, +) -> None: + project = _project_with_symlinked_video( + target_kind=SymlinkTargetKind.FILE, + project_root=tmp_path, + ) + + findings = validate_project_structure( + project, + project_root=tmp_path, + ) + + assert findings == () + + +def test_valid_structure_has_no_structural_findings( + valid_project: Path, +) -> None: + project = discover_project(valid_project) + + findings = validate_project_structure( + project, + project_root=valid_project, + ) + + assert findings == () + + +def test_a_known_unmanaged_node_yields_no_structure_findings( + tmp_path: Path, +) -> None: + (tmp_path / "labeled-data" / "m1" / ".napari-deeplabcut.json").mkdir(parents=True) + + project = discover_project(tmp_path) + pending = [project] + statuses = {} + + while pending: + node = pending.pop() + statuses[node.schema_id] = node.status + pending.extend(node.schema_children) + + assert statuses["dlc.labeled_data.napari_metadata"] is NodeStatus.WRONG_TYPE + + findings = validate_project_structure( + project, + project_root=tmp_path, + ) + + assert [finding for finding in findings if finding.schema_id == "dlc.labeled_data.napari_metadata"] == [] From 341ef11cedc5a7fd6c9f85ec97a42e83f4c43791 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Thu, 26 Nov 2026 15:10:11 +0100 Subject: [PATCH 2/2] Adapt project structure validation to dlccore - Rename the packages to dlccore.project and add the dlccore license header - Declare StructureFindingCode without PROJECT_RESOURCE_PATH_COLLISION, which lands with the video validation that emits it - Export only requests and structure from validation; context and project follow with the PRs that add them - Add two tests of tests/validation/test_finding_schema_ids.py; the others need inspect_project or video validation - The closed-catalogue test now expects StructureFindingCode --- dlccore/project/schemas/base/finding_codes.py | 6 -- dlccore/project/validation/__init__.py | 16 ++-- dlccore/project/validation/requests.py | 17 +++- dlccore/project/validation/results.py | 17 +++- dlccore/project/validation/structure.py | 27 +++++-- tests/project/schemas/test_finding_codes.py | 9 ++- .../validation/test_finding_schema_ids.py | 80 +++++++++++++++++++ tests/project/validation/test_requests.py | 17 +++- tests/project/validation/test_structure.py | 27 +++++-- 9 files changed, 178 insertions(+), 38 deletions(-) create mode 100644 tests/project/validation/test_finding_schema_ids.py diff --git a/dlccore/project/schemas/base/finding_codes.py b/dlccore/project/schemas/base/finding_codes.py index 31b6c4d..4c658ac 100644 --- a/dlccore/project/schemas/base/finding_codes.py +++ b/dlccore/project/schemas/base/finding_codes.py @@ -89,12 +89,6 @@ class StructureFindingCode(FindingCode): Severity.ERROR, ("resource_kind", "error_type", "error_message"), ) - PROJECT_RESOURCE_PATH_COLLISION = ( - "PROJECT_RESOURCE_PATH_COLLISION", - "This project resource resolves to the same filesystem path as another; each one is reported.", - Severity.ERROR, - ("resource_kind", "collision_path", "collision_group"), - ) def iter_finding_codes() -> tuple[FindingCode, ...]: diff --git a/dlccore/project/validation/__init__.py b/dlccore/project/validation/__init__.py index ac51098..3f447fe 100644 --- a/dlccore/project/validation/__init__.py +++ b/dlccore/project/validation/__init__.py @@ -1,5 +1,14 @@ -from .context import ValidationContext, build_validation_context -from .project import validate_project +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + from .requests import ( ProjectValidationRequest, ResourceValidationRequest, @@ -9,8 +18,5 @@ __all__ = [ "ProjectValidationRequest", "ResourceValidationRequest", - "ValidationContext", - "build_validation_context", - "validate_project", "validate_project_structure", ] diff --git a/dlccore/project/validation/requests.py b/dlccore/project/validation/requests.py index b21dbe7..95dc0b7 100644 --- a/dlccore/project/validation/requests.py +++ b/dlccore/project/validation/requests.py @@ -1,13 +1,24 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + from __future__ import annotations from pydantic import model_validator from typing_extensions import Self -from dlc_octopus.schemas.base.core import FrozenModel -from dlc_octopus.schemas.base.resources import ( +from dlccore.project.schemas.base.core import FrozenModel +from dlccore.project.schemas.base.resources import ( ResourceKind, ) -from dlc_octopus.schemas.base.validation import ValidationDepth +from dlccore.project.schemas.base.validation import ValidationDepth class ResourceValidationRequest(FrozenModel): diff --git a/dlccore/project/validation/results.py b/dlccore/project/validation/results.py index e7632b5..a56b9bc 100644 --- a/dlccore/project/validation/results.py +++ b/dlccore/project/validation/results.py @@ -1,8 +1,19 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + from __future__ import annotations -from dlc_octopus.schemas.base.core import FrozenModel -from dlc_octopus.schemas.base.findings import Finding -from dlc_octopus.schemas.base.validation import ValidationCheck +from dlccore.project.schemas.base.core import FrozenModel +from dlccore.project.schemas.base.findings import Finding +from dlccore.project.schemas.base.validation import ValidationCheck class ValidationResult(FrozenModel): diff --git a/dlccore/project/validation/structure.py b/dlccore/project/validation/structure.py index 3b2b012..9b245c6 100644 --- a/dlccore/project/validation/structure.py +++ b/dlccore/project/validation/structure.py @@ -1,19 +1,30 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + from __future__ import annotations from pathlib import Path -from dlc_octopus.schemas.base.filesystem import ( +from dlccore.project.schemas.base.filesystem import ( DirectorySchema, LayoutIndex, NodeSchema, ) -from dlc_octopus.schemas.base.finding_codes import StructureFindingCode -from dlc_octopus.schemas.base.findings import Finding -from dlc_octopus.schemas.base.observations import NodeStatus, ObservedNode, walk_observed_nodes -from dlc_octopus.schemas.base.resources import ResourceManagement -from dlc_octopus.schemas.base.symlinks import SymlinkTargetKind -from dlc_octopus.schemas.locations import Location, node_location -from dlc_octopus.schemas.project_layout.project import PROJECT_INDEX +from dlccore.project.schemas.base.finding_codes import StructureFindingCode +from dlccore.project.schemas.base.findings import Finding +from dlccore.project.schemas.base.observations import NodeStatus, ObservedNode, walk_observed_nodes +from dlccore.project.schemas.base.resources import ResourceManagement +from dlccore.project.schemas.base.symlinks import SymlinkTargetKind +from dlccore.project.schemas.locations import Location, node_location +from dlccore.project.schemas.project_layout.project import PROJECT_INDEX def validate_project_structure( diff --git a/tests/project/schemas/test_finding_codes.py b/tests/project/schemas/test_finding_codes.py index 47c163b..feeb202 100644 --- a/tests/project/schemas/test_finding_codes.py +++ b/tests/project/schemas/test_finding_codes.py @@ -17,7 +17,12 @@ import pytest from pydantic import ValidationError -from dlccore.project.schemas.base.finding_codes import FindingCode, Severity, iter_finding_codes +from dlccore.project.schemas.base.finding_codes import ( + FindingCode, + Severity, + StructureFindingCode, + iter_finding_codes, +) from dlccore.project.schemas.base.findings import Finding, Reach _SCHEMA_ID = "dlc.videos.source_video" @@ -196,7 +201,7 @@ def test_an_example_namespace_leaves_the_catalogue_after_its_test() -> None: def test_the_finding_code_catalogue_is_closed() -> None: """The catalogue is closed: adding a namespace is deliberate, so it fails here first.""" - assert set(FindingCode.__subclasses__()) == set() + assert set(FindingCode.__subclasses__()) == {StructureFindingCode} def test_severity_has_three_rungs() -> None: diff --git a/tests/project/validation/test_finding_schema_ids.py b/tests/project/validation/test_finding_schema_ids.py new file mode 100644 index 0000000..90173dd --- /dev/null +++ b/tests/project/validation/test_finding_schema_ids.py @@ -0,0 +1,80 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + +from __future__ import annotations + +from dlccore.project.schemas.base.filesystem import walk_schema +from dlccore.project.schemas.base.finding_codes import ( + StructureFindingCode, +) +from dlccore.project.schemas.base.findings import Finding, Reach +from dlccore.project.schemas.locations import all_of, one_of, one_per_project +from dlccore.project.schemas.project_layout.project import PROJECT, PROJECT_INDEX + +_DECLARED = frozenset(schema.schema_id for schema, _ in walk_schema(PROJECT)) +_NODES = PROJECT_INDEX.schemas + + +# Moving a code to another node changes every consumer's scope, so each non-structural code's +# anchor is pinned here. Structure codes anchor at the node they are about. + + +def _undeclared_schema_ids(findings: tuple[Finding, ...]) -> set[str]: + return {finding.schema_id for finding in findings} - _DECLARED + + +def _misfit_reaches(findings: tuple[Finding, ...]) -> set[tuple[str, Reach]]: + """Return the declared nodes whose emitted reach the layout location helpers would refuse.""" + + misfits: set[tuple[str, Reach]] = set() + + for finding in findings: + node = _NODES.get(finding.schema_id) + + if node is None: + continue + + try: + if finding.reach is Reach.ONE_PER_PROJECT: + one_per_project(node) + elif finding.reach is Reach.ONE_OF: + one_of(node, finding.instance) + else: + all_of(node, finding.instance) + except ValueError: + misfits.add((finding.schema_id, finding.reach)) + + return misfits + + +def test_an_undeclared_schema_id_is_detected() -> None: + code = StructureFindingCode.PROJECT_NODE_UNREADABLE + planted = Finding( + code=code, + schema_id="dlc.not_declared_anywhere", + reach=Reach.ONE_PER_PROJECT, + evidence=dict.fromkeys(code.evidence_keys, "placeholder"), + ) + + assert _undeclared_schema_ids((planted,)) == {"dlc.not_declared_anywhere"} + + +def test_a_reach_that_does_not_fit_the_layout_is_detected() -> None: + code = StructureFindingCode.PROJECT_NODE_UNREADABLE + # The model accepts this, since it does not know the layout: only the helpers would refuse it. + planted = Finding( + code=code, + schema_id="dlc.videos.source_video", + reach=Reach.ONE_PER_PROJECT, + evidence=dict.fromkeys(code.evidence_keys, "placeholder"), + ) + + assert _misfit_reaches((planted,)) == {("dlc.videos.source_video", Reach.ONE_PER_PROJECT)} diff --git a/tests/project/validation/test_requests.py b/tests/project/validation/test_requests.py index c934e37..6806fa5 100644 --- a/tests/project/validation/test_requests.py +++ b/tests/project/validation/test_requests.py @@ -1,15 +1,26 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + from __future__ import annotations import pytest from pydantic import ValidationError -from dlc_octopus.schemas.base.resources import ( +from dlccore.project.schemas.base.resources import ( DirectoryResourceKind, ) -from dlc_octopus.schemas.base.validation import ( +from dlccore.project.schemas.base.validation import ( ValidationDepth, ) -from dlc_octopus.validation import ( +from dlccore.project.validation import ( ProjectValidationRequest, ResourceValidationRequest, ) diff --git a/tests/project/validation/test_structure.py b/tests/project/validation/test_structure.py index 1c4bb52..438e321 100644 --- a/tests/project/validation/test_structure.py +++ b/tests/project/validation/test_structure.py @@ -1,40 +1,51 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + from __future__ import annotations from pathlib import Path -from dlc_octopus.discovery.observer import ( +from dlccore.project.discovery.observer import ( ExpectedEntryKind, discover_project, status_for_expected_entry, ) -from dlc_octopus.schemas.base.filesystem import ( +from dlccore.project.schemas.base.filesystem import ( DirectorySchema, FileSchema, NamePattern, RepeatedNodeSchema, index_layout, ) -from dlc_octopus.schemas.base.findings import ( +from dlccore.project.schemas.base.findings import ( Severity, ) -from dlc_octopus.schemas.base.observations import ( +from dlccore.project.schemas.base.observations import ( NodeStatus, ObservedNode, ) -from dlc_octopus.schemas.base.probes import ( +from dlccore.project.schemas.base.probes import ( FilesystemEntryKind, PathProbe, PathProbeStatus, ) -from dlc_octopus.schemas.base.resources import ( +from dlccore.project.schemas.base.resources import ( DirectoryResourceKind, FileResourceKind, ) -from dlc_octopus.schemas.base.symlinks import ( +from dlccore.project.schemas.base.symlinks import ( SymlinkObservation, SymlinkTargetKind, ) -from dlc_octopus.validation.structure import ( +from dlccore.project.validation.structure import ( validate_project_structure, )