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: def get(self, id: str, tribes: list[Tribe]) -> Student:
cursor = self.conn.cursor() cursor = self.conn.cursor()
cursor.execute( cursor.execute(
@ -42,12 +46,11 @@ class StudentSQLiteRepository(AbstractRepository):
row = cursor.fetchone() row = cursor.fetchone()
if row: if row:
tribe = next(filter(lambda t: t.name == row[2], tribes)) return self._rebuild_student(row, tribes)
return Student(id=row[0], name=row[1], tribe=tribe)
raise ValueError(f"The student ({id=}) does not exists") 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 = self.conn.cursor()
cursor.execute( cursor.execute(
""" """
@ -56,14 +59,14 @@ class StudentSQLiteRepository(AbstractRepository):
) )
rows = cursor.fetchall() 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: def delete(self, student: Student) -> None:
self.conn.execute( 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]: def populate_students(conn, tribes: list[Tribe]) -> list[Student]:
cursor = conn.cursor() cursor = conn.cursor()
prebuild_students = build_student(tribes, 2) prebuild_students = build_student(tribes, 2)
print(prebuild_students)
cursor.executemany( cursor.executemany(
""" """
INSERT INTO students(id, name, tribe_name) VALUES (:id, :name, :tribe_name) 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): def test_get_student(sqlite_conn):
create_db(sqlite_conn) create_db(sqlite_conn)
prebuilt_tribes = populate_tribes(sqlite_conn) prebuild_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuilt_tribes) prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn) student_repo = StudentSQLiteRepository(sqlite_conn)
student_id = prebuild_students[0].id 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 assert prebuild_students[0] == student
# def test_get_student_not_exists(sqlite_conn): def test_get_student_not_exists(sqlite_conn):
# create_db(sqlite_conn) create_db(sqlite_conn)
# prebuild_students = populate_student(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 = StudentSQLiteRepository(sqlite_conn)
# student_repo.get("student0") with pytest.raises(ValueError):
# student_repo.get("student0", prebuild_tribes)
#
# def test_list_students(sqlite_conn):
# create_db(sqlite_conn) def test_list_students(sqlite_conn):
# prebuild_students = populate_student(sqlite_conn) create_db(sqlite_conn)
# prebuild_tribes = populate_tribes(sqlite_conn)
# student_repo = studentSQLiteRepository(sqlite_conn) prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
# students = student_repo.list()
# student_repo = StudentSQLiteRepository(sqlite_conn)
# assert prebuild_students == students students = student_repo.list(prebuild_tribes)
#
# assert prebuild_students == students
# def test_add_student(sqlite_conn):
# create_db(sqlite_conn)
# def test_add_student(sqlite_conn):
# student_repo = studentSQLiteRepository(sqlite_conn) create_db(sqlite_conn)
# prebuild_tribes = populate_tribes(sqlite_conn)
# student_infos = ("student1", "2nd")
# student = student(*student_infos) student_repo = StudentSQLiteRepository(sqlite_conn)
# student_repo.add(student)
# sqlite_conn.commit() student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
# student = Student(**student_infos)
# cursor = sqlite_conn.cursor() student_repo.add(student)
# cursor.execute( sqlite_conn.commit()
# """
# SELECT * FROM students WHERE name=? cursor = sqlite_conn.cursor()
# """, cursor.execute(
# ("student1",), """
# ) SELECT id, name, tribe_name FROM students WHERE id=?
# """,
# row = cursor.fetchone() (student.id,),
# assert row == student_infos )
#
# row = cursor.fetchone()
# def test_add_student_fail_exists(sqlite_conn): assert row == student.to_tuple()
# create_db(sqlite_conn)
# prebuild_students = populate_student(sqlite_conn)
# def test_add_student_fail_exists(sqlite_conn):
# student_repo = studentSQLiteRepository(sqlite_conn) create_db(sqlite_conn)
# prebuild_tribes = populate_tribes(sqlite_conn)
# student_infos = ("student1", "2nd")
# student = student(*student_infos) student_repo = StudentSQLiteRepository(sqlite_conn)
# with pytest.raises(sqlite3.IntegrityError):
# student_repo.add(student) student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
# student = Student(**student_infos)
# student_repo.add(student)
# def test_update_student(sqlite_conn): sqlite_conn.commit()
# create_db(sqlite_conn)
# prebuild_students = populate_student(sqlite_conn) with pytest.raises(sqlite3.IntegrityError):
# student_repo.add(student)
# student_repo = studentSQLiteRepository(sqlite_conn)
# student_repo.update("student1", student("student0", "Term"))
# sqlite_conn.commit() def test_update_student(sqlite_conn):
# create_db(sqlite_conn)
# expected = [ prebuild_tribes = populate_tribes(sqlite_conn)
# student("student0", "Term"), prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
# student("student2", "2nd"),
# student("student3", "1ST"), student_repo = StudentSQLiteRepository(sqlite_conn)
# ]
# assert student_repo.list() == expected student = prebuild_students[0]
# student.name = "Boby"
# student.tribe = prebuild_tribes[-1]
# def test_delete_student(sqlite_conn):
# create_db(sqlite_conn) student_repo.update(student.id, student)
# prebuild_students = populate_student(sqlite_conn) sqlite_conn.commit()
#
# student_repo = studentSQLiteRepository(sqlite_conn) student_list = student_repo.list(prebuild_tribes)
# student_repo.delete(prebuild_students[0]) assert set(student_list) == set(prebuild_students)
# sqlite_conn.commit()
# moded_student = next(filter(lambda s: s.id == student.id, student_list))
# expected = [ assert moded_student == student
# student("student2", "2nd"),
# student("student3", "1ST"),
# ] def test_delete_student(sqlite_conn):
# assert student_repo.list() == expected 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