Files
scenar-creator/tests/test_api.py
Daneel 25fd578543
Some checks failed
Build & Push Docker / build (push) Has been cancelled
feat: v3.0 - canvas editor, JSON-only, no Excel, new UI
- Remove all Excel code (import, export, template, pandas, openpyxl)
- New canvas-based schedule editor with drag & drop (interact.js)
- Modern 3-panel UI: sidebar, canvas, documentation tab
- New data model: Block with id/date/start/end, ProgramType with id/name/color
- Clean API: GET /api/health, POST /api/validate, GET /api/sample, POST /api/generate-pdf
- Rewritten PDF generator using ScenarioDocument directly (no DataFrame)
- Professional PDF output: dark header, colored blocks, merged cells, legend, footer
- Sample JSON: "Zimní výjezd oddílu" with 11 blocks, 3 program types
- 30 tests passing (API, core models, PDF generation)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 17:02:51 +01:00

147 lines
3.7 KiB
Python

"""
API endpoint tests for Scenar Creator v3.
"""
import pytest
from fastapi.testclient import TestClient
from app.main import app
@pytest.fixture
def client():
return TestClient(app)
def make_valid_doc():
return {
"version": "1.0",
"event": {"title": "Test Event"},
"program_types": [{"id": "ws", "name": "Workshop", "color": "#FF0000"}],
"blocks": [{
"id": "b1",
"date": "2026-03-01",
"start": "09:00",
"end": "10:00",
"title": "Opening",
"type_id": "ws"
}]
}
def test_health(client):
r = client.get("/api/health")
assert r.status_code == 200
data = r.json()
assert data["status"] == "ok"
assert data["version"] == "3.0.0"
def test_root_returns_html(client):
r = client.get("/")
assert r.status_code == 200
assert "text/html" in r.headers["content-type"]
assert "Scen" in r.text and "Creator" in r.text
def test_validate_valid(client):
doc = make_valid_doc()
r = client.post("/api/validate", json=doc)
assert r.status_code == 200
data = r.json()
assert data["valid"] is True
assert data["errors"] == []
def test_validate_unknown_type(client):
doc = make_valid_doc()
doc["blocks"][0]["type_id"] = "UNKNOWN"
r = client.post("/api/validate", json=doc)
assert r.status_code == 200
data = r.json()
assert data["valid"] is False
assert any("UNKNOWN" in e for e in data["errors"])
def test_validate_bad_time_order(client):
doc = make_valid_doc()
doc["blocks"][0]["start"] = "10:00"
doc["blocks"][0]["end"] = "09:00"
r = client.post("/api/validate", json=doc)
assert r.status_code == 200
data = r.json()
assert data["valid"] is False
assert any("start time" in e for e in data["errors"])
def test_validate_no_blocks(client):
doc = make_valid_doc()
doc["blocks"] = []
r = client.post("/api/validate", json=doc)
assert r.status_code == 200
data = r.json()
assert data["valid"] is False
def test_validate_no_types(client):
doc = make_valid_doc()
doc["program_types"] = []
r = client.post("/api/validate", json=doc)
assert r.status_code == 200
data = r.json()
assert data["valid"] is False
def test_sample_endpoint(client):
r = client.get("/api/sample")
assert r.status_code == 200
data = r.json()
assert data["version"] == "1.0"
assert data["event"]["title"] == "Zimní výjezd oddílu"
assert len(data["program_types"]) == 3
assert len(data["blocks"]) >= 8
def test_sample_blocks_valid(client):
r = client.get("/api/sample")
data = r.json()
type_ids = {pt["id"] for pt in data["program_types"]}
for block in data["blocks"]:
assert block["type_id"] in type_ids
assert block["start"] < block["end"]
def test_generate_pdf(client):
doc = make_valid_doc()
r = client.post("/api/generate-pdf", json=doc)
assert r.status_code == 200
assert r.headers["content-type"] == "application/pdf"
assert r.content[:5] == b'%PDF-'
def test_generate_pdf_no_blocks(client):
doc = make_valid_doc()
doc["blocks"] = []
r = client.post("/api/generate-pdf", json=doc)
assert r.status_code == 422
def test_generate_pdf_multiday(client):
doc = make_valid_doc()
doc["blocks"].append({
"id": "b2",
"date": "2026-03-02",
"start": "14:00",
"end": "15:00",
"title": "Day 2 Session",
"type_id": "ws"
})
r = client.post("/api/generate-pdf", json=doc)
assert r.status_code == 200
assert r.content[:5] == b'%PDF-'
def test_swagger_docs(client):
r = client.get("/docs")
assert r.status_code == 200