File size: 2,266 Bytes
6595ce6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Unit tests for app.py endpoints."""
import os
import sys
import unittest

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

from fastapi.testclient import TestClient
from app import app


class TestAPIEndpoints(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.client = TestClient(app)
        # Trigger lifespan
        cls.client.__enter__()

    @classmethod
    def tearDownClass(cls):
        cls.client.__exit__(None, None, None)

    def test_health_endpoint(self):
        """Health check should return ok status."""
        response = self.client.get("/health")
        self.assertEqual(response.status_code, 200)
        
        data = response.json()
        self.assertEqual(data["status"], "ok")
        self.assertIn("model", data)
        self.assertIn(data["model"], ["qwen2.5-3b-lora", "fallback"])

    def test_humanize_endpoint_fallback(self):
        """Humanize endpoint should work in fallback mode."""
        response = self.client.post(
            "/humanize",
            json={"text": "In order to provide you with results, it is important to note that data was collected."}
        )
        self.assertEqual(response.status_code, 200)
        
        data = response.json()
        self.assertIn("original", data)
        self.assertIn("humanized", data)
        self.assertIn("model", data)
        self.assertEqual(data["model"], "fallback")
        self.assertNotEqual(data["original"], data["humanized"])

    def test_humanize_empty_text(self):
        """Empty text should return 400."""
        response = self.client.post("/humanize", json={"text": "   "})
        self.assertEqual(response.status_code, 400)

    def test_humanize_missing_text(self):
        """Missing text field should return 422 validation error."""
        response = self.client.post("/humanize", json={})
        self.assertEqual(response.status_code, 422)

    def test_root_endpoint(self):
        """Root should serve HTML UI."""
        response = self.client.get("/")
        self.assertEqual(response.status_code, 200)
        self.assertIn("text/html", response.headers["content-type"])
        self.assertIn("humanize-e", response.text)


if __name__ == '__main__':
    unittest.main()