95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
"""Test fixtures."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.config import Settings
|
|
from app.main import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_pool():
|
|
return {
|
|
"title": "Sample Quiz",
|
|
"score_fn": "linear_decay",
|
|
"time_limit_default": 2,
|
|
"questions": [
|
|
{
|
|
"id": "q1",
|
|
"text": "First question?",
|
|
"options": {"A": "Alpha", "B": "Beta", "C": "Gamma", "D": "Delta"},
|
|
"correct": "B",
|
|
"time_limit": 2,
|
|
"explanation": "B is correct.",
|
|
},
|
|
{
|
|
"id": "q2",
|
|
"text": "Second question?",
|
|
"options": {"A": "One", "B": "Two", "C": "Three", "D": "Four"},
|
|
"correct": "C",
|
|
"time_limit": 2,
|
|
},
|
|
{
|
|
"id": "q3",
|
|
"text": "Third question?",
|
|
"options": {"A": "Red", "B": "Blue", "C": "Green", "D": "Gold"},
|
|
"correct": "A",
|
|
"time_limit": 2,
|
|
},
|
|
{
|
|
"id": "q4",
|
|
"text": "Fourth question?",
|
|
"options": {"A": "North", "B": "South", "C": "East", "D": "West"},
|
|
"correct": "D",
|
|
"time_limit": 2,
|
|
},
|
|
{
|
|
"id": "q5",
|
|
"text": "Fifth question?",
|
|
"options": {"A": "Fetch", "B": "Decode", "C": "Execute", "D": "Write"},
|
|
"correct": "A",
|
|
"time_limit": 1,
|
|
},
|
|
],
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def client(tmp_path):
|
|
settings = Settings(
|
|
db_path=str(tmp_path / "quiz.db"),
|
|
secret_key="test-secret",
|
|
admin_password="admin-pass",
|
|
public_url="http://testserver",
|
|
)
|
|
app = create_app(settings)
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
|
|
|
|
def admin_login(client: TestClient) -> None:
|
|
response = client.post("/admin/login", json={"password": "admin-pass"})
|
|
assert response.status_code == 200
|
|
|
|
|
|
def create_quiz(client: TestClient, pool: dict) -> int:
|
|
admin_login(client)
|
|
response = client.post("/admin/api/quizzes", json={"pool_json": pool})
|
|
assert response.status_code == 200, response.text
|
|
return response.json()["quiz_id"]
|
|
|
|
|
|
def create_session(client: TestClient, pool: dict) -> str:
|
|
quiz_id = create_quiz(client, pool)
|
|
response = client.post("/admin/api/sessions", json={"quiz_id": quiz_id})
|
|
assert response.status_code == 200, response.text
|
|
return response.json()["sid"]
|
|
|
|
|
|
def join_student(client: TestClient, sid: str, student_id: str = "s1", name: str = "Student One") -> dict:
|
|
response = client.post(f"/api/session/{sid}/join", json={"student_id": student_id, "name": name})
|
|
assert response.status_code == 200, response.text
|
|
return response.json()
|