File size: 1,699 Bytes
84c52b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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