Feat: add get method for student repository

This commit is contained in:
Bertrand Benjamin 2022-12-27 08:13:54 +01:00
parent 4e13d0e32f
commit fd567c292d
3 changed files with 206 additions and 0 deletions

View File

@ -14,5 +14,20 @@ def create_tribe_table(conn) -> None:
conn.commit()
def create_student_table(conn) -> None:
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS students(
id VARCHAR(500) PRIMARY KEY UNIQUE,
name VARCHAR,
tribe_name VARCHAR
)
"""
)
conn.commit()
def create_db(conn) -> None:
create_tribe_table(conn)
create_student_table(conn)

View File

@ -0,0 +1,69 @@
from backend.model.student import Student
from backend.model.tribe import Tribe
from backend.repository.abstract_repository import AbstractRepository
class StudentSQLiteRepository(AbstractRepository):
def __init__(self, conn) -> None:
self.conn = conn
def add(self, student: Student) -> None:
self.conn.execute(
"""
INSERT INTO students(id, name, tribe_name) VALUES (?, ?, ?)
""",
(
student.id,
student.name,
student.tribe.name,
),
)
def update(self, name: str, student: Student) -> None:
self.conn.execute(
"""
UPDATE students SET name=:newname, tribe_name=:newtribe WHERE id=:id
""",
{
"newname": student.name,
"newtribe": student.tribe.name,
"id": student.id,
},
)
def get(self, id: str, tribes: list[Tribe]) -> Student:
cursor = self.conn.cursor()
cursor.execute(
"""
SELECT id, name, tribe_name FROM students WHERE id=?
""",
(id,),
)
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)
raise ValueError(f"The student ({id=}) does not exists")
def list(self) -> list[Student]:
cursor = self.conn.cursor()
cursor.execute(
"""
SELECT * FROM students
"""
)
rows = cursor.fetchall()
return [Student(*r) for r in rows]
def delete(self, student: Student) -> None:
self.conn.execute(
"""
DELETE FROM students WHERE name=:name
""",
{
"name": student.name,
},
)

View File

@ -0,0 +1,122 @@
import sqlite3
import pytest
from backend.adapters.sqlite import create_db
from backend.model.student import Student
from backend.model.tribe import Tribe
from backend.repository.student_sqlite_repository import StudentSQLiteRepository
from tests.integration.test_repository_tribe_sqlite import populate_tribes
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)
""",
[s.to_dict() for s in prebuild_students],
)
conn.commit()
return prebuild_students
def test_get_student(sqlite_conn):
create_db(sqlite_conn)
prebuilt_tribes = populate_tribes(sqlite_conn)
prebuild_students = populate_students(sqlite_conn, prebuilt_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn)
student_id = prebuild_students[0].id
student = student_repo.get(student_id, prebuilt_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