43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
import os
|
|
import shutil
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from sqlmodel import Session, SQLModel, create_engine, select
|
|
from sqlmodel.pool import StaticPool
|
|
from database.exercices.models import Exercice
|
|
from main import app
|
|
|
|
from database.db import get_session
|
|
|
|
|
|
@pytest.fixture(name="session")
|
|
def session_fixture():
|
|
engine = create_engine(
|
|
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool, echo=False
|
|
)
|
|
SQLModel.metadata.create_all(engine)
|
|
with Session(engine) as session:
|
|
yield session
|
|
#cleanup uploads
|
|
exos = session.exec(select(Exercice)).all()
|
|
for e in exos:
|
|
try:
|
|
shutil.rmtree(os.path.join('uploads', e.id_code))
|
|
except:
|
|
pass
|
|
|
|
@pytest.fixture(name="client")
|
|
def client_fixture(session: Session):
|
|
|
|
def get_session_override():
|
|
return session
|
|
|
|
app.dependency_overrides[get_session] = get_session_override
|
|
|
|
client = TestClient(app)
|
|
|
|
yield client
|
|
|
|
app.dependency_overrides.clear()
|
|
|
|
|