Refact: rewrite populate db

This commit is contained in:
Bertrand Benjamin 2022-12-31 14:28:46 +01:00
parent 0ebc24ff29
commit ccb59975f7
3 changed files with 183 additions and 201 deletions

View File

@ -10,8 +10,9 @@ from sqlalchemy.orm import clear_mappers, sessionmaker
from backend import config from backend import config
from backend.adapters.orm import metadata, start_mappers from backend.adapters.orm import metadata, start_mappers
from backend.adapters.sqlite import create_db from backend.adapters.sqlite import create_db
from tests.integration.test_repository_student_sqlite import populate_students from backend.model.student import Student
from tests.integration.test_repository_tribe_sqlite import populate_tribes from backend.model.tribe import Tribe
from tests.model.fakes import build_student, build_tribes
@pytest.fixture @pytest.fixture
@ -29,7 +30,7 @@ def session(in_memory_db):
@pytest.fixture @pytest.fixture
def sqlite_conn(): def memory_sqlite_conn():
sqlite_db = ":memory:" sqlite_db = ":memory:"
conn = sqlite3.connect(sqlite_db) conn = sqlite3.connect(sqlite_db)
create_db(conn) create_db(conn)
@ -49,35 +50,70 @@ def clean_db():
conn.commit() conn.commit()
def populate_tribes(conn) -> list[Tribe]:
cursor = conn.cursor()
tribes = build_tribes(3)
cursor.executemany(
"""
INSERT INTO tribes(name, level) VALUES (?, ?)
""",
[t.to_tuple() for t in tribes],
)
conn.commit()
return tribes
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
@pytest.fixture @pytest.fixture
def populate_db(sqlite_conn): def populate_db():
_tribes = [] class Student_tribe_context:
_students = [] _tribes = []
_students = []
def _populate_db(): def __init__(self, conn):
tribes = populate_tribes(sqlite_conn) self.conn = conn
_tribes += tribes
students = populate_students(sqlite_conn, tribes)
_students += students
return tribes, students
yield _populate_db def __enter__(self):
self._tribes += populate_tribes(self.conn)
self._students += populate_students(self.conn, self._tribes)
return self._tribes, self._students
for student in _students: def __exit__(self, *args):
sqlite_conn.execute(
""" for student in self._students:
DELETE FROM students WHERE id=:id self.conn.execute(
""", """
{"id": student.id}, DELETE FROM students WHERE id=:id
) """,
for tribe in _tribes: {"id": student.id},
sqlite_conn.execute( )
""" for tribe in self._tribes:
DELETE FROM tribes WHERE name=:name self.conn.execute(
""", """
{"name": tribe.name}, DELETE FROM tribes WHERE name=:name
) """,
sqlite_conn.commit() {"name": tribe.name},
)
self.conn.commit()
def fixture(conn):
return Student_tribe_context(conn)
yield fixture
def wait_for_webapp_to_come_up(): def wait_for_webapp_to_come_up():

View File

@ -3,135 +3,105 @@ import sqlite3
import pytest import pytest
from backend.model.student import Student from backend.model.student import Student
from backend.model.tribe import Tribe
from backend.repository.student_sqlite_repository import ( from backend.repository.student_sqlite_repository import (
StudentRepositoryError, StudentRepositoryError,
StudentSQLiteRepository, 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]: def test_get_student(memory_sqlite_conn, populate_db):
cursor = conn.cursor() with populate_db(memory_sqlite_conn) as (prebuild_tribes, prebuild_students):
prebuild_students = build_student(tribes, 2) student_repo = StudentSQLiteRepository(memory_sqlite_conn)
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 student_id = prebuild_students[0].id
student = student_repo.get(student_id, prebuild_tribes)
assert prebuild_students[0] == student
def test_get_student(sqlite_conn): def test_get_student_not_exists(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
prebuild_students = populate_students(sqlite_conn, prebuild_tribes) student_repo = StudentSQLiteRepository(memory_sqlite_conn)
with pytest.raises(ValueError):
student_repo = StudentSQLiteRepository(sqlite_conn) student_repo.get("student0", prebuild_tribes)
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): def test_list_students(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, prebuild_students):
prebuild_students = populate_students(sqlite_conn, prebuild_tribes) student_repo = StudentSQLiteRepository(memory_sqlite_conn)
students = student_repo.list(prebuild_tribes)
student_repo = StudentSQLiteRepository(sqlite_conn) assert prebuild_students == students
with pytest.raises(ValueError):
student_repo.get("student0", prebuild_tribes)
def test_list_students(sqlite_conn): def test_add_student(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
prebuild_students = populate_students(sqlite_conn, prebuild_tribes) student_repo = StudentSQLiteRepository(memory_sqlite_conn)
student_repo = StudentSQLiteRepository(sqlite_conn) student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
students = student_repo.list(prebuild_tribes) student = Student(**student_infos)
assert prebuild_students == students
def test_add_student(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):
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) student_repo.add(student)
memory_sqlite_conn.commit()
cursor = memory_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_update_student(sqlite_conn): def test_add_student_fail_exists(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
prebuild_students = populate_students(sqlite_conn, prebuild_tribes) student_repo = StudentSQLiteRepository(memory_sqlite_conn)
student_repo = StudentSQLiteRepository(sqlite_conn) student_infos = {"name": "student1", "tribe": prebuild_tribes[0]}
student = Student(**student_infos)
student_repo.add(student)
memory_sqlite_conn.commit()
student = prebuild_students[0] with pytest.raises(sqlite3.IntegrityError):
student.name = "Boby" student_repo.add(student)
student.tribe = prebuild_tribes[-1]
student_repo.update(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_update_student_does_not_exists(sqlite_conn): def test_update_student(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, prebuild_students):
student_repo = StudentSQLiteRepository(memory_sqlite_conn)
student_repo = StudentSQLiteRepository(sqlite_conn) student = prebuild_students[0]
student.name = "Boby"
student.tribe = prebuild_tribes[-1]
student = Student(name="jkl", tribe=prebuild_tribes[0])
with pytest.raises(StudentRepositoryError):
student_repo.update(student) student_repo.update(student)
memory_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): def test_update_student_does_not_exists(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
prebuild_students = populate_students(sqlite_conn, prebuild_tribes) student_repo = StudentSQLiteRepository(memory_sqlite_conn)
student_repo = StudentSQLiteRepository(sqlite_conn) student = Student(name="jkl", tribe=prebuild_tribes[0])
deleted_student = prebuild_students.pop() with pytest.raises(StudentRepositoryError):
student_repo.delete(deleted_student) student_repo.update(student)
sqlite_conn.commit()
assert student_repo.list(prebuild_tribes) == prebuild_students
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)
deleted_student = prebuild_students.pop()
student_repo.delete(deleted_student.id)
memory_sqlite_conn.commit()
assert student_repo.list(prebuild_tribes) == prebuild_students

View File

@ -1,5 +1,3 @@
import sqlite3
import pytest import pytest
from backend.model.tribe import Tribe from backend.model.tribe import Tribe
@ -7,60 +5,41 @@ from backend.repository.tribe_sqlite_repository import (
TribeRepositoryError, TribeRepositoryError,
TribeSQLiteRepository, TribeSQLiteRepository,
) )
from tests.model.fakes import build_tribes
def populate_tribes(conn) -> list[Tribe]: def test_get_tribe(memory_sqlite_conn, populate_db):
cursor = conn.cursor() with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
tribes = build_tribes(3) name = prebuild_tribes[0].name
cursor.executemany(
"""
INSERT INTO tribes(name, level) VALUES (?, ?)
""",
[t.to_tuple() for t in tribes],
)
conn.commit()
return tribes tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
tribes = tribe_repo.get(name)
assert prebuild_tribes[0] == tribes
def test_get_tribe(sqlite_conn): def test_get_tribe_not_exists(memory_sqlite_conn):
prebuild_tribes = populate_tribes(sqlite_conn) tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
name = prebuild_tribes[0].name
tribe_repo = TribeSQLiteRepository(sqlite_conn)
tribes = tribe_repo.get(name)
assert prebuild_tribes[0] == tribes
def test_get_tribe_not_exists(sqlite_conn):
prebuild_tribes = populate_tribes(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn)
with pytest.raises(TribeRepositoryError): with pytest.raises(TribeRepositoryError):
tribe_repo.get("Tribe0") tribe_repo.get("Tribe0")
def test_list_tribes(sqlite_conn): def test_list_tribes(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
listed_tribes = tribe_repo.list()
tribe_repo = TribeSQLiteRepository(sqlite_conn) assert prebuild_tribes == listed_tribes
tribes = tribe_repo.list()
assert prebuild_tribes == tribes
def test_add_tribe(sqlite_conn): def test_add_tribe(memory_sqlite_conn):
tribe_repo = TribeSQLiteRepository(sqlite_conn) tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
tribe_infos = ("tribe1", "2nd") tribe_infos = ("tribe1", "2nd")
tribe = Tribe(*tribe_infos) tribe = Tribe(*tribe_infos)
tribe_repo.add(tribe) tribe_repo.add(tribe)
sqlite_conn.commit() memory_sqlite_conn.commit()
cursor = sqlite_conn.cursor() cursor = memory_sqlite_conn.cursor()
cursor.execute( cursor.execute(
""" """
SELECT * FROM tribes WHERE name=? SELECT * FROM tribes WHERE name=?
@ -72,46 +51,43 @@ def test_add_tribe(sqlite_conn):
assert row == tribe_infos assert row == tribe_infos
def test_add_tribe_fail_exists(sqlite_conn): def test_add_tribe_fail_exists(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn) existing_tribe = prebuild_tribes[0]
with pytest.raises(TribeRepositoryError):
existing_tribe = prebuild_tribes[0] tribe_repo.add(existing_tribe)
with pytest.raises(TribeRepositoryError):
tribe_repo.add(existing_tribe)
def test_update_tribe(sqlite_conn): def test_update_tribe(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
name = prebuild_tribes[0].name name = prebuild_tribes[0].name
new_tribe = Tribe("Tribe0", "Term") new_tribe = Tribe("Tribe0", "Term")
tribe_repo.update(name, new_tribe) tribe_repo.update(name, new_tribe)
sqlite_conn.commit() memory_sqlite_conn.commit()
prebuild_tribes[0] = new_tribe prebuild_tribes[0] = new_tribe
assert tribe_repo.list() == prebuild_tribes assert tribe_repo.list() == prebuild_tribes
def test_update_tribe_not_exists(sqlite_conn): def test_update_tribe_not_exists(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn) name = prebuild_tribes[0].name
new_tribe = Tribe("Tribe0", "Term")
name = prebuild_tribes[0].name with pytest.raises(TribeRepositoryError):
new_tribe = Tribe("Tribe0", "Term") tribe_repo.update("iouiou", new_tribe)
with pytest.raises(TribeRepositoryError):
tribe_repo.update("iouiou", new_tribe)
def test_delete_tribe(sqlite_conn): def test_delete_tribe(memory_sqlite_conn, populate_db):
prebuild_tribes = populate_tribes(sqlite_conn) with populate_db(memory_sqlite_conn) as (prebuild_tribes, _):
tribe_repo = TribeSQLiteRepository(memory_sqlite_conn)
tribe_repo = TribeSQLiteRepository(sqlite_conn) deleted_tribe = prebuild_tribes.pop()
deleted_tribe = prebuild_tribes.pop() deleted_tribe.name = "iouiou"
deleted_tribe.name = "iouiou" with pytest.raises(TribeRepositoryError):
with pytest.raises(TribeRepositoryError): tribe_repo.delete(deleted_tribe)
tribe_repo.delete(deleted_tribe)