File size: 2,610 Bytes
1130076
 
 
d9530b5
1130076
 
d9530b5
1130076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9530b5
1130076
 
 
 
 
 
 
d0a5e7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 fastapi.testclient import TestClient
import os
os.environ["DATABASE_URL"] = "sqlite:///./tests/fixtures/test.db"
import unittest.mock as mock

from api.app.main import app
import json
import io

client = TestClient(app)

def test_health_endpoint():
    with TestClient(app) as client:
        response = client.get("/health")
        assert response.status_code == 200
        assert response.json()["status"] == "ok"

def test_predict_english():
    with TestClient(app) as client:
        payload = {"text": "The food was great but service was slow.", "language": "en"}
        response = client.post("/predict", json=payload)
        assert response.status_code == 200
        data = response.json()
        assert data["language"] == "en"
        assert "aspects" in data

def test_predict_hindi():
    with TestClient(app) as client:
        payload = {"text": "खाना बहुत अच्छा था", "language": "hi"}
        response = client.post("/predict", json=payload)
        assert response.status_code == 200
        data = response.json()
        assert data["language"] == "hi"
        assert "aspects" in data

def test_predict_empty():
    with TestClient(app) as client:
        payload = {"text": ""}
        response = client.post("/predict", json=payload)
        assert response.status_code == 200

def test_batch_upload():
    with TestClient(app) as client:
        csv_content = "text\nThe food was great\nTerrible service"
        files = {"file": ("test.csv", io.BytesIO(csv_content.encode("utf-8")), "text/csv")}
        with mock.patch("api.app.routes.predict.process_batch.delay") as mock_delay:
            response = client.post("/batch", files=files)
            assert response.status_code == 200
            data = response.json()
            assert "job_id" in data
            assert data["status"] == "queued"
            assert data["total_reviews"] == 2
            mock_delay.assert_called_once()

def test_info_endpoint():
    with TestClient(app) as client:
        response = client.get("/info")
        assert response.status_code == 200
        data = response.json()
        assert "model_name" in data
        assert "supported_languages" in data
        assert isinstance(data["supported_languages"], str)

def test_metrics_endpoint():
    with TestClient(app) as client:
        response = client.get("/metrics")
        assert response.status_code == 200
        # metrics returns plain text Prometheus data
        assert "text/plain" in response.headers["content-type"]
        assert "http_requests_total" in response.text