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
|
|
|
|
from tests.integration.test_repository_student_sqlite import populate_students
|
|
|
|
from tests.integration.test_repository_tribe_sqlite import populate_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
|
|
|
|
def sqlite_conn():
|
2022-12-28 06:47:35 +00:00
|
|
|
conn = sqlite3.connect("sqlite.db")
|
|
|
|
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
|
|
|
|
def clean_db(sqlite_conn):
|
|
|
|
sqlite_conn.execute("""DROP TABLE tribes""")
|
|
|
|
sqlite_conn.execute("""DROP TABLE students""")
|
|
|
|
sqlite_conn.commit()
|
|
|
|
create_db(sqlite_conn)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def populate_db(sqlite_conn):
|
|
|
|
_tribes = []
|
|
|
|
_students = []
|
|
|
|
|
|
|
|
def _populate_db():
|
|
|
|
tribes = populate_tribes(sqlite_conn)
|
|
|
|
_tribes += tribes
|
|
|
|
students = populate_students(sqlite_conn, tribes)
|
|
|
|
_students += students
|
|
|
|
return tribes, students
|
|
|
|
|
|
|
|
yield _populate_db
|
|
|
|
|
|
|
|
for student in _students:
|
|
|
|
sqlite_conn.execute(
|
|
|
|
"""
|
|
|
|
DELETE FROM students WHERE id=:id
|
|
|
|
""",
|
|
|
|
{"id": student.id},
|
|
|
|
)
|
|
|
|
for tribe in _tribes:
|
|
|
|
sqlite_conn.execute(
|
|
|
|
"""
|
|
|
|
DELETE FROM tribes WHERE name=:name
|
|
|
|
""",
|
|
|
|
{"name": tribe.name},
|
|
|
|
)
|
|
|
|
sqlite_conn.commit()
|
|
|
|
|
|
|
|
|
|
|
|
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()
|