File size: 3,158 Bytes
b0d0019 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | """
API endpoint tests using FastAPI TestClient.
"""
import pytest
from unittest.mock import patch, MagicMock
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_root():
response = client.get("/")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
assert "timestamp" in data
def test_health():
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
@patch("app.api.routes.run_full_crew", return_value="Mock itinerary result")
@patch("app.api.routes.load_and_preprocess", side_effect=FileNotFoundError)
def test_predict_no_data_file(mock_load, mock_crew):
payload = {
"from_city": "Cairo",
"destination_city": "Luxor",
"date_from": "2026/6/19",
"date_to": "2026/6/20",
"tags": ["historical_site", "restaurant"],
"interests": "sightseeing and food",
}
response = client.post("/predict", json=payload)
assert response.status_code == 200
data = response.json()
assert data["status"] == "success"
assert "itinerary" in data
mock_crew.assert_called_once()
@patch("app.api.routes.run_full_crew", side_effect=RuntimeError("LLM timeout"))
@patch("app.api.routes.load_and_preprocess", side_effect=FileNotFoundError)
def test_predict_error_handling(mock_load, mock_crew):
payload = {
"from_city": "Cairo",
"destination_city": "Luxor",
"date_from": "2026/6/19",
"date_to": "2026/6/20",
"tags": [],
"interests": "beaches",
}
response = client.post("/predict", json=payload)
assert response.status_code == 500
@patch("app.api.routes.run_full_crew", return_value="Batch result")
@patch("app.api.routes.load_and_preprocess", side_effect=FileNotFoundError)
def test_batch_predict(mock_load, mock_crew):
payload = [
{
"from_city": "Cairo",
"destination_city": "Luxor",
"date_from": "2026/6/19",
"date_to": "2026/6/20",
"tags": [],
"interests": "history",
},
{
"from_city": "Cairo",
"destination_city": "Hurghada",
"date_from": "2026/7/1",
"date_to": "2026/7/3",
"tags": ["beach"],
"interests": "beach and relaxation",
},
]
response = client.post("/batch_predict", json=payload)
assert response.status_code == 200
results = response.json()
assert len(results) == 2
assert all(r["status"] == "success" for r in results)
@patch("app.api.routes.chat_with_agent")
def test_chat(mock_chat):
mock_chat.return_value = {
"reply": "Sure! Which city in Egypt would you like to visit?",
"thread_id": "test_thread",
"message_count": 2,
}
payload = {"message": "I want to plan a trip", "thread_id": "test_thread"}
response = client.post("/chat", json=payload)
assert response.status_code == 200
data = response.json()
assert data["thread_id"] == "test_thread"
assert "reply" in data
|