Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Apr 17, 2024
1 parent 32f644b commit 28edb40
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 180 deletions.
26 changes: 15 additions & 11 deletions tests/test_ap_serialization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import json
import re
import shutil
import unittest
from pathlib import Path
from typing import Iterator

import pytest

from dsp_permissions_scripts.ap.ap_model import Ap
from dsp_permissions_scripts.ap.ap_model import ApValue
Expand All @@ -10,8 +14,6 @@
from dsp_permissions_scripts.models import group
from dsp_permissions_scripts.models.host import Hosts

# ruff: noqa: PT009 (pytest-unittest-assertion) (remove this line when pytest is used instead of unittest)


class TestApSerialization(unittest.TestCase):
shortcode = "1234"
Expand All @@ -34,13 +36,14 @@ class TestApSerialization(unittest.TestCase):
iri="http://rdfh.ch/ap-2",
)

def setUp(self) -> None:
@pytest.fixture()
def _setup_teardown(self) -> Iterator[None]:
self.output_dir.mkdir(parents=True, exist_ok=True)

def tearDown(self) -> None:
yield
if self.output_dir.is_dir():
shutil.rmtree(self.output_dir)

@pytest.mark.usefixtures("_setup_teardown")
def test_serialize_aps_of_project(self) -> None:
serialize_aps_of_project(
project_aps=[self.ap1, self.ap2],
Expand All @@ -51,20 +54,21 @@ def test_serialize_aps_of_project(self) -> None:
with open(self.output_file, mode="r", encoding="utf-8") as f:
aps_file = json.load(f)
explanation_text = next(iter(aps_file.keys()))
self.assertRegex(explanation_text, r"Project 1234 on host .+ has \d+ APs")
assert re.search(r"Project 1234 on host .+ has \d+ APs", explanation_text)
aps_as_dicts = aps_file[explanation_text]
self.assertEqual(self.ap1, Ap.model_validate(aps_as_dicts[0]))
self.assertEqual(self.ap2, Ap.model_validate(aps_as_dicts[1]))
assert self.ap1 == Ap.model_validate(aps_as_dicts[0])
assert self.ap2 == Ap.model_validate(aps_as_dicts[1])

@pytest.mark.usefixtures("_setup_teardown")
def test_deserialize_aps_of_project(self) -> None:
shutil.copy(src=self.testdata_file, dst=self.output_file)
aps = deserialize_aps_of_project(
shortcode=self.shortcode,
mode="original",
)
self.assertEqual(self.ap1, aps[0])
self.assertEqual(self.ap2, aps[1])
assert self.ap1 == aps[0]
assert self.ap2 == aps[1]


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
16 changes: 10 additions & 6 deletions tests/test_doap_serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import shutil
import unittest
from pathlib import Path
from typing import Iterator

import pytest

from dsp_permissions_scripts.doap.doap_model import Doap
from dsp_permissions_scripts.doap.doap_model import DoapTarget
Expand All @@ -11,17 +14,18 @@
from dsp_permissions_scripts.models.scope import PermissionScope
from tests.test_scope_serialization import compare_scopes

# ruff: noqa: PT009 (pytest-unittest-assertion) (remove this line when pytest is used instead of unittest)


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

def tearDown(self) -> None:
@pytest.fixture()
def _setup_teardown(self) -> Iterator[None]:
yield
testdata_dir = Path(f"project_data/{self.shortcode}")
if testdata_dir.is_dir():
shutil.rmtree(testdata_dir)

@pytest.mark.usefixtures("_setup_teardown")
def test_doap_serialization(self) -> None:
doap1 = Doap(
target=DoapTarget(
Expand Down Expand Up @@ -59,10 +63,10 @@ def test_doap_serialization(self) -> None:
self._compare_doaps(deserialized_doaps[1], doap2)

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


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
57 changes: 27 additions & 30 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import unittest
import pytest

from dsp_permissions_scripts.models import group
from dsp_permissions_scripts.utils.helpers import sort_groups

# ruff: noqa: PT009 (pytest-unittest-assertion) (remove this line when pytest is used instead of unittest)


class TestHelpers(unittest.TestCase):
def test_sort_groups(self) -> None:
groups_original = [
group.Group(val="http://www.knora.org/ontology/knora-admin#C_CustomGroup"),
group.UNKNOWN_USER,
group.PROJECT_ADMIN,
group.PROJECT_MEMBER,
group.CREATOR,
group.Group(val="http://www.knora.org/ontology/knora-admin#A_CustomGroup"),
group.Group(val="http://www.knora.org/ontology/knora-admin#B_CustomGroup"),
group.KNOWN_USER,
group.SYSTEM_ADMIN,
]
groups_expected = [
group.SYSTEM_ADMIN,
group.CREATOR,
group.PROJECT_ADMIN,
group.PROJECT_MEMBER,
group.KNOWN_USER,
group.UNKNOWN_USER,
group.Group(val="http://www.knora.org/ontology/knora-admin#A_CustomGroup"),
group.Group(val="http://www.knora.org/ontology/knora-admin#B_CustomGroup"),
group.Group(val="http://www.knora.org/ontology/knora-admin#C_CustomGroup"),
]
groups_returned = sort_groups(groups_original)
self.assertEqual(groups_returned, groups_expected)
def test_sort_groups() -> None:
groups_original = [
group.Group(val="http://www.knora.org/ontology/knora-admin#C_CustomGroup"),
group.UNKNOWN_USER,
group.PROJECT_ADMIN,
group.PROJECT_MEMBER,
group.CREATOR,
group.Group(val="http://www.knora.org/ontology/knora-admin#A_CustomGroup"),
group.Group(val="http://www.knora.org/ontology/knora-admin#B_CustomGroup"),
group.KNOWN_USER,
group.SYSTEM_ADMIN,
]
groups_expected = [
group.SYSTEM_ADMIN,
group.CREATOR,
group.PROJECT_ADMIN,
group.PROJECT_MEMBER,
group.KNOWN_USER,
group.UNKNOWN_USER,
group.Group(val="http://www.knora.org/ontology/knora-admin#A_CustomGroup"),
group.Group(val="http://www.knora.org/ontology/knora-admin#B_CustomGroup"),
group.Group(val="http://www.knora.org/ontology/knora-admin#C_CustomGroup"),
]
groups_returned = sort_groups(groups_original)
assert groups_returned == groups_expected


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
Loading

0 comments on commit 28edb40

Please sign in to comment.