recopytex/tests/integration/test_repository_student_sqlite.py

132 lines
3.8 KiB
Python

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)
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)
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, prebuild_tribes)
assert prebuild_students[0] == student
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