File size: 3,643 Bytes
0d683e2
 
 
 
 
fa85955
0d683e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0497d92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c658eb
0497d92
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
"""
Integration tests for API endpoints.
"""
import pytest
from unittest.mock import patch
from starlette.testclient import TestClient
from app.main import app

from tests.test_services import StubAsyncClient, StubAsyncResponse


client = TestClient(app)


@pytest.mark.integration
def test_summarize_endpoint_success(sample_text, mock_ollama_response):
    """Test successful summarization via API endpoint."""
    stub_response = StubAsyncResponse(json_data=mock_ollama_response)
    with patch('httpx.AsyncClient', return_value=StubAsyncClient(post_result=stub_response)):
        resp = client.post(
            "/api/v1/summarize/",
            json={"text": sample_text, "max_tokens": 128}
        )
        assert resp.status_code == 200
        data = resp.json()
        assert data["summary"] == mock_ollama_response["response"]
        assert data["model"]


@pytest.mark.integration
def test_summarize_endpoint_validation_error():
    """Test validation error for empty text."""
    resp = client.post(
        "/api/v1/summarize/",
        json={"text": ""}
    )
    assert resp.status_code == 422

# Tests for Better Error Handling
@pytest.mark.integration
def test_summarize_endpoint_timeout_error():
    """Test that timeout errors return 504 Gateway Timeout instead of 502."""
    import httpx
    
    with patch('httpx.AsyncClient', return_value=StubAsyncClient(post_exc=httpx.TimeoutException("Timeout"))):
        resp = client.post(
            "/api/v1/summarize/",
            json={"text": "Test text that will timeout"}
        )
        assert resp.status_code == 504  # Gateway Timeout
        data = resp.json()
        assert "timeout" in data["detail"].lower()
        assert "text may be too long" in data["detail"].lower()

@pytest.mark.integration
def test_summarize_endpoint_http_error():
    """Test that HTTP errors return 502 Bad Gateway."""
    import httpx
    
    http_error = httpx.HTTPStatusError("Bad Request", request=MagicMock(), response=MagicMock())
    with patch('httpx.AsyncClient', return_value=StubAsyncClient(post_exc=http_error)):
        resp = client.post(
            "/api/v1/summarize/",
            json={"text": "Test text"}
        )
        assert resp.status_code == 502  # Bad Gateway
        data = resp.json()
        assert "Summarization failed" in data["detail"]

@pytest.mark.integration
def test_summarize_endpoint_unexpected_error():
    """Test that unexpected errors return 500 Internal Server Error."""
    with patch('httpx.AsyncClient', return_value=StubAsyncClient(post_exc=Exception("Unexpected error"))):
        resp = client.post(
            "/api/v1/summarize/",
            json={"text": "Test text"}
        )
        assert resp.status_code == 500  # Internal Server Error
        data = resp.json()
        assert "Internal server error" in data["detail"]

@pytest.mark.integration
def test_summarize_endpoint_large_text_handling():
    """Test that large text requests are handled with appropriate timeout."""
    large_text = "A" * 5000  # Large text that should trigger dynamic timeout
    
    with patch('httpx.AsyncClient') as mock_client:
        mock_client.return_value = StubAsyncClient(post_result=StubAsyncResponse())
        
        resp = client.post(
            "/api/v1/summarize/",
            json={"text": large_text, "max_tokens": 256}
        )
        
        # Verify the client was called with extended timeout
        mock_client.assert_called_once()
        call_args = mock_client.call_args
        expected_timeout = 60 + (5000 - 1000) // 1000 * 5  # 80 seconds
        assert call_args[1]['timeout'] == expected_timeout