from __future__ import annotations import pytest from fastapi.testclient import TestClient from fan_passport.config import Settings from fan_passport.content_importer import import_seed_payload from fan_passport.database import SessionLocal, configure_database, init_db from fan_passport.main import create_app TEST_SEED = { "competitions": [ { "code": "test-cup", "name": "Test Cup", "season": "2026", "governing_body": "TEST", "starts_at": "2026-06-01T00:00:00Z", "ends_at": "2026-07-01T00:00:00Z", "active": True, } ], "teams": [ { "id": "ALPHA", "competition_code": "test-cup", "name": "Alpha FC", "fifa_code": "ALP", "country_code": "AA", "group_name": "A", }, { "id": "BETA", "competition_code": "test-cup", "name": "Beta FC", "fifa_code": "BET", "country_code": "BB", "group_name": "A", }, ], "stadiums": [ { "id": "TEST-STADIUM", "competition_code": "test-cup", "name": "Test Stadium", "city": "Testville", "country": "Testland", "capacity": 50000, } ], "stickers": [ { "id": "STICKER-ALPHA", "competition_code": "test-cup", "name": "Alpha Crest", "rarity": "common", "team_id": "ALPHA", } ], "matches": [ { "id": "MATCH-1", "competition_code": "test-cup", "fifa_match_no": 1, "stage": "group", "group_name": "A", "home_team_id": "ALPHA", "away_team_id": "BETA", "stadium_id": "TEST-STADIUM", "kickoff_at": "2026-06-10T12:00:00Z", "status": "scheduled", } ], "quiz_questions": [ { "id": "QUIZ-1", "competition_code": "test-cup", "slug": "test-host", "prompt": "What is the answer?", "options_json": [{"id": "A", "text": "Wrong"}, {"id": "B", "text": "Right"}], "correct_option": "B", "explanation": "B is right.", "difficulty": "easy", "scheduled_date": "2026-06-01", } ], "challenges": [ { "competition_code": "test-cup", "slug": "start", "title": "Start", "description": "Create a profile.", "category": "onboarding", "points": 10, "rules_json": {"type": "profile_created", "target": 1}, }, { "competition_code": "test-cup", "slug": "collect-one-team", "title": "Collect One Team", "description": "Collect any team.", "category": "collection", "points": 30, "rules_json": {"type": "collect_count", "item_type": "team", "target": 1}, }, { "competition_code": "test-cup", "slug": "complete-group-a", "title": "Complete Group A", "description": "Collect Alpha and Beta.", "category": "collection", "points": 80, "rules_json": {"type": "collect_group", "group_name": "A", "competition_code": "test-cup"}, }, { "competition_code": "test-cup", "slug": "call-giant-killing", "title": "Call Giant Killing", "description": "Predict an upset.", "category": "prediction", "points": 70, "rules_json": {"type": "giant_killing_predictions", "target": 1}, }, ], "badges": [ { "competition_code": "test-cup", "slug": "founder", "title": "Founder", "description": "Profile created.", "tier": "bronze", "points": 5, "criteria_json": {"type": "profile_created", "target": 1}, }, { "competition_code": "test-cup", "slug": "team-collector", "title": "Team Collector", "description": "Collected a team.", "tier": "bronze", "points": 5, "criteria_json": {"type": "collect_count", "item_type": "team", "target": 1}, }, { "competition_code": "test-cup", "slug": "upset-prophet", "title": "Upset Prophet", "description": "Correct giant killing.", "tier": "gold", "points": 25, "criteria_json": {"type": "giant_killing_predictions", "target": 1}, }, ], } @pytest.fixture() def client() -> TestClient: settings = Settings( environment="test", database_url="sqlite+pysqlite:///:memory:", auto_create_tables=True, admin_api_key="test-admin", enable_dev_auth=True, ) configure_database(settings.database_url) init_db() with SessionLocal() as session: import_seed_payload(session, TEST_SEED) session.commit() app = create_app(settings) with TestClient(app) as test_client: yield test_client