-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
99c76ea
commit 1784065
Showing
6 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import warnings | ||
from dataclasses import dataclass | ||
|
||
from typing_extensions import deprecated | ||
|
||
from dsp_tools.models.custom_warnings import DspToolsUserWarning | ||
from dsp_tools.xmllib.value_checkers import is_dsp_ark | ||
from dsp_tools.xmllib.value_checkers import is_dsp_iri | ||
from dsp_tools.xmllib.value_checkers import is_timestamp | ||
|
||
LIST_SEPARATOR = "\n - " | ||
|
||
|
||
@dataclass | ||
class MigrationMetadata: | ||
creation_date: str | None | ||
iri: str | None | ||
ark: str | None | ||
res_id: str | ||
|
||
@deprecated("This is for salsah migration only and will be deleted in future releases.") | ||
def __post_init__(self) -> None: | ||
msg_list = [] | ||
if self.creation_date and not is_timestamp(self.creation_date): | ||
msg_list.append(f"The value for creation date is not a valid timestamp: {self.creation_date}") | ||
if self.iri and not is_dsp_iri(self.iri): | ||
msg_list.append(f"The provided IRI is not valid: {self.iri}") | ||
if self.ark and not is_dsp_ark(self.ark): | ||
msg_list.append(f"The provided ARK is not valid: {self.ark}") | ||
if msg_list: | ||
msg = ( | ||
f"The migration metadata of the resource with the ID '{self.res_id}' has the following problem(s):" | ||
f"{LIST_SEPARATOR}{LIST_SEPARATOR.join(msg_list)}" | ||
) | ||
warnings.warn(DspToolsUserWarning(msg)) | ||
|
||
def as_attrib(self) -> dict[str, str]: | ||
attrib_dict = {} | ||
if self.creation_date: | ||
attrib_dict["creation_date"] = self.creation_date | ||
if self.iri: | ||
attrib_dict["iri"] = self.iri | ||
if self.ark: | ||
attrib_dict["ark"] = self.ark | ||
if not attrib_dict: | ||
msg = ( | ||
f"The metadata of the resource with the ID '{self.res_id}' does not contain any values. " | ||
f"Please check if an error occurred." | ||
) | ||
warnings.warn(DspToolsUserWarning(msg)) | ||
return attrib_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import warnings | ||
from warnings import WarningMessage | ||
|
||
import pytest | ||
|
||
from dsp_tools.models.custom_warnings import DspToolsUserWarning | ||
from dsp_tools.xmllib.models.migration_metadata import MigrationMetadata | ||
|
||
|
||
def test_migration_metadata_creation_date_good() -> None: | ||
with warnings.catch_warnings(record=True) as caught_warnings: | ||
MigrationMetadata("2019-01-09T15:45:54.502951Z", None, None, "id") | ||
assert len(caught_warnings) == 1 | ||
assert isinstance(caught_warnings[0], WarningMessage) | ||
|
||
|
||
def test_migration_metadata_iri_good() -> None: | ||
with warnings.catch_warnings(record=True) as caught_warnings: | ||
MigrationMetadata(None, "http://rdfh.ch/4123/TqAnYQzrSzC2ctT06OJMYB", None, "id") | ||
assert len(caught_warnings) == 1 | ||
assert isinstance(caught_warnings[0], WarningMessage) | ||
|
||
|
||
def test_migration_metadata_ark_good() -> None: | ||
with warnings.catch_warnings(record=True) as caught_warnings: | ||
MigrationMetadata(None, None, "ark:/72163/4123-43xc6ivb931-a.2022829", "id") | ||
assert len(caught_warnings) == 1 | ||
assert isinstance(caught_warnings[0], WarningMessage) | ||
|
||
|
||
def test_migration_metadata_creation_date_warns() -> None: | ||
with pytest.warns(DspToolsUserWarning): | ||
MigrationMetadata("2019-01-054.502951Z", None, None, "id") | ||
|
||
|
||
def test_migration_metadata_iri_warns() -> None: | ||
with pytest.warns(DspToolsUserWarning): | ||
MigrationMetadata(None, "http:123/TqAnYQzrSzC2ctT06OJMYB", None, "id") | ||
|
||
|
||
def test_migration_metadata_ark_warns() -> None: | ||
with pytest.warns(DspToolsUserWarning): | ||
MigrationMetadata(None, None, "163/4123-43xc6ivb931-a.2022829", "id") | ||
|
||
|
||
def test_migration_metadata_as_attrib_empty() -> None: | ||
with pytest.warns(DspToolsUserWarning): | ||
result = MigrationMetadata(None, None, None, "id").as_attrib() | ||
assert not result | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters