Spaces:
Sleeping
Sleeping
File size: 1,857 Bytes
4a0e21d | 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 | """
Unit tests for backend.service.run_inference and /api/v1/analyze endpoint.
"""
import unittest
from unittest.mock import patch
from backend.service import ml_service
from backend.pydantic_models import SpectrumData # Adjust import if needed
from fastapi.testclient import TestClient
from backend.main import app
class TestService(unittest.TestCase):
"""Tests for ml_service.run_inference."""
@patch('backend.service.log_model_performance')
def test_run_inference_calls_log_model_performance(self, mock_log_model_performance):
"""Test that run_inference calls log_model_performance with valid input."""
# Build a real SpectrumData instance with required fields only
dummy_spectrum = SpectrumData(
x_values=[200, 210],
y_values=[0.5, 0.6],
filename="dummy.txt"
)
model_name = "figure2"
modality = "raman"
# Call with separate model_name and modality args (not as SpectrumData attributes)
ml_service.run_inference(dummy_spectrum, model_name, modality)
mock_log_model_performance.assert_called_once()
class TestAPI(unittest.TestCase):
"""Tests for /api/v1/analyze endpoint."""
def setUp(self):
self.client = TestClient(app)
def test_analyze_spectrum_valid_payload(self):
"""Test /api/v1/analyze with valid payload."""
payload = {
"spectrum": {
"x_values": [200, 210],
"y_values": [0.5, 0.6],
"filename": "dummy.txt"
},
"modality": "raman",
"model_name": "figure2"
}
response = self.client.post("/api/v1/analyze", json=payload)
assert response.status_code == 200
# Optionally, check response.json() for expected keys
if __name__ == "__main__":
unittest.main()
|