Download tests/test_api.py from alaa3/TouRa: direct link, hf CLI and curl.
- Browser
- Download file 3.16 kB
-
https://huggingface.co/spaces/alaa3/TouRa/resolve/main/tests/test_api.py
- Command line
-
hf download hf://spaces/alaa3/TouRa/tests/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/alaa3/TouRa/resolve/main/tests/test_api.py
3.16 kB
| """ | |
| 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" | |
| 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() | |
| 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 | |
| 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) | |
| 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 | |