From b8a769b96dc44ef89247eea8323c320c2219e585 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 30 Dec 2022 07:19:48 +0100 Subject: [PATCH] Feat: add add_tribe in service --- backend/service/services.py | 16 ++++++ tests/model/fakes.py | 32 ----------- tests/unit/test_service.py | 108 ++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 32 deletions(-) create mode 100644 backend/service/services.py create mode 100644 tests/unit/test_service.py diff --git a/backend/service/services.py b/backend/service/services.py new file mode 100644 index 0000000..fdbddd2 --- /dev/null +++ b/backend/service/services.py @@ -0,0 +1,16 @@ +from backend.model.tribe import Tribe +from backend.repository.abstract_repository import AbstractRepository +from backend.repository.tribe_sqlite_repository import TribeRepositoryError + + +class TribeExists(Exception): + pass + + +def add_tribe(tribe: Tribe, tribe_repo: AbstractRepository, conn): + try: + tribe_repo.add(tribe) + except TribeRepositoryError: + raise TribeExists(f"The tribe {tribe.name} already exists") + conn.commit() + return tribe diff --git a/tests/model/fakes.py b/tests/model/fakes.py index 6ffa738..0ce9369 100644 --- a/tests/model/fakes.py +++ b/tests/model/fakes.py @@ -91,35 +91,3 @@ def build_student( Student(name=faker.name(), tribe=tribe) for _ in range(students_per_tribe) ] return students - - -class FakeTribeRepository(AbstractRepository): - def __init__(self, tribes): - self._tribes = {t.name: t for t in tribes} - - def add(self, tribe): - if tribe.name not in self._tribes.keys(): - self._tribes[tribe.name] = tribe - raise KeyError(f"{tribe} already exists") - - def update(self, name, tribe): - try: - self._tribes.pop(name) - self._tribes[tribe.name] = tribe - except KeyError: - raise KeyError(f"The tribe {tribe} does not exists") - - def list(self): - return list(self._tribes.values()) - - def get(self, name): - try: - return self._tribes[name] - except KeyError: - raise KeyError(f"The tribe {tribe} does not exists") - - def delete(self, name): - try: - self._tribes.pop(name) - except KeyError: - raise KeyError(f"The tribe {tribe} does not exists") diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py new file mode 100644 index 0000000..df19ad4 --- /dev/null +++ b/tests/unit/test_service.py @@ -0,0 +1,108 @@ +import pytest + +from backend.model.student import Student +from backend.model.tribe import Tribe +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 tests.model.fakes import build_tribes + + +class FakeTribeRepository(AbstractRepository): + def __init__(self, tribes: list[Tribe] = []) -> None: + self._tribes = {t.name: t for t in tribes} + + def add(self, tribe: Tribe) -> None: + if tribe.name not in self._tribes.keys(): + self._tribes[tribe.name] = tribe + else: + raise TribeRepositoryError(f"{tribe} already exists") + + def update(self, name: str, tribe: Tribe) -> None: + try: + self._tribes.pop(name) + self._tribes[tribe.name] = tribe + except KeyError: + raise TribeRepositoryError(f"The tribe {tribe} does not exists") + + def list(self) -> list[Tribe]: + return list(self._tribes.values()) + + def get(self, name: str) -> Tribe: + try: + return self._tribes[name] + except KeyError: + raise TribeRepositoryError(f"The tribe {tribe} does not exists") + + def delete(self, name: str) -> None: + try: + self._tribes.pop(name) + except KeyError: + raise KeyError(f"The tribe {tribe} does not exists") + + +class FakeStudentRepository(AbstractRepository): + def __init__(self, students: list[Student] = []) -> None: + self._students = {s.id: s for s in students} + + def add(self, student: Student) -> None: + if student.id not in self._students.keys(): + self._students[student.id] = student + raise StudentRepositoryError(f"{student} already exists") + + def update(self, id: str, student: Student) -> None: + try: + self._students.pop(id) + self._students[student.id] = student + except KeyError: + raise StudentRepositoryError(f"The student {student} does not exists") + + def list(self) -> list[Student]: + return list(self._students.values()) + + def get(self, id: str) -> Student: + try: + return self._students[id] + except KeyError: + raise KeyError(f"The student {student} does not exists") + + def delete(self, id: str) -> None: + try: + self._students.pop(id) + except KeyError: + raise StudentRepositoryError(f"The student {student} does not exists") + + +class FakeConn: + committed = False + + def commit(self): + self.committed = True + + def reset_commit(self): + self.committed = False + + +def test_add_tribe(): + tribe_repo = FakeTribeRepository() + tribe = build_tribes(1)[0] + conn = FakeConn() + services.add_tribe(tribe, tribe_repo, conn) + + assert conn.committed == True + assert tribe_repo.list() == [tribe] + + +def test_add_tribe_fail_exists(): + tribe_repo = FakeTribeRepository() + tribe = build_tribes(1)[0] + conn = FakeConn() + services.add_tribe(tribe, tribe_repo, conn) + conn.reset_commit() + + with pytest.raises(TribeExists): + services.add_tribe(tribe, tribe_repo, conn) + + assert conn.committed == False