recopytex/tests/integration/test_repository_student_sqlite.py

123 lines
3.4 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)
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