Feat: add update_student to service

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

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