Files
brackets/backend/tests/conftest.py
T
WilliamSoderberg Verified b7da527e95
Test and Release / test-backend (push) Successful in 6s
Test and Release / test-frontend (push) Successful in 11s
Test and Release / build-docker (backend) (push) Skipped
Test and Release / build-docker (frontend) (push) Skipped
Test and Release / test-backend (release) Successful in 6s
Test and Release / test-frontend (release) Successful in 11s
Test and Release / build-docker (backend) (release) Successful in 27s
Test and Release / build-docker (frontend) (release) Successful in 26s
Updated PyTests
2026-08-29 21:05:58 +02:00

110 lines
3.1 KiB
Python

# backend/tests/conftest.py
from typing import AsyncGenerator, Generator
import pytest
from app.core.auth import get_admin_user, get_optional_user
from app.database import Base, get_db
from app.main import app
from app import models
from fastapi import Request
from httpx import ASGITransport, AsyncClient
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.pool import StaticPool
# --- DATABASE SETUP ---
SQLALCHEMY_DATABASE_URL = "sqlite:///"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
@pytest.fixture(scope="session")
def prepare_db():
Base.metadata.create_all(bind=engine)
yield
Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="function")
def db(prepare_db) -> Generator[Session, None, None]:
connection = engine.connect()
transaction = connection.begin()
session = TestingSessionLocal(bind=connection)
yield session
session.close()
transaction.rollback()
connection.close()
@pytest.fixture(autouse=True)
def clean_db(db):
db.query(models.Match).delete()
db.query(models.Team).delete()
db.query(models.Court).delete()
db.commit()
yield
@pytest.fixture(scope="function")
async def client(db: Session) -> AsyncGenerator[AsyncClient, None]:
def override_get_db():
try:
yield db
finally:
pass
def override_get_admin_user(request: Request):
if "Authorization" not in request.headers:
raise pytest.skip("Auth header missing in strict auth test")
return "test_admin"
def override_get_optional_user(request: Request):
if "Authorization" in request.headers:
return "test_admin"
return None
app.dependency_overrides[get_db] = override_get_db
app.dependency_overrides[get_admin_user] = override_get_admin_user
app.dependency_overrides[get_optional_user] = override_get_optional_user
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as c:
yield c
app.dependency_overrides.clear()
@pytest.fixture
def auth_headers(client):
return {"Authorization": "Bearer test_token"}
@pytest.fixture
async def valid_tournament_payload(db):
# 1. Create global courts first
c1 = models.Court(name="Plan 1")
c2 = models.Court(name="Plan 2")
db.add_all([c1, c2])
db.commit()
# 2. Return payload with the NEW object format for teams!
return {
"name": "Test Tournament",
"code": "1234",
"type": "Double",
"timestamp": "2026-05-24T11:00:00",
"duration": 15,
"teams": [
{"id": "temp-1", "name": "Team A", "dropped_out": False},
{"id": "temp-2", "name": "Team B", "dropped_out": False},
{"id": "temp-3", "name": "Team C", "dropped_out": False},
{"id": "temp-4", "name": "Team D", "dropped_out": False},
],
"courts": [c1.id, c2.id],
}