Feat: add update_tribe in service

This commit is contained in:
Bertrand Benjamin 2022-12-30 07:35:05 +01:00
parent a95ce91b29
commit 36e90a004e
3 changed files with 46 additions and 1 deletions

View File

@ -45,6 +45,9 @@ class Tribe:
return self.name == other.name return self.name == other.name
return False return False
def __hash__(self) -> int:
return hash(self.name)
def to_dict(self) -> dict: def to_dict(self) -> dict:
return {"name": self.name, "level": self.level} return {"name": self.name, "level": self.level}

View File

@ -7,6 +7,10 @@ class TribeExists(Exception):
pass pass
class TribeDosNotExists(Exception):
pass
def add_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn): def add_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn):
tribe = Tribe(name=name, level=level) tribe = Tribe(name=name, level=level)
try: 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") raise TribeExists(f"The tribe {tribe.name} already exists")
conn.commit() conn.commit()
return tribe 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

View File

@ -6,7 +6,7 @@ from backend.repository.abstract_repository import AbstractRepository
from backend.repository.student_sqlite_repository import StudentRepositoryError from backend.repository.student_sqlite_repository import StudentRepositoryError
from backend.repository.tribe_sqlite_repository import TribeRepositoryError from backend.repository.tribe_sqlite_repository import TribeRepositoryError
from backend.service import services 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 from tests.model.fakes import build_tribes
@ -112,3 +112,31 @@ def test_add_tribe_fail_exists():
) )
assert conn.committed == False 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