Feat: basic managent for students

This commit is contained in:
Bertrand Benjamin 2022-12-27 08:41:59 +01:00
parent fd567c292d
commit e5a50e0be8
2 changed files with 104 additions and 92 deletions

View File

@ -31,6 +31,10 @@ class StudentSQLiteRepository(AbstractRepository):
},
)
def _rebuild_student(self, row: tuple, tribes: list[Tribe]) -> Student:
tribe = next(filter(lambda t: t.name == row[2], tribes))
return Student(id=row[0], name=row[1], tribe=tribe)
def get(self, id: str, tribes: list[Tribe]) -> Student:
cursor = self.conn.cursor()
cursor.execute(
@ -42,12 +46,11 @@ class StudentSQLiteRepository(AbstractRepository):
row = cursor.fetchone()
if row:
tribe = next(filter(lambda t: t.name == row[2], tribes))
return Student(id=row[0], name=row[1], tribe=tribe)
return self._rebuild_student(row, tribes)
raise ValueError(f"The student ({id=}) does not exists")
def list(self) -> list[Student]:
def list(self, tribes: list[Tribe]) -> list[Student]:
cursor = self.conn.cursor()
cursor.execute(
"""
@ -56,14 +59,14 @@ class StudentSQLiteRepository(AbstractRepository):
)
rows = cursor.fetchall()
return [Student(*r) for r in rows]
return [self._rebuild_student(r, tribes) for r in rows]
def delete(self, student: Student) -> None:
self.conn.execute(
"""
DELETE FROM students WHERE name=:name
DELETE FROM students WHERE id=:id
""",
{
"name": student.name,
"id": student.id,
},
)

View File

@ -13,7 +13,6 @@ from tests.model.fakes import build_student
def populate_students(conn, tribes: list[Tribe]) -> list[Student]:
cursor = conn.cursor()
prebuild_students = build_student(tribes, 2)
print(prebuild_students)
cursor.executemany(
"""
INSERT INTO students(id, name, tribe_name) VALUES (:id, :name, :tribe_name)
@ -27,96 +26,106 @@ def populate_students(conn, tribes: list[Tribe]) -> list[Student]:
def test_get_student(sqlite_conn):
create_db(sqlite_conn)
prebuilt_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuilt_tribes)
prebuild_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn)
student_id = prebuild_students[0].id
student = student_repo.get(student_id, prebuilt_tribes)
student = student_repo.get(student_id, prebuild_tribes)
assert prebuild_students[0] == student
# def test_get_student_not_exists(sqlite_conn):
# create_db(sqlite_conn)
# prebuild_students = populate_student(sqlite_conn)
#
# student_repo = studentSQLiteRepository(sqlite_conn)
# with pytest.raises(ValueError):
# student_repo.get("student0")
#
#
# def test_list_students(sqlite_conn):
# create_db(sqlite_conn)
# prebuild_students = populate_student(sqlite_conn)
#
# student_repo = studentSQLiteRepository(sqlite_conn)
# students = student_repo.list()
#
# assert prebuild_students == students
#
#
# def test_add_student(sqlite_conn):
# create_db(sqlite_conn)
#
# student_repo = studentSQLiteRepository(sqlite_conn)
#
# student_infos = ("student1", "2nd")
# student = student(*student_infos)
# student_repo.add(student)
# sqlite_conn.commit()
#
# cursor = sqlite_conn.cursor()
# cursor.execute(
# """
# SELECT * FROM students WHERE name=?
# """,
# ("student1",),
# )
#
# row = cursor.fetchone()
# assert row == student_infos
#
#
# def test_add_student_fail_exists(sqlite_conn):
# create_db(sqlite_conn)
# prebuild_students = populate_student(sqlite_conn)
#
# student_repo = studentSQLiteRepository(sqlite_conn)
#
# student_infos = ("student1", "2nd")
# student = student(*student_infos)
# with pytest.raises(sqlite3.IntegrityError):
# student_repo.add(student)
#
#
# def test_update_student(sqlite_conn):
# create_db(sqlite_conn)
# prebuild_students = populate_student(sqlite_conn)
#
# student_repo = studentSQLiteRepository(sqlite_conn)
# student_repo.update("student1", student("student0", "Term"))
# sqlite_conn.commit()
#
# expected = [
# student("student0", "Term"),
# student("student2", "2nd"),
# student("student3", "1ST"),
# ]
# assert student_repo.list() == expected
#
#
# def test_delete_student(sqlite_conn):
# create_db(sqlite_conn)
# prebuild_students = populate_student(sqlite_conn)
#
# student_repo = studentSQLiteRepository(sqlite_conn)
# student_repo.delete(prebuild_students[0])
# sqlite_conn.commit()
#
# expected = [
# student("student2", "2nd"),
# student("student3", "1ST"),
# ]
# assert student_repo.list() == expected
def test_get_student_not_exists(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn)
with pytest.raises(ValueError):
student_repo.get("student0", prebuild_tribes)
def test_list_students(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn)
students = student_repo.list(prebuild_tribes)
assert prebuild_students == students
def test_add_student(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribes(sqlite_conn)
student_repo = StudentSQLiteRepository(sqlite_conn)
student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
student = Student(**student_infos)
student_repo.add(student)
sqlite_conn.commit()
cursor = sqlite_conn.cursor()
cursor.execute(
"""
SELECT id, name, tribe_name FROM students WHERE id=?
""",
(student.id,),
)
row = cursor.fetchone()
assert row == student.to_tuple()
def test_add_student_fail_exists(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribes(sqlite_conn)
student_repo = StudentSQLiteRepository(sqlite_conn)
student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
student = Student(**student_infos)
student_repo.add(student)
sqlite_conn.commit()
with pytest.raises(sqlite3.IntegrityError):
student_repo.add(student)
def test_update_student(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn)
student = prebuild_students[0]
student.name = "Boby"
student.tribe = prebuild_tribes[-1]
student_repo.update(student.id, student)
sqlite_conn.commit()
student_list = student_repo.list(prebuild_tribes)
assert set(student_list) == set(prebuild_students)
moded_student = next(filter(lambda s: s.id == student.id, student_list))
assert moded_student == student
def test_delete_student(sqlite_conn):
create_db(sqlite_conn)
prebuild_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn)
deleted_student = prebuild_students.pop()
student_repo.delete(deleted_student)
sqlite_conn.commit()
assert student_repo.list(prebuild_tribes) == prebuild_students