File size: 4,167 Bytes
3d9ba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Unit & Integration tests for FastAPI server endpoints
"""

import os
import sys
import pytest

if hasattr(sys.stdout, 'reconfigure'):
    try:
        sys.stdout.reconfigure(encoding='utf-8')
    except Exception:
        pass

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from fastapi.testclient import TestClient
from api.server import app

client = TestClient(app)

def test_health_endpoint():
    res = client.get("/api/v1/health")
    assert res.status_code == 200
    data = res.json()
    assert data["status"] == "healthy"
    assert "loaded_checkpoints" in data
    print("  [✓] Health test passed")

def test_models_list():
    res = client.get("/api/v1/models")
    assert res.status_code == 200
    data = res.json()
    assert data["total_models"] >= 4
    print("  [✓] Models list test passed")

def test_detect_language():
    res_ar = client.post("/api/v1/detect-language", json={"text": "هذا مقال باللغة العربية الفصحى للتجربة."})
    assert res_ar.status_code == 200
    assert res_ar.json()["detected_language"] == "ar"

    res_en = client.post("/api/v1/detect-language", json={"text": "This is a clean English test paragraph for language detection."})
    assert res_en.status_code == 200
    assert res_en.json()["detected_language"] == "en"
    print("  [✓] Language detection test passed")

def test_tokenize_endpoint():
    res = client.post("/api/v1/tokenize", json={
        "text": "الذكاءُ الاصطناعيُّ يُحدِث ثورةً. هل توافق؟",
        "lang": "ar"
    })
    assert res.status_code == 200
    data = res.json()
    assert data["sentence_count"] == 2
    assert len(data["tokens"]) > 0
    print("  [✓] Tokenize test passed")

def test_summarize_extractive_arabic():
    res = client.post("/api/v1/summarize", json={
        "text": "شهدت الاسواق العالمية تراجعا حادا في اسعار النفط الخام بعد قرار منظمة اوبك زيادة الانتاج. ادى ذلك الى انخفاض سعر برميل النفط الى ما دون الثمانين دولارا للمرة الاولى منذ عام كامل.",
        "mode": "extractive",
        "method": "textrank",
        "sentences": 1
    })
    assert res.status_code == 200
    data = res.json()
    assert len(data["summary"]) > 0
    assert data["detected_language"] == "ar"
    print("  [✓] Extractive Arabic summarization test passed")

def test_summarize_abstractive_arabic():
    res = client.post("/api/v1/summarize", json={
        "text": "اعلنت وكالة ناسا الفضائية الامريكية عن خطط طموحة للعودة الى سطح القمر في اطار برنامج ارتيميس حيث ستشمل البعثة اول امراة واول شخص من ذوي البشرة الداكنة.",
        "mode": "abstractive",
        "method": "seq2seq",
        "lang": "ar",
        "beam_width": 3
    })
    assert res.status_code == 200
    data = res.json()
    assert len(data["summary"]) > 0
    assert data["mode"] == "abstractive"
    print("  [✓] Abstractive Arabic summarization test passed:", data["summary"])

def test_evaluate_endpoint():
    res = client.post("/api/v1/evaluate", json={
        "original_text": "Global semiconductor shortages are easing according to the latest industry report with chip inventories returning to normal.",
        "generated_summary": "Semiconductor shortage eases as chip inventories return to normal levels.",
        "reference_summary": "Global chip shortage easing with inventories normalizing across industry.",
        "lang": "en"
    })
    assert res.status_code == 200
    data = res.json()
    assert "rouge_1" in data
    assert "bleu_cumulative" in data
    print("  [✓] Evaluate test passed")

if __name__ == "__main__":
    test_health_endpoint()
    test_models_list()
    test_detect_language()
    test_tokenize_endpoint()
    test_summarize_extractive_arabic()
    test_summarize_abstractive_arabic()
    test_evaluate_endpoint()
    print("\n[🎉] ALL API INTEGRATION TESTS PASSED SUCCESSFULLY!")