Skip to content

Commit

Permalink
test doap serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 11, 2023
1 parent d995c11 commit 997bc60
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
6 changes: 3 additions & 3 deletions dsp_permissions_scripts/models/doap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class Doap(BaseModel):

class DoapTarget(BaseModel):
project: str
group: str | None
resource_class: str | None
property: str | None
group: str | None = None
resource_class: str | None = None
property: str | None = None

@model_validator(mode="after")
def assert_correct_combination(self) -> Self:
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/utils/doap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ def deserialize_doaps_of_project(
filepath = _get_file_path(shortcode, mode)
with open(filepath, mode="r", encoding="utf-8") as f:
doaps_as_dict = json.load(f)
doaps_as_dicts = doaps_as_dict.values()[0]
doaps_as_dicts = list(doaps_as_dict.values())[0]
return [Doap.model_validate(d) for d in doaps_as_dicts]
65 changes: 65 additions & 0 deletions tests/test_doap_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest

from dsp_permissions_scripts.models import scope
from dsp_permissions_scripts.models.doap import Doap, DoapTarget
from dsp_permissions_scripts.models.groups import BuiltinGroup
from dsp_permissions_scripts.utils.doap_serialize import (
deserialize_doaps_of_project,
serialize_doaps_of_project,
)
from tests.test_scope_serialization import compare_scopes


class TestDoapSerialization(unittest.TestCase):
def test_doap_serialization(self):
shortcode = "1234"

target1 = DoapTarget(
project="http://rdfh.ch/projects/MsOaiQkcQ7-QPxsYBKckfQ",
group="http://www.knora.org/ontology/knora-admin#ProjectAdmin",
)
scope1 = scope.PermissionScope.create(
CR=[BuiltinGroup.PROJECT_ADMIN],
V=[BuiltinGroup.PROJECT_MEMBER],
)
doap1 = Doap(
target=target1,
scope=scope1,
doap_iri="http://rdfh.ch/doap-1",
)

target2 = DoapTarget(
project="http://rdfh.ch/projects/MsOaiQkcQ7-QPxsYBKckfQ",
group="http://www.knora.org/ontology/knora-admin#ProjectMember",
)
scope2 = scope.PermissionScope.create(
D=[BuiltinGroup.SYSTEM_ADMIN],
M=[BuiltinGroup.KNOWN_USER],
)
doap2 = Doap(
target=target2,
scope=scope2,
doap_iri="http://rdfh.ch/doap-2",
)

serialize_doaps_of_project(
project_doaps=[doap1, doap2],
shortcode=shortcode,
mode="original",
)
deserialized_doaps = deserialize_doaps_of_project(
shortcode=shortcode,
mode="original",
)

self._compare_doaps(deserialized_doaps[0], doap1)
self._compare_doaps(deserialized_doaps[1], doap2)

def _compare_doaps(self, doap1: Doap, doap2: Doap) -> None:
self.assertEqual(doap1.target, doap2.target)
compare_scopes(doap1.scope, doap2.scope)
self.assertEqual(doap1.doap_iri, doap2.doap_iri)


if __name__ == "__main__":
unittest.main()

0 comments on commit 997bc60

Please sign in to comment.