Feat: add add_student to service

This commit is contained in:
Bertrand Benjamin 2022-12-30 08:15:37 +01:00
parent 12b3220170
commit 94c942d055
2 changed files with 96 additions and 12 deletions

View File

@ -1,5 +1,7 @@
from backend.model.student import Student
from backend.model.tribe import Tribe from backend.model.tribe import Tribe
from backend.repository.abstract_repository import AbstractRepository 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.repository.tribe_sqlite_repository import TribeRepositoryError
@ -7,7 +9,15 @@ class TribeExists(Exception):
pass pass
class TribeDosNotExists(Exception): class TribeDoesNotExist(Exception):
pass
class StudentExists(Exception):
pass
class StudentDoesExist(Exception):
pass pass
@ -26,7 +36,7 @@ def update_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn) ->
try: try:
tribe_repo.update(name=name, tribe=tribe) tribe_repo.update(name=name, tribe=tribe)
except TribeRepositoryError: 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() conn.commit()
return tribe return tribe
@ -35,6 +45,32 @@ def delete_tribe(name: str, tribe_repo: AbstractRepository, conn) -> None:
try: try:
tribe_repo.delete(name=name) tribe_repo.delete(name=name)
except TribeRepositoryError: 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() 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

View File

@ -6,8 +6,8 @@ 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 TribeDosNotExists, TribeExists from backend.service.services import StudentExists, TribeDoesNotExist, TribeExists
from tests.model.fakes import build_tribes from tests.model.fakes import build_student, build_tribes
class FakeTribeRepository(AbstractRepository): class FakeTribeRepository(AbstractRepository):
@ -34,7 +34,7 @@ class FakeTribeRepository(AbstractRepository):
try: try:
return self._tribes[name] return self._tribes[name]
except KeyError: 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: def delete(self, name: str) -> None:
try: try:
@ -50,6 +50,7 @@ class FakeStudentRepository(AbstractRepository):
def add(self, student: Student) -> None: def add(self, student: Student) -> None:
if student.id not in self._students.keys(): if student.id not in self._students.keys():
self._students[student.id] = student self._students[student.id] = student
else:
raise StudentRepositoryError(f"{student} already exists") raise StudentRepositoryError(f"{student} already exists")
def update(self, id: str, student: Student) -> None: def update(self, id: str, student: Student) -> None:
@ -93,7 +94,7 @@ def test_add_tribe():
name=tribe.name, level=tribe.level, tribe_repo=tribe_repo, conn=conn 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] 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 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) assert set(tribe_repo.list()) == set(tribes)
@ -134,7 +135,7 @@ def test_update_tribe_fail_not_exists():
tribe_repo = FakeTribeRepository(tribes) tribe_repo = FakeTribeRepository(tribes)
conn = FakeConn() conn = FakeConn()
with pytest.raises(TribeDosNotExists): with pytest.raises(TribeDoesNotExist):
services.update_tribe( services.update_tribe(
name="azerty", level="jkl", tribe_repo=tribe_repo, conn=conn name="azerty", level="jkl", tribe_repo=tribe_repo, conn=conn
) )
@ -150,7 +151,7 @@ def test_delete_tribe():
tribe = tribes.pop() tribe = tribes.pop()
services.delete_tribe(name=tribe.name, tribe_repo=tribe_repo, conn=conn) 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) assert set(tribe_repo.list()) == set(tribes)
@ -159,7 +160,54 @@ def test_delete_tribe_fail_not_exists():
tribe_repo = FakeTribeRepository(tribes) tribe_repo = FakeTribeRepository(tribes)
conn = FakeConn() conn = FakeConn()
with pytest.raises(TribeDosNotExists): with pytest.raises(TribeDoesNotExist):
services.delete_tribe(name="azerty", tribe_repo=tribe_repo, conn=conn) services.delete_tribe(name="azerty", tribe_repo=tribe_repo, conn=conn)
assert conn.committed == False 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