52 lines
1.3 KiB
Python
52 lines
1.3 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
|
|
import json
|
|
import pydantic.json
|
|
|
|
|
|
def _custom_json_serializer(*args, **kwargs) -> str:
|
|
"""
|
|
Encodes json in the same way that pydantic does.
|
|
"""
|
|
return json.dumps(*args, default=pydantic.json.pydantic_encoder, **kwargs)
|
|
|
|
|
|
@pytest.fixture(name="session")
|
|
def session_fixture():
|
|
engine = create_engine(
|
|
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool, echo=False, json_serializer=_custom_json_serializer
|
|
)
|
|
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()
|
|
|
|
|