Feat: add update_student to service

This commit is contained in:
Bertrand Benjamin 2022-12-30 10:34:20 +01:00
parent 94c942d055
commit 066990d109
3 changed files with 135 additions and 9 deletions

View File

@ -26,10 +26,11 @@ class StudentSQLiteRepository(AbstractRepository):
def update(self, student: Student) -> None:
search_student = self.conn.execute(
"""
SELECT id FROM students WHERE id=:id
""",
SELECT id FROM students WHERE id=:id
""",
{"id": student.id},
).fetchone()
if search_student is None:
raise StudentRepositoryError(f"The student ({student.id=}) does not exists")

View File

@ -74,3 +74,32 @@ def add_student(
conn.commit()
return student
def update_student(
id: str,
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 update a student with it"
)
student = Student(id=id, name=name, tribe=_tribe)
try:
student_repo.update(student)
except StudentRepositoryError:
raise StudentDoesExist(
f"The student {student.name} ({student.id=}) does not exists. Can't update it."
)
conn.commit()
return student

View File

@ -6,7 +6,12 @@ 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 StudentExists, TribeDoesNotExist, TribeExists
from backend.service.services import (
StudentDoesExist,
StudentExists,
TribeDoesNotExist,
TribeExists,
)
from tests.model.fakes import build_student, build_tribes
@ -53,13 +58,12 @@ class FakeStudentRepository(AbstractRepository):
else:
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:
def update(self, student: Student) -> None:
if student.id not in self._students.keys():
raise StudentRepositoryError(f"The student {student} does not exists")
self._students[student.id] = student
def list(self) -> list[Student]:
return list(self._students.values())
@ -67,7 +71,7 @@ class FakeStudentRepository(AbstractRepository):
try:
return self._students[id]
except KeyError:
raise KeyError(f"The student {student} does not exists")
raise KeyError(f"The student ({id=}) does not exists")
def delete(self, id: str) -> None:
try:
@ -211,3 +215,95 @@ def test_add_student_tribe_doesnt_exist():
)
assert conn.committed is False
def test_update_student():
tribes = build_tribes(2)
tribe_repo = FakeTribeRepository(tribes)
students = build_student(tribes, 1)
student_repo = FakeStudentRepository(students)
conn = FakeConn()
id = students[0].id
new_name = "new name"
new_tribe_name = tribes[1].name
saved_student = services.update_student(
id=id,
name=new_name,
tribe=new_tribe_name,
student_repo=student_repo,
tribe_repo=tribe_repo,
conn=conn,
)
assert conn.committed is True
mod_student = student_repo.get(id)
assert mod_student.name == new_name
assert mod_student.tribe.name == new_tribe_name
listed_student = student_repo.list()
assert len(listed_student) == 2
def test_update_student_tribe_doesnt_exists():
tribes = build_tribes(2)
tribe_repo = FakeTribeRepository(tribes)
students = build_student(tribes, 1)
student_repo = FakeStudentRepository(students)
conn = FakeConn()
id = students[0].id
new_name = "new name"
new_tribe_name = "not existing tribe"
with pytest.raises(TribeDoesNotExist):
services.update_student(
id=id,
name=new_name,
tribe=new_tribe_name,
student_repo=student_repo,
tribe_repo=tribe_repo,
conn=conn,
)
assert conn.committed is False
mod_student = student_repo.get(id)
assert mod_student.name == students[0].name
assert mod_student.tribe.name == students[0].tribe.name
listed_student = student_repo.list()
assert len(listed_student) == 2
def test_update_student_doesnt_exists():
tribes = build_tribes(2)
tribe_repo = FakeTribeRepository(tribes)
students = build_student(tribes, 1)
student_repo = FakeStudentRepository(students)
conn = FakeConn()
id = "not existing id"
new_name = students[0].name
new_tribe_name = students[0].tribe.name
with pytest.raises(StudentDoesExist):
services.update_student(
id=id,
name=new_name,
tribe=new_tribe_name,
student_repo=student_repo,
tribe_repo=tribe_repo,
conn=conn,
)
assert conn.committed is False
original_student = student_repo.get(students[0].id)
assert original_student.name == students[0].name
assert original_student.tribe.name == students[0].tribe.name
listed_student = student_repo.list()
assert len(listed_student) == 2