diff --git a/src/dandisets_linkml_status_tools/cli/models.py b/src/dandisets_linkml_status_tools/cli/models.py index c2d68fa..e526bc8 100644 --- a/src/dandisets_linkml_status_tools/cli/models.py +++ b/src/dandisets_linkml_status_tools/cli/models.py @@ -1,6 +1,6 @@ from collections.abc import Sequence from datetime import datetime -from typing import Annotated, Any, Union +from typing import Annotated, Any, NamedTuple, Union from dandi.dandiapi import VersionStatus from jsonschema.exceptions import ValidationError @@ -160,3 +160,26 @@ def dandiset_schema_version(self) -> str: # Errors encountered in validation against the dandiset metadata model in LinkML linkml_validation_errs: LinkmlValidationErrsType = [] + + +class JsonschemaValidationErrorType(NamedTuple): + """ + A named tuple for representing types of `jsonschema.exceptions.ValidationError` + objects. + + The type of a `jsonschema.exceptions.ValidationError` is decided by the value of its + `validator` field and the value of its `validator_value` field. The values + of these fields are bundled in an instance of this named tuple to represent a type + of `jsonschema.exceptions.ValidationError` objects. + """ + + validator: str + validator_value: Any + + def __eq__(self, other: object) -> bool: + return ( + isinstance(other, JsonschemaValidationErrorType) + and self.validator == other.validator + and type(self.validator_value) is type(other.validator_value) # noqa E721 + and self.validator_value == other.validator_value + ) diff --git a/tests/test_cli/test_models.py b/tests/test_cli/test_models.py new file mode 100644 index 0000000..fef40f6 --- /dev/null +++ b/tests/test_cli/test_models.py @@ -0,0 +1,41 @@ +import pytest + +from dandisets_linkml_status_tools.cli.models import JsonschemaValidationErrorType + + +@pytest.mark.parametrize( + ("op1", "op2", "expected_result"), + [ + (JsonschemaValidationErrorType("integer", [1, 2]), "hello", False), + ( + JsonschemaValidationErrorType("integer", 1), + JsonschemaValidationErrorType("string", 1), + False, + ), + ( + JsonschemaValidationErrorType("integer", 1), + JsonschemaValidationErrorType("integer", "1"), + False, + ), + ( + JsonschemaValidationErrorType("integer", 1), + JsonschemaValidationErrorType("integer", 2), + False, + ), + ( + JsonschemaValidationErrorType("integer", 42), + JsonschemaValidationErrorType("integer", 42), + True, + ), + ( + JsonschemaValidationErrorType("integer", [1, 2, 3]), + JsonschemaValidationErrorType("integer", [1, 2, 3]), + True, + ), + ], +) +def test_jsonschema_validation_error_type_equality(op1, op2, expected_result): + """ + Test the equal operator of the `JsonschemaValidationErrorType` class + """ + assert (op1 == op2) == expected_result