import pytest from apps.catalog.application.dto import ServiceData, ServiceUnitData, WorkTypeData from apps.catalog.application.use_cases import ( AddService, AddServiceUnit, AddWorkType, MoveItem, RenameService, RenameServiceUnit, SetActive, UpdateWorkType, ) from apps.catalog.domain.exceptions import ( CannotMove, ServiceNameTaken, ServiceNotFound, ServiceNotInUnit, ServiceUnitNameTaken, ServiceUnitNotFound, WorkTypeNameTaken, WorkTypeNotFound, ) from apps.catalog.domain.ordering import Direction from apps.catalog.tests.fakes import ( InMemoryServiceRepository, InMemoryServiceUnitRepository, InMemoryWorkTypeRepository, ) from apps.shared.tests.fakes import FakeUnitOfWork class Catalog: def __init__(self) -> None: self.units = InMemoryServiceUnitRepository() self.services = InMemoryServiceRepository() self.work_types = InMemoryWorkTypeRepository() def unit(self, name: str) -> int: return AddServiceUnit(self.units, FakeUnitOfWork()).execute(ServiceUnitData(name)) def service(self, unit_id: int, name: str) -> int: return AddService(self.units, self.services, FakeUnitOfWork()).execute( unit_id, ServiceData(name) ) def work_type(self, unit_id: int, name: str, service_id: int | None = None) -> int: return AddWorkType(self.units, self.services, self.work_types, FakeUnitOfWork()).execute( unit_id, WorkTypeData(name, service_id) ) def update_work_type(self, work_type_id: int, name: str, service_id: int | None) -> None: UpdateWorkType(self.units, self.services, self.work_types, FakeUnitOfWork()).execute( work_type_id, WorkTypeData(name, service_id) ) @pytest.fixture def cat() -> Catalog: return Catalog() class TestServiceUnits: def test_new_units_go_last(self, cat: Catalog) -> None: cat.unit("Markkinointi") cat.unit("Painotuotanto") assert cat.units.ordered_names(()) == ["Markkinointi", "Painotuotanto"] def test_name_is_unique_case_insensitively(self, cat: Catalog) -> None: cat.unit("Markkinointi") with pytest.raises(ServiceUnitNameTaken): cat.unit("markkinointi") def test_rename(self, cat: Catalog) -> None: a = cat.unit("Markkinointi") b = cat.unit("Painotuotanto") rename = RenameServiceUnit(cat.units, FakeUnitOfWork()) rename.execute(a, ServiceUnitData("MARKKINOINTI")) # own name in another case is fine assert cat.units.items[a].name.value == "MARKKINOINTI" with pytest.raises(ServiceUnitNameTaken): rename.execute(b, ServiceUnitData("markkinointi")) with pytest.raises(ServiceUnitNotFound): rename.execute(99, ServiceUnitData("X")) class TestServices: def test_name_unique_within_unit_only(self, cat: Catalog) -> None: a, b = cat.unit("Mediatuotanto"), cat.unit("Kuluttajapalvelut") cat.service(a, "Valokuvaus") cat.service(b, "Valokuvaus") with pytest.raises(ServiceNameTaken): cat.service(a, "VALOKUVAUS") def test_unknown_unit(self, cat: Catalog) -> None: with pytest.raises(ServiceUnitNotFound): cat.service(5, "Valokuvaus") def test_rename(self, cat: Catalog) -> None: a = cat.unit("Mediatuotanto") s1 = cat.service(a, "Valokuvaus") cat.service(a, "Videotuotanto") rename = RenameService(cat.services, FakeUnitOfWork()) with pytest.raises(ServiceNameTaken): rename.execute(s1, ServiceData("videotuotanto")) rename.execute(s1, ServiceData("Kuvaus")) assert cat.services.items[s1].name.value == "Kuvaus" class TestWorkTypes: def test_same_name_allowed_on_different_levels(self, cat: Catalog) -> None: unit = cat.unit("Mediatuotanto") video, photo = cat.service(unit, "Videotuotanto"), cat.service(unit, "Valokuvaus") cat.work_type(unit, "Editointi", video) cat.work_type(unit, "Editointi", photo) cat.work_type(unit, "Editointi") with pytest.raises(WorkTypeNameTaken): cat.work_type(unit, "editointi", video) def test_service_must_exist_and_belong_to_unit(self, cat: Catalog) -> None: a, b = cat.unit("A"), cat.unit("B") other = cat.service(b, "Palvelu") with pytest.raises(ServiceNotFound): cat.work_type(a, "X", 99) with pytest.raises(ServiceNotInUnit): cat.work_type(a, "X", other) def test_move_to_unit_and_back_goes_last(self, cat: Catalog) -> None: unit = cat.unit("Mediatuotanto") video = cat.service(unit, "Videotuotanto") cat.work_type(unit, "Suunnittelu") edit = cat.work_type(unit, "Editointi", video) cat.work_type(unit, "Animointi", video) cat.update_work_type(edit, "Editointi", None) assert cat.work_types.ordered_names((unit, None)) == ["Suunnittelu", "Editointi"] assert cat.work_types.ordered_names((unit, video)) == ["Animointi"] cat.update_work_type(edit, "Videoeditointi", video) assert cat.work_types.ordered_names((unit, video)) == ["Animointi", "Videoeditointi"] def test_moving_into_a_level_with_the_same_name_is_rejected(self, cat: Catalog) -> None: unit = cat.unit("Mediatuotanto") video = cat.service(unit, "Videotuotanto") cat.work_type(unit, "Editointi") edit = cat.work_type(unit, "Editointi", video) with pytest.raises(WorkTypeNameTaken): cat.update_work_type(edit, "Editointi", None) assert cat.work_types.items[edit].service_id == video def test_unknown_work_type(self, cat: Catalog) -> None: with pytest.raises(WorkTypeNotFound): cat.update_work_type(1, "X", None) class TestOrderAndActivation: def test_move_up_and_down(self, cat: Catalog) -> None: ids = [cat.unit(n) for n in ("A", "B", "C")] move = MoveItem(cat.units, FakeUnitOfWork(), ServiceUnitNotFound) move.execute(ids[2], Direction.UP) assert cat.units.ordered_names(()) == ["A", "C", "B"] move.execute(ids[0], Direction.DOWN) assert cat.units.ordered_names(()) == ["C", "A", "B"] with pytest.raises(CannotMove): move.execute(ids[1], Direction.DOWN) with pytest.raises(ServiceUnitNotFound): move.execute(99, Direction.UP) def test_move_stays_within_siblings(self, cat: Catalog) -> None: unit = cat.unit("U") service = cat.service(unit, "S") direct = cat.work_type(unit, "Suora") first = cat.work_type(unit, "Eka", service) with pytest.raises(CannotMove): # only work type directly under the unit MoveItem(cat.work_types, FakeUnitOfWork(), WorkTypeNotFound).execute( direct, Direction.DOWN ) assert cat.work_types.sibling_ids(cat.work_types.items[first]) == [first] def test_set_active(self, cat: Catalog) -> None: unit = cat.unit("U") toggle = SetActive(cat.units, FakeUnitOfWork(), ServiceUnitNotFound) toggle.execute(unit, False) assert not cat.units.items[unit].is_active toggle.execute(unit, True) assert cat.units.items[unit].is_active with pytest.raises(ServiceUnitNotFound): toggle.execute(42, False)