Spaces:
Sleeping
Sleeping
File size: 2,619 Bytes
4e65a9c | 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 | import pytest
from unittest.mock import MagicMock, patch
from fastapi.testclient import TestClient
import io
from PIL import Image
from backend.api import app
client = TestClient(app)
def test_read_root():
"""Test root endpoint / returns 200 and expected message."""
response = client.get("/")
assert response.status_code == 200
assert "ChiliCare API" in response.json()["message"]
def test_detect_disease_invalid_file_type():
"""Test /detect rejects non-image file uploads with 400 Bad Request."""
files = {"file": ("test.txt", b"dummy content", "text/plain")}
response = client.post("/detect", files=files)
assert response.status_code == 400
assert "gambar" in response.json()["detail"]
@patch("backend.api.model")
@patch("backend.api.generate_narrative")
def test_detect_disease_success(mock_generate_narrative, mock_model):
"""Test /detect endpoint with a valid image file and mocked YOLO model."""
mock_generate_narrative.return_value = "Saran penanganan penyakit kuning."
# Mock YOLO model box output
mock_box = MagicMock()
mock_box.cls = [0]
mock_box.conf = [0.95]
mock_result = MagicMock()
mock_result.boxes = [mock_box]
# plot() returns a dummy numpy array representing RGB image
import numpy as np
mock_result.plot.return_value = np.zeros((100, 100, 3), dtype=np.uint8)
mock_model.return_value = [mock_result]
mock_model.names = {0: "kuning"}
# Create a small dummy image in bytes
img = Image.new("RGB", (50, 50), color="green")
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format="JPEG")
img_bytes = img_byte_arr.getvalue()
files = {"file": ("chili_leaf.jpg", img_bytes, "image/jpeg")}
response = client.post("/detect", files=files)
assert response.status_code == 200
data = response.json()
assert data["status"] == "success"
assert data["total_detections"] == 1
assert data["results"][0]["class"] == "Kuning"
assert data["results"][0]["narrative"] == "Saran penanganan penyakit kuning."
@patch("backend.api.create_rag_chain")
def test_ask_expert_endpoint(mock_create_rag_chain):
"""Test /ask endpoint streaming response from RAG chain."""
mock_chain = MagicMock()
mock_chain.stream.return_value = ["Tanaman ", "cabai ", "membutuhkan ", "air."]
mock_create_rag_chain.return_value = mock_chain
payload = {"question": "Bagaimana penyiraman cabai?"}
response = client.post("/ask", json=payload)
assert response.status_code == 200
assert "Tanaman cabai membutuhkan air." in response.text
|