From 0b09fbb5b55c5be06ec07f6d3e363ebe4778ee00 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Fri, 19 Apr 2024 11:16:51 +0200 Subject: [PATCH] edit --- tests/test_doap_serialization.py | 12 ++----- tests/test_oap_serialization.py | 25 ++------------- tests/test_scope.py | 53 +++++++++++++++---------------- tests/test_scope_serialization.py | 25 ++------------- 4 files changed, 34 insertions(+), 81 deletions(-) diff --git a/tests/test_doap_serialization.py b/tests/test_doap_serialization.py index e2126c9c..6ca841e5 100644 --- a/tests/test_doap_serialization.py +++ b/tests/test_doap_serialization.py @@ -11,7 +11,6 @@ from dsp_permissions_scripts.models import group from dsp_permissions_scripts.models.host import Hosts from dsp_permissions_scripts.models.scope import PermissionScope -from tests.test_scope_serialization import compare_scopes class TestDoapSerialization: @@ -47,8 +46,9 @@ def test_doap_serialization(self) -> None: ), doap_iri="http://rdfh.ch/doap-2", ) + doaps_original = [doap1, doap2] serialize_doaps_of_project( - project_doaps=[doap1, doap2], + project_doaps=doaps_original, shortcode=self.shortcode, mode="original", host=Hosts.LOCALHOST, @@ -57,13 +57,7 @@ def test_doap_serialization(self) -> None: shortcode=self.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: - assert doap1.target == doap2.target - compare_scopes(doap1.scope, doap2.scope) - assert doap1.doap_iri == doap2.doap_iri + assert doaps_original == deserialized_doaps if __name__ == "__main__": diff --git a/tests/test_oap_serialization.py b/tests/test_oap_serialization.py index 367205d0..79c92bf3 100644 --- a/tests/test_oap_serialization.py +++ b/tests/test_oap_serialization.py @@ -11,7 +11,6 @@ from dsp_permissions_scripts.oap.oap_model import ValueOap from dsp_permissions_scripts.oap.oap_serialize import deserialize_oaps from dsp_permissions_scripts.oap.oap_serialize import serialize_oaps -from tests.test_scope_serialization import compare_scopes class TestOapSerialization: @@ -28,12 +27,10 @@ def test_oap_serialization(self) -> None: oap1 = self._get_oap_one_value_only() oap2 = self._get_oap_full() oap3 = self._get_oap_res_only() - - serialize_oaps([oap1, oap2, oap3], self.shortcode, "original") + oaps_original = [oap1, oap2, oap3] + serialize_oaps(oaps_original, self.shortcode, "original") deserialized_oaps = deserialize_oaps(self.shortcode, "original") - self._compare_oaps(deserialized_oaps[0], oap1) - self._compare_oaps(deserialized_oaps[1], oap2) - self._compare_oaps(deserialized_oaps[2], oap3) + assert oaps_original == deserialized_oaps def _get_oap_full(self) -> Oap: scope = PermissionScope.create(CR=[group.PROJECT_ADMIN], V=[group.PROJECT_MEMBER]) @@ -74,22 +71,6 @@ def _get_oap_res_only(self) -> Oap: res_oap = ResourceOap(scope=scope, resource_iri=res_iri) return Oap(resource_oap=res_oap, value_oaps=[]) - def _compare_oaps(self, oap1: Oap, oap2: Oap) -> None: - if oap1.resource_oap is None: - assert oap2.resource_oap is None - elif oap2.resource_oap is None: - assert oap1.resource_oap is None - else: - assert oap1.resource_oap.resource_iri == oap2.resource_oap.resource_iri - compare_scopes(oap1.resource_oap.scope, oap2.resource_oap.scope) - - assert len(oap1.value_oaps) == len(oap2.value_oaps) - for val_oap1, val_oap2 in zip(oap1.value_oaps, oap2.value_oaps): - assert val_oap1.value_iri == val_oap2.value_iri - assert val_oap1.property == val_oap2.property - assert val_oap1.value_type == val_oap2.value_type - compare_scopes(val_oap1.scope, val_oap2.scope) - if __name__ == "__main__": pytest.main([__file__]) diff --git a/tests/test_scope.py b/tests/test_scope.py index 5a51cf2a..f9436adb 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -4,7 +4,6 @@ from dsp_permissions_scripts.models import group from dsp_permissions_scripts.models.scope import PermissionScope -from tests.test_scope_serialization import compare_scopes def test_scope_validation_on_creation() -> None: @@ -17,7 +16,20 @@ def test_scope_validation_on_creation() -> None: class TestAdd: - def test_scope_validation_on_add_to_same_permission(self) -> None: + def test_add_to_scope(self) -> None: + scope = PermissionScope.create( + D={group.SYSTEM_ADMIN}, + M={group.PROJECT_MEMBER, group.KNOWN_USER}, + ) + scope = scope.add("CR", group.PROJECT_ADMIN) + scope_expected = PermissionScope.create( + CR={group.PROJECT_ADMIN}, + D={group.SYSTEM_ADMIN}, + M={group.PROJECT_MEMBER, group.KNOWN_USER}, + ) + assert scope == scope_expected + + def test_add_same_group_to_same_permission(self) -> None: scope = PermissionScope.create( CR={group.PROJECT_ADMIN}, V={group.UNKNOWN_USER, group.KNOWN_USER}, @@ -26,7 +38,7 @@ def test_scope_validation_on_add_to_same_permission(self) -> None: with pytest.raises(ValueError, match=re.escape(rgx)): _ = scope.add("CR", group.PROJECT_ADMIN) - def test_scope_validation_on_add_to_different_permission(self) -> None: + def test_add_same_group_to_different_permission(self) -> None: scope = PermissionScope.create( CR={group.PROJECT_ADMIN}, V={group.UNKNOWN_USER, group.KNOWN_USER}, @@ -34,23 +46,21 @@ def test_scope_validation_on_add_to_different_permission(self) -> None: with pytest.raises(ValueError, match=re.escape("must not occur in more than one field")): _ = scope.add("RV", group.PROJECT_ADMIN) - def test_add_to_scope(self) -> None: + +class TestRemove: + def test_remove_from_scope(self) -> None: scope = PermissionScope.create( + CR={group.PROJECT_ADMIN}, D={group.SYSTEM_ADMIN}, M={group.PROJECT_MEMBER, group.KNOWN_USER}, ) - scope_added = scope.add("CR", group.PROJECT_ADMIN) - compare_scopes( - scope1=scope_added, - scope2=PermissionScope.create( - CR={group.PROJECT_ADMIN}, - D={group.SYSTEM_ADMIN}, - M={group.PROJECT_MEMBER, group.KNOWN_USER}, - ), + scope = scope.remove("CR", group.PROJECT_ADMIN) + scope_expected = PermissionScope.create( + D={group.SYSTEM_ADMIN}, + M={group.PROJECT_MEMBER, group.KNOWN_USER}, ) + assert scope == scope_expected - -class TestRemove: def test_remove_inexisting_group(self) -> None: scope = PermissionScope.create( D={group.SYSTEM_ADMIN}, @@ -67,21 +77,8 @@ def test_remove_from_empty_perm(self) -> None: with pytest.raises(ValueError, match=re.escape("is not in permission 'CR'")): _ = scope.remove("CR", group.PROJECT_ADMIN) - def test_remove_from_scope(self) -> None: - scope = PermissionScope.create( - CR={group.PROJECT_ADMIN}, - D={group.SYSTEM_ADMIN}, - M={group.PROJECT_MEMBER, group.KNOWN_USER}, - ) - scope_removed = scope.remove("CR", group.PROJECT_ADMIN) - compare_scopes( - scope1=scope_removed, - scope2=PermissionScope.create( - D={group.SYSTEM_ADMIN}, - M={group.PROJECT_MEMBER, group.KNOWN_USER}, - ), - ) +class TestRemoveDuplicatesFromKwargs: def test_remove_duplicates_from_kwargs_CR(self) -> None: original: dict[str, list[str]] = { "CR": ["knora-admin:ProjectAdmin"], diff --git a/tests/test_scope_serialization.py b/tests/test_scope_serialization.py index bb0ddae7..c8faa35c 100644 --- a/tests/test_scope_serialization.py +++ b/tests/test_scope_serialization.py @@ -11,18 +11,6 @@ from dsp_permissions_scripts.utils.scope_serialization import create_string_from_scope -def compare_scopes( - scope1: PermissionScope, - scope2: PermissionScope, - msg: str | None = None, -) -> None: - scope1_dict = scope1.model_dump(mode="json") - scope1_dict = {k: sorted(v, key=lambda x: x["val"]) for k, v in scope1_dict.items()} - scope2_dict = scope2.model_dump(mode="json") - scope2_dict = {k: sorted(v, key=lambda x: x["val"]) for k, v in scope2_dict.items()} - assert scope1_dict == scope2_dict, msg - - class TestScopeSerialization: perm_strings = ( "CR knora-admin:SystemAdmin|V knora-admin:CustomGroup", @@ -75,19 +63,12 @@ class TestScopeSerialization: def test_create_scope_from_string(self) -> None: for perm_string, scope in zip(self.perm_strings, self.scopes): - compare_scopes( - scope1=create_scope_from_string(perm_string), - scope2=scope, - msg=f"Failed with permission string '{perm_string}'", - ) + assert create_scope_from_string(perm_string) == scope, f"Failed with permission string '{perm_string}'" def test_create_scope_from_admin_route_object(self) -> None: for admin_route_object, scope, index in zip(self.admin_route_objects, self.scopes, range(len(self.scopes))): - compare_scopes( - scope1=create_scope_from_admin_route_object(admin_route_object), - scope2=scope, - msg=f"Failed with admin group object no. {index}", - ) + fail_msg = f"Failed with admin group object no. {index}" + assert create_scope_from_admin_route_object(admin_route_object) == scope, fail_msg def test_create_string_from_scope(self) -> None: for perm_string, scope in zip(self.perm_strings, self.scopes):