Spaces:
Sleeping
Sleeping
| import pytest | |
| from fastapi.testclient import TestClient | |
| from app.main import app | |
| import os | |
| import glob | |
| client = TestClient(app) | |
| def get_sample_image_path(): | |
| image_dir = os.path.join("image", "Khao_phat") | |
| files = glob.glob(os.path.join(image_dir, "*.jpg")) + \ | |
| glob.glob(os.path.join(image_dir, "*.png")) + \ | |
| glob.glob(os.path.join(image_dir, "*.jpeg")) | |
| if not files: | |
| pytest.fail("No sample images found") | |
| return files[0] | |
| def test_predict_api_returns_valid_json(): | |
| image_path = get_sample_image_path() | |
| with open(image_path, "rb") as f: | |
| response = client.post( | |
| "/predict", | |
| data={"model_type": "onnx"}, | |
| files={"file": ("test.jpg", f, "image/jpeg")} | |
| ) | |
| assert response.status_code == 200 | |
| json_data = response.json() | |
| # 1) เช็คว่าเป็น JSON structure ถูกต้อง | |
| assert isinstance(json_data, dict) | |
| assert "prediction_class_id" in json_data | |
| assert "confidence_score" in json_data | |
| assert "prediction_class_name" in json_data | |
| def test_predict_model_returns_prediction(): | |
| image_path = get_sample_image_path() | |
| with open(image_path, "rb") as f: | |
| response = client.post( | |
| "/predict", | |
| data={"model_type": "onnx"}, | |
| files={"file": ("test.jpg", f, "image/jpeg")} | |
| ) | |
| assert response.status_code == 200 | |
| json_data = response.json() | |
| # 2) เช็คว่า model “ทำนายได้จริง” | |
| assert json_data["prediction_class_name"] is not None | |
| assert json_data["confidence_score"] >= 0 |