diff --git a/backend/service/services.py b/backend/service/services.py index dbeb840..ab7b884 100644 --- a/backend/service/services.py +++ b/backend/service/services.py @@ -1,5 +1,7 @@ +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 @@ -7,7 +9,15 @@ class TribeExists(Exception): pass -class TribeDosNotExists(Exception): +class TribeDoesNotExist(Exception): + pass + + +class StudentExists(Exception): + pass + + +class StudentDoesExist(Exception): pass @@ -26,7 +36,7 @@ def update_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn) -> try: tribe_repo.update(name=name, tribe=tribe) except TribeRepositoryError: - raise TribeDosNotExists(f"The tribe {name} does not exists you can't update it") + raise TribeDoesNotExist(f"The tribe {name} does not exists you can't update it") conn.commit() return tribe @@ -35,6 +45,32 @@ def delete_tribe(name: str, tribe_repo: AbstractRepository, conn) -> None: try: tribe_repo.delete(name=name) except TribeRepositoryError: - raise TribeDosNotExists(f"The tribe {name} does not exists you can't delete it") + raise TribeDoesNotExist(f"The tribe {name} does not exists you can't delete it") conn.commit() + + +def add_student( + name: str, + tribe: str, + student_repo: AbstractRepository, + tribe_repo: AbstractRepository, + conn, +) -> Student: + + try: + _tribe = tribe_repo.get(tribe) + except TribeRepositoryError: + raise TribeDoesNotExist( + f"The tribe {tribe} does not exists. Can't add a student in it" + ) + + student = Student(name=name, tribe=_tribe) + + try: + student_repo.add(student) + except StudentRepositoryError: + raise StudentExists(f"The student {student.name} already exists. Can't add it.") + + conn.commit() + return student diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 523e856..ec91c30 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -6,8 +6,8 @@ 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 TribeDosNotExists, TribeExists -from tests.model.fakes import build_tribes +from backend.service.services import StudentExists, TribeDoesNotExist, TribeExists +from tests.model.fakes import build_student, build_tribes class FakeTribeRepository(AbstractRepository): @@ -34,7 +34,7 @@ class FakeTribeRepository(AbstractRepository): try: return self._tribes[name] except KeyError: - raise TribeRepositoryError(f"The tribe {tribe} does not exists") + raise TribeRepositoryError(f"The tribe {name} does not exists") def delete(self, name: str) -> None: try: @@ -50,7 +50,8 @@ class FakeStudentRepository(AbstractRepository): 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") + else: + raise StudentRepositoryError(f"{student} already exists") def update(self, id: str, student: Student) -> None: try: @@ -93,7 +94,7 @@ def test_add_tribe(): name=tribe.name, level=tribe.level, tribe_repo=tribe_repo, conn=conn ) - assert conn.committed == True + assert conn.committed is True assert tribe_repo.list() == [tribe] @@ -125,7 +126,7 @@ def test_update_tribe(): name=tribes[0].name, level=other_level, tribe_repo=tribe_repo, conn=conn ) - assert conn.committed == True + assert conn.committed is True assert set(tribe_repo.list()) == set(tribes) @@ -134,7 +135,7 @@ def test_update_tribe_fail_not_exists(): tribe_repo = FakeTribeRepository(tribes) conn = FakeConn() - with pytest.raises(TribeDosNotExists): + with pytest.raises(TribeDoesNotExist): services.update_tribe( name="azerty", level="jkl", tribe_repo=tribe_repo, conn=conn ) @@ -150,7 +151,7 @@ def test_delete_tribe(): tribe = tribes.pop() services.delete_tribe(name=tribe.name, tribe_repo=tribe_repo, conn=conn) - assert conn.committed == True + assert conn.committed is True assert set(tribe_repo.list()) == set(tribes) @@ -159,7 +160,54 @@ def test_delete_tribe_fail_not_exists(): tribe_repo = FakeTribeRepository(tribes) conn = FakeConn() - with pytest.raises(TribeDosNotExists): + with pytest.raises(TribeDoesNotExist): services.delete_tribe(name="azerty", tribe_repo=tribe_repo, conn=conn) assert conn.committed == False + + +def test_add_student(): + tribes = build_tribes(1) + tribe_repo = FakeTribeRepository(tribes) + student = build_student(tribes, 1)[0] + student_repo = FakeStudentRepository() + conn = FakeConn() + + saved_student = services.add_student( + name=student.name, + tribe=student.tribe.name, + student_repo=student_repo, + tribe_repo=tribe_repo, + conn=conn, + ) + + assert conn.committed is True + + listed_student = student_repo.list()[0] + + assert student.name == listed_student.name + assert student.tribe.name == listed_student.tribe.name + # The id is not passed to the service, they can't have the same. + assert student.id != listed_student.id + + assert saved_student == listed_student + + +def test_add_student_tribe_doesnt_exist(): + tribes = build_tribes(1) + tribe_repo = FakeTribeRepository(tribes) + students = build_student(tribes, 1) + student_repo = FakeStudentRepository() + conn = FakeConn() + + student = students[0] + with pytest.raises(TribeDoesNotExist): + services.add_student( + name=student.name, + tribe="iuouiouiouio", + student_repo=student_repo, + tribe_repo=tribe_repo, + conn=conn, + ) + + assert conn.committed is False