import pytest from apps.catalog.domain.entities import Service, ServiceUnit, WorkType from apps.catalog.domain.exceptions import CannotMove, InvalidCatalogName, ServiceNotInUnit from apps.catalog.domain.ordering import Direction, move from apps.catalog.domain.value_objects import CatalogName class TestCatalogName: def test_spaces_are_normalized(self) -> None: assert CatalogName(" Graafinen työ ").value == "Graafinen työ" @pytest.mark.parametrize("value", ["", " ", "x" * 101]) def test_invalid(self, value: str) -> None: with pytest.raises(InvalidCatalogName): CatalogName(value) def test_same_as_ignores_case_and_spaces(self) -> None: assert CatalogName("Editointi").same_as(" EDITOINTI ") assert not CatalogName("Editointi").same_as("Kuvaus") class TestMove: def test_up_and_down(self) -> None: assert move([1, 2, 3], 2, Direction.UP) == [2, 1, 3] assert move([1, 2, 3], 2, Direction.DOWN) == [1, 3, 2] def test_edges(self) -> None: with pytest.raises(CannotMove): move([1, 2], 1, Direction.UP) with pytest.raises(CannotMove): move([1, 2], 2, Direction.DOWN) def service(id: int, unit_id: int) -> Service: return Service.reconstitute( id=id, service_unit_id=unit_id, name=CatalogName("S"), is_active=True, sort_order=1 ) class TestWorkType: def test_service_must_belong_to_the_same_unit(self) -> None: with pytest.raises(ServiceNotInUnit): WorkType.register( service_unit_id=1, service=service(5, 2), name=CatalogName("X"), sort_order=1 ) def test_move_between_service_and_unit(self) -> None: wt = WorkType.register( service_unit_id=1, service=service(5, 1), name=CatalogName("X"), sort_order=1 ) assert wt.service_id == 5 moved = wt.move_to(None, sort_order=7) assert (moved.service_id, moved.sort_order) == (None, 7) with pytest.raises(ServiceNotInUnit): moved.move_to(service(6, 3), sort_order=1) def test_deactivate_and_activate(self) -> None: unit = ServiceUnit.register(name=CatalogName("U"), sort_order=1) assert unit.is_active assert not unit.with_active(False).is_active assert unit.with_active(False).with_active(True).is_active