From 356db507eb4a80cdbf5f16a85087ecc93ba0cbb8 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 30 Dec 2022 10:39:51 +0100 Subject: [PATCH] Feat: add delete_student --- backend/service/services.py | 13 +++++++++- tests/unit/test_service.py | 47 ++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/backend/service/services.py b/backend/service/services.py index 16ecf88..89d5eea 100644 --- a/backend/service/services.py +++ b/backend/service/services.py @@ -84,7 +84,6 @@ def update_student( tribe_repo: AbstractRepository, conn, ) -> Student: - try: _tribe = tribe_repo.get(tribe) except TribeRepositoryError: @@ -103,3 +102,15 @@ def update_student( conn.commit() return student + + +def delete_student( + id: str, + student_repo: AbstractRepository, + conn, +) -> Student: + try: + student_repo.delete(id=id) + except StudentRepositoryError: + raise StudentDoesExist("The student with id {id} does not exists") + conn.commit() diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index bed89ca..0f4afe4 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -77,7 +77,7 @@ class FakeStudentRepository(AbstractRepository): try: self._students.pop(id) except KeyError: - raise StudentRepositoryError(f"The student {student} does not exists") + raise StudentRepositoryError(f"The student with id {id} does not exists") class FakeConn: @@ -247,7 +247,7 @@ def test_update_student(): assert len(listed_student) == 2 -def test_update_student_tribe_doesnt_exists(): +def test_update_student_tribe_doesnt_exist(): tribes = build_tribes(2) tribe_repo = FakeTribeRepository(tribes) students = build_student(tribes, 1) @@ -278,7 +278,7 @@ def test_update_student_tribe_doesnt_exists(): assert len(listed_student) == 2 -def test_update_student_doesnt_exists(): +def test_update_student_doesnt_exist(): tribes = build_tribes(2) tribe_repo = FakeTribeRepository(tribes) students = build_student(tribes, 1) @@ -307,3 +307,44 @@ def test_update_student_doesnt_exists(): listed_student = student_repo.list() assert len(listed_student) == 2 + + +def test_delete_student(): + tribes = build_tribes(2) + tribe_repo = FakeTribeRepository(tribes) + students = build_student(tribes, 1) + student_repo = FakeStudentRepository(students) + conn = FakeConn() + + student = students.pop() + + services.delete_student( + id=student.id, + student_repo=student_repo, + conn=conn, + ) + + assert conn.committed is True + + listed_student = student_repo.list() + assert listed_student == students + + +def test_delete_student_doesnt_exist(): + tribes = build_tribes(2) + tribe_repo = FakeTribeRepository(tribes) + students = build_student(tribes, 1) + student_repo = FakeStudentRepository(students) + conn = FakeConn() + + with pytest.raises(StudentDoesExist): + services.delete_student( + id="not existing id", + student_repo=student_repo, + conn=conn, + ) + + assert conn.committed is False + + listed_student = student_repo.list() + assert set(listed_student) == set(students)