Skip to content

Commit

Permalink
InvalidEntityDoapTargetError
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Nov 12, 2024
1 parent 869e3c3 commit dd5798d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
14 changes: 3 additions & 11 deletions dsp_permissions_scripts/doap/doap_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pydantic import model_validator

from dsp_permissions_scripts.models.errors import EmptyDoapTargetError
from dsp_permissions_scripts.models.errors import InvalidEntityDoapTargetError
from dsp_permissions_scripts.models.group import PREFIXED_IRI_REGEX
from dsp_permissions_scripts.models.group import Group
from dsp_permissions_scripts.models.scope import PermissionScope
Expand Down Expand Up @@ -47,19 +48,10 @@ def _validate_iri_format(self) -> Self:
r"^http://api\.(.+\.)?dasch\.swiss/ontology/[A-Fa-f0-9]{4}/[^/#]+/v2#[^/#]+$",
r"^http://api\.knora\.org/ontology/knora-api/v2#[^/#]+$",
]
iri_formats = [
"http://0.0.0.0:3333/ontology/<shortcode>/<ontoname>/v2#<classname_or_property_name>",
"http://api.<subdomain>.dasch.swiss/ontology/<shortcode>/<ontoname>/v2#<classname_or_property_name>",
"http://api.knora.org/ontology/knora-api/v2#<knora_base_class_or_base_property>",
]
if self.resclass_iri and not any(re.search(r, self.resclass_iri) for r in regexes):
raise ValueError(
f"The IRI must be in one of the formats {iri_formats}, but you provided {self.resclass_iri}"
)
raise InvalidEntityDoapTargetError(self.resclass_iri)
if self.property_iri and not any(re.search(r, self.property_iri) for r in regexes):
raise ValueError(
f"The IRI must be in one of the formats {iri_formats}, but you provided {self.property_iri}"
)
raise InvalidEntityDoapTargetError(self.property_iri)
return self


Expand Down
12 changes: 12 additions & 0 deletions dsp_permissions_scripts/models/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ class InvalidIRIError(Exception):
@dataclass
class EmptyDoapTargetError(Exception):
message = "At least one of resource_class or property must be set"


class InvalidEntityDoapTargetError(Exception):
message: str

def __init__(self, resclass_iri: str) -> None:
iri_formats = [
"http://0.0.0.0:3333/ontology/<shortcode>/<ontoname>/v2#<classname_or_property_name>",
"http://api.<subdomain>.dasch.swiss/ontology/<shortcode>/<ontoname>/v2#<classname_or_property_name>",
"http://api.knora.org/ontology/knora-api/v2#<knora_base_class_or_base_property>",
]
self.message = f"The IRI must be in one of the formats {iri_formats}, but you provided {resclass_iri}"
5 changes: 3 additions & 2 deletions tests/test_doap_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dsp_permissions_scripts.doap.doap_model import EntityDoapTarget
from dsp_permissions_scripts.doap.doap_model import NewEntityDoapTarget
from dsp_permissions_scripts.models.errors import EmptyDoapTargetError
from dsp_permissions_scripts.models.errors import InvalidEntityDoapTargetError

SHORTCODE = "0000"
ONTO_NAME = "limc"
Expand Down Expand Up @@ -58,9 +59,9 @@ def test_valid_knora_base(self, resclass: str | None, prop: str | None) -> None:
],
)
def test_invalid(self, inv: str) -> None:
with pytest.raises(ValueError, match="IRI must be in one of the formats"):
with pytest.raises(InvalidEntityDoapTargetError):
_ = EntityDoapTarget(project_iri=PROJ_IRI, resclass_iri=inv)
with pytest.raises(ValueError, match="IRI must be in one of the formats"):
with pytest.raises(InvalidEntityDoapTargetError):
_ = EntityDoapTarget(project_iri=PROJ_IRI, property_iri=inv)


Expand Down

0 comments on commit dd5798d

Please sign in to comment.