recopytex/tests/integration/test_repository_student_sqlite.py

108 lines
3.6 KiB
Python
Raw Normal View History

import sqlite3
import pytest
from backend.model.student import Student
from backend.repository.student_sqlite_repository import (
StudentRepositoryError,
StudentSQLiteRepository,
)
2022-12-31 13:28:46 +00:00
def test_get_student(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, prebuild_students):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
2022-12-31 13:28:46 +00:00
student_id = prebuild_students[0].id
student = student_repo.get(student_id, prebuild_tribes)
2022-12-31 13:28:46 +00:00
assert prebuild_students[0] == student
2022-12-31 13:28:46 +00:00
def test_get_student_not_exists(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
with pytest.raises(ValueError):
student_repo.get("student0", prebuild_tribes)
2022-12-31 13:28:46 +00:00
def test_list_students(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, prebuild_students):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
students = student_repo.list(prebuild_tribes)
2022-12-31 13:28:46 +00:00
assert prebuild_students == students
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
def test_add_student(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
student = Student(**student_infos)
student_repo.add(student)
memory_sqlite_conn.commit()
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
cursor = memory_sqlite_conn.cursor()
cursor.execute(
"""
SELECT id, name, tribe_name FROM students WHERE id=?
""",
(student.id,),
)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
row = cursor.fetchone()
assert row == student.to_tuple()
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
def test_add_student_fail_exists(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
student = Student(**student_infos)
2022-12-27 07:41:59 +00:00
student_repo.add(student)
2022-12-31 13:28:46 +00:00
memory_sqlite_conn.commit()
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
with pytest.raises(sqlite3.IntegrityError):
student_repo.add(student)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
def test_update_student(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, prebuild_students):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
student = prebuild_students[0]
student.name = "Boby"
student.tribe = prebuild_tribes[-1]
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
student_repo.update(student)
memory_sqlite_conn.commit()
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
student_list = student_repo.list(prebuild_tribes)
assert set(student_list) == set(prebuild_students)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
moded_student = next(filter(lambda s: s.id == student.id, student_list))
assert moded_student == student
2022-12-31 13:28:46 +00:00
def test_update_student_does_not_exists(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
2022-12-31 13:28:46 +00:00
student = Student(name="jkl", tribe=prebuild_tribes[0])
2022-12-31 13:28:46 +00:00
with pytest.raises(StudentRepositoryError):
student_repo.update(student)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
def test_delete_student(memory_sqlite_conn, populate_db):
with populate_db(memory_sqlite_conn) as (prebuild_tribes, prebuild_students):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
deleted_student = prebuild_students.pop()
student_repo.delete(deleted_student.id)
memory_sqlite_conn.commit()
2022-12-27 07:41:59 +00:00
2022-12-31 13:28:46 +00:00
assert student_repo.list(prebuild_tribes) == prebuild_students