2022-12-26 17:07:23 +00:00
|
|
|
import sqlite3
|
2022-12-28 06:47:35 +00:00
|
|
|
import time
|
|
|
|
from pathlib import Path
|
2022-12-26 17:07:23 +00:00
|
|
|
|
2022-12-20 09:25:12 +00:00
|
|
|
import pytest
|
2022-12-28 06:47:35 +00:00
|
|
|
import requests
|
2022-12-20 09:25:12 +00:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.orm import clear_mappers, sessionmaker
|
|
|
|
|
2022-12-28 06:47:35 +00:00
|
|
|
from backend import config
|
2022-12-20 09:25:12 +00:00
|
|
|
from backend.adapters.orm import metadata, start_mappers
|
2022-12-28 06:47:35 +00:00
|
|
|
from backend.adapters.sqlite import create_db
|
2022-12-31 13:28:46 +00:00
|
|
|
from backend.model.student import Student
|
|
|
|
from backend.model.tribe import Tribe
|
|
|
|
from tests.model.fakes import build_student, build_tribes
|
2022-12-20 09:25:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def in_memory_db():
|
|
|
|
engine = create_engine("sqlite:///:memory:")
|
|
|
|
metadata.create_all(engine)
|
|
|
|
return engine
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def session(in_memory_db):
|
|
|
|
start_mappers()
|
|
|
|
yield sessionmaker(bind=in_memory_db)()
|
|
|
|
clear_mappers()
|
2022-12-26 17:07:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-12-31 13:28:46 +00:00
|
|
|
def memory_sqlite_conn():
|
2022-12-29 16:47:40 +00:00
|
|
|
sqlite_db = ":memory:"
|
|
|
|
conn = sqlite3.connect(sqlite_db)
|
2022-12-28 06:47:35 +00:00
|
|
|
create_db(conn)
|
2022-12-26 17:07:23 +00:00
|
|
|
yield conn
|
|
|
|
conn.close()
|
2022-12-28 06:47:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-12-29 16:47:40 +00:00
|
|
|
def clean_db():
|
|
|
|
sqlite_db = "sqlite.db"
|
|
|
|
conn = sqlite3.connect(sqlite_db)
|
|
|
|
create_db(conn)
|
|
|
|
yield
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("""DROP TABLE tribes""")
|
|
|
|
cursor.execute("""DROP TABLE students""")
|
|
|
|
conn.commit()
|
2022-12-28 06:47:35 +00:00
|
|
|
|
|
|
|
|
2022-12-31 13:28:46 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-12-28 06:47:35 +00:00
|
|
|
@pytest.fixture
|
2022-12-31 13:28:46 +00:00
|
|
|
def populate_db():
|
|
|
|
class Student_tribe_context:
|
|
|
|
_tribes = []
|
|
|
|
_students = []
|
|
|
|
|
|
|
|
def __init__(self, conn):
|
|
|
|
self.conn = conn
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self._tribes += populate_tribes(self.conn)
|
|
|
|
self._students += populate_students(self.conn, self._tribes)
|
|
|
|
return self._tribes, self._students
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
|
|
|
|
for student in self._students:
|
|
|
|
self.conn.execute(
|
|
|
|
"""
|
|
|
|
DELETE FROM students WHERE id=:id
|
|
|
|
""",
|
|
|
|
{"id": student.id},
|
|
|
|
)
|
|
|
|
for tribe in self._tribes:
|
|
|
|
self.conn.execute(
|
|
|
|
"""
|
|
|
|
DELETE FROM tribes WHERE name=:name
|
|
|
|
""",
|
|
|
|
{"name": tribe.name},
|
|
|
|
)
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
def fixture(conn):
|
|
|
|
return Student_tribe_context(conn)
|
|
|
|
|
|
|
|
yield fixture
|
2022-12-28 06:47:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def wait_for_webapp_to_come_up():
|
|
|
|
deadline = time.time() + 10
|
|
|
|
url = config.get_api_url()
|
|
|
|
while time.time() < deadline:
|
|
|
|
try:
|
|
|
|
return requests.get(url)
|
|
|
|
except ConnectionError:
|
|
|
|
time.sleep(0.5)
|
|
|
|
pytest.fail("API never came up")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def restart_api():
|
|
|
|
(Path(__file__).parent.parent / "backend" / "api" / "main.py").touch()
|
|
|
|
time.sleep(0.5)
|
|
|
|
wait_for_webapp_to_come_up()
|