Some checks failed
Build & Push Docker / build (push) Has been cancelled
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
181 lines
5.0 KiB
Python
181 lines
5.0 KiB
Python
"""
|
|
API endpoint tests using FastAPI TestClient.
|
|
"""
|
|
|
|
import io
|
|
import json
|
|
import pandas as pd
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
return TestClient(app)
|
|
|
|
|
|
def make_excel_bytes(df: pd.DataFrame) -> bytes:
|
|
bio = io.BytesIO()
|
|
with pd.ExcelWriter(bio, engine='openpyxl') as writer:
|
|
df.to_excel(writer, index=False)
|
|
return bio.getvalue()
|
|
|
|
|
|
def test_health(client):
|
|
r = client.get("/api/health")
|
|
assert r.status_code == 200
|
|
data = r.json()
|
|
assert data["status"] == "ok"
|
|
assert data["version"] == "2.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 "Scenar Creator" in r.text
|
|
|
|
|
|
def test_validate_valid(client):
|
|
doc = {
|
|
"event": {"title": "Test", "detail": "Detail"},
|
|
"program_types": [{"code": "WS", "description": "Workshop", "color": "#FF0000"}],
|
|
"blocks": [{
|
|
"datum": "2025-11-13",
|
|
"zacatek": "09:00:00",
|
|
"konec": "10:00:00",
|
|
"program": "Opening",
|
|
"typ": "WS"
|
|
}]
|
|
}
|
|
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 = {
|
|
"event": {"title": "Test", "detail": "Detail"},
|
|
"program_types": [{"code": "WS", "description": "Workshop", "color": "#FF0000"}],
|
|
"blocks": [{
|
|
"datum": "2025-11-13",
|
|
"zacatek": "09:00:00",
|
|
"konec": "10:00:00",
|
|
"program": "Opening",
|
|
"typ": "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 = {
|
|
"event": {"title": "Test", "detail": "Detail"},
|
|
"program_types": [{"code": "WS", "description": "Workshop", "color": "#FF0000"}],
|
|
"blocks": [{
|
|
"datum": "2025-11-13",
|
|
"zacatek": "10:00:00",
|
|
"konec": "09:00:00",
|
|
"program": "Bad",
|
|
"typ": "WS"
|
|
}]
|
|
}
|
|
r = client.post("/api/validate", json=doc)
|
|
assert r.status_code == 200
|
|
data = r.json()
|
|
assert data["valid"] is False
|
|
|
|
|
|
def test_import_excel(client):
|
|
df = pd.DataFrame({
|
|
'Datum': [pd.Timestamp('2025-11-13').date()],
|
|
'Zacatek': ['09:00'],
|
|
'Konec': ['10:00'],
|
|
'Program': ['Test Program'],
|
|
'Typ': ['WORKSHOP'],
|
|
'Garant': ['John'],
|
|
'Poznamka': ['Note']
|
|
})
|
|
|
|
content = make_excel_bytes(df)
|
|
r = client.post(
|
|
"/api/import-excel",
|
|
files={"file": ("test.xlsx", content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")},
|
|
data={"title": "Imported Event", "detail": "From Excel"}
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
data = r.json()
|
|
assert data["success"] is True
|
|
assert data["document"] is not None
|
|
assert data["document"]["event"]["title"] == "Imported Event"
|
|
assert len(data["document"]["blocks"]) == 1
|
|
|
|
|
|
def test_generate_excel(client):
|
|
doc = {
|
|
"event": {"title": "Test Event", "detail": "Test Detail"},
|
|
"program_types": [{"code": "WS", "description": "Workshop", "color": "#0070C0"}],
|
|
"blocks": [{
|
|
"datum": "2025-11-13",
|
|
"zacatek": "09:00:00",
|
|
"konec": "10:00:00",
|
|
"program": "Opening",
|
|
"typ": "WS",
|
|
"garant": "John",
|
|
"poznamka": "Note"
|
|
}]
|
|
}
|
|
r = client.post("/api/generate-excel", json=doc)
|
|
assert r.status_code == 200
|
|
assert "spreadsheetml" in r.headers["content-type"]
|
|
assert len(r.content) > 0
|
|
|
|
|
|
def test_generate_excel_no_blocks(client):
|
|
doc = {
|
|
"event": {"title": "Test", "detail": "Detail"},
|
|
"program_types": [{"code": "WS", "description": "Workshop", "color": "#FF0000"}],
|
|
"blocks": []
|
|
}
|
|
r = client.post("/api/generate-excel", json=doc)
|
|
assert r.status_code == 422
|
|
|
|
|
|
def test_export_json(client):
|
|
doc = {
|
|
"event": {"title": "Test", "detail": "Detail"},
|
|
"program_types": [{"code": "WS", "description": "Workshop", "color": "#FF0000"}],
|
|
"blocks": [{
|
|
"datum": "2025-11-13",
|
|
"zacatek": "09:00:00",
|
|
"konec": "10:00:00",
|
|
"program": "Opening",
|
|
"typ": "WS"
|
|
}]
|
|
}
|
|
r = client.post("/api/export-json", json=doc)
|
|
assert r.status_code == 200
|
|
data = r.json()
|
|
assert data["event"]["title"] == "Test"
|
|
assert len(data["blocks"]) == 1
|
|
|
|
|
|
def test_template_download(client):
|
|
r = client.get("/api/template")
|
|
# Template might not exist in test env, but endpoint should work
|
|
assert r.status_code in [200, 404]
|
|
|
|
|
|
def test_swagger_docs(client):
|
|
r = client.get("/docs")
|
|
assert r.status_code == 200
|