Spaces:
Running
Running
File size: 4,478 Bytes
7701077 | 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | """
API endpoint testleri.
ΓalΔ±ΕtΔ±rmak iΓ§in: pytest src/test_api.py -v
API'nin 8000'de Γ§alΔ±ΕΔ±yor olmasΔ± gerekir.
"""
import pytest
import requests
BASE_URL = "http://localhost:8000"
# ββ Health ββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_health():
r = requests.get(f"{BASE_URL}/health")
assert r.status_code == 200
assert r.json()["status"] == "ok"
def test_root():
r = requests.get(f"{BASE_URL}/")
assert r.status_code == 200
# ββ Predict βββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_predict_positive():
r = requests.post(f"{BASE_URL}/predict", json={
"text": "Revenue surged 40% driven by record-breaking demand."
})
assert r.status_code == 200
data = r.json()
assert data["sentiment"] == "positive"
assert data["confidence"] > 0.8
assert data["language"] == "en"
assert "keywords" in data
assert "risk_score" in data
assert "risk_level" in data
def test_predict_negative():
r = requests.post(f"{BASE_URL}/predict", json={
"text": "Stock prices crashed after company reported massive losses."
})
assert r.status_code == 200
data = r.json()
assert data["sentiment"] == "negative"
assert data["confidence"] > 0.7
def test_predict_neutral():
r = requests.post(f"{BASE_URL}/predict", json={
"text": "Net sales remained stable compared to the previous fiscal year."
})
assert r.status_code == 200
data = r.json()
assert data["sentiment"] == "neutral"
def test_predict_turkish():
r = requests.post(f"{BASE_URL}/predict", json={
"text": "BIST 100 rekor kΔ±rdΔ±, yatΔ±rΔ±mcΔ±lar bΓΌyΓΌk kazanΓ§ elde etti."
})
assert r.status_code == 200
data = r.json()
assert data["language"] == "tr"
assert data["translated_text"] is not None
assert data["sentiment"] in ["positive", "neutral", "negative"]
def test_predict_response_schema():
r = requests.post(f"{BASE_URL}/predict", json={
"text": "Company earnings beat expectations."
})
assert r.status_code == 200
data = r.json()
required_fields = [
"text", "sentiment", "confidence", "language",
"scores", "keywords", "risk_score", "risk_level", "latency_ms"
]
for field in required_fields:
assert field in data, f"Eksik alan: {field}"
assert data["sentiment"] in ["positive", "neutral", "negative"]
assert 0 <= data["confidence"] <= 1
assert data["risk_level"] in ["LOW", "MEDIUM", "HIGH"]
# ββ Validation ββββββββββββββββββββββββββββββββββββββββββββββββ
def test_predict_empty_text():
r = requests.post(f"{BASE_URL}/predict", json={"text": ""})
assert r.status_code == 422
def test_predict_too_long_text():
r = requests.post(f"{BASE_URL}/predict", json={"text": "x" * 2001})
assert r.status_code == 422
# ββ Batch βββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_batch_predict():
r = requests.post(f"{BASE_URL}/predict/batch", json={
"texts": [
"Profits rose sharply this quarter.",
"Company filed for bankruptcy.",
"Sales remained flat year over year.",
]
})
assert r.status_code == 200
data = r.json()
assert len(data["results"]) == 3
assert data["results"][0]["sentiment"] == "positive"
assert data["results"][1]["sentiment"] == "negative"
assert data["results"][2]["sentiment"] == "neutral"
def test_batch_too_large():
r = requests.post(f"{BASE_URL}/predict/batch", json={
"texts": ["text"] * 33
})
assert r.status_code == 422
def test_batch_empty():
r = requests.post(f"{BASE_URL}/predict/batch", json={"texts": []})
assert r.status_code == 422
# ββ Monitoring ββββββββββββββββββββββββββββββββββββββββββββββββ
def test_monitoring_stats():
r = requests.get(f"{BASE_URL}/monitoring/stats")
assert r.status_code == 200
data = r.json()
assert "total" in data
|