diff --git a/backend/model/tribe.py b/backend/model/tribe.py index b4c5a28..2783ef1 100644 --- a/backend/model/tribe.py +++ b/backend/model/tribe.py @@ -45,6 +45,9 @@ class Tribe: return self.name == other.name return False + def __hash__(self) -> int: + return hash(self.name) + def to_dict(self) -> dict: return {"name": self.name, "level": self.level} diff --git a/backend/service/services.py b/backend/service/services.py index 1c874b0..39a6b1a 100644 --- a/backend/service/services.py +++ b/backend/service/services.py @@ -7,6 +7,10 @@ class TribeExists(Exception): pass +class TribeDosNotExists(Exception): + pass + + def add_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn): tribe = Tribe(name=name, level=level) try: @@ -15,3 +19,13 @@ def add_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn): raise TribeExists(f"The tribe {tribe.name} already exists") conn.commit() return tribe + + +def update_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn): + tribe = Tribe(name=name, level=level) + try: + tribe_repo.update(name=name, tribe=tribe) + except TribeRepositoryError: + raise TribeDosNotExists(f"The tribe {name} does not exists you can't update it") + conn.commit() + return tribe diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 365379a..0a87ef5 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -6,7 +6,7 @@ from backend.repository.abstract_repository import AbstractRepository from backend.repository.student_sqlite_repository import StudentRepositoryError from backend.repository.tribe_sqlite_repository import TribeRepositoryError from backend.service import services -from backend.service.services import TribeExists +from backend.service.services import TribeDosNotExists, TribeExists from tests.model.fakes import build_tribes @@ -112,3 +112,31 @@ def test_add_tribe_fail_exists(): ) assert conn.committed == False + + +def test_update_tribe(): + tribes = build_tribes(3) + tribe_repo = FakeTribeRepository(tribes) + conn = FakeConn() + + other_level = "iouiouiou" + tribes[0].level = other_level + services.update_tribe( + name=tribes[0].name, level=other_level, tribe_repo=tribe_repo, conn=conn + ) + + assert conn.committed == True + assert set(tribe_repo.list()) == set(tribes) + + +def test_update_tribe_fail_not_exists(): + tribes = build_tribes(3) + tribe_repo = FakeTribeRepository(tribes) + conn = FakeConn() + + with pytest.raises(TribeDosNotExists): + services.update_tribe( + name="azerty", level="jkl", tribe_repo=tribe_repo, conn=conn + ) + + assert conn.committed == False