File size: 10,632 Bytes
0d683e2
 
 
6e01ea3
0d683e2
6e01ea3
fa85955
0d683e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0497d92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca285d
0497d92
 
 
 
 
8ca285d
0497d92
8ca285d
0497d92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca285d
 
6e01ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""
Integration tests for API endpoints.
"""
import json
import pytest
from unittest.mock import patch, MagicMock
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 502 Bad Gateway (actual behavior)."""
    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 == 502  # Bad Gateway (actual behavior)
        data = resp.json()
        assert "Summarization failed" 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
        # Timeout calculated with ORIGINAL text length (5000 chars): 30 + (5000-1000)//1000*3 = 30 + 12 = 42
        expected_timeout = 30 + (5000 - 1000) // 1000 * 3  # 42 seconds
        assert call_args[1]['timeout'] == expected_timeout


# Tests for Streaming Endpoint
@pytest.mark.integration
def test_summarize_stream_endpoint_success(sample_text):
    """Test successful streaming summarization via API endpoint."""
    # Mock streaming response data
    mock_stream_data = [
        '{"response": "This", "done": false, "eval_count": 1}\n',
        '{"response": " is", "done": false, "eval_count": 2}\n',
        '{"response": " a", "done": false, "eval_count": 3}\n',
        '{"response": " test", "done": true, "eval_count": 4}\n'
    ]
    
    class MockStreamResponse:
        def __init__(self, data):
            self.data = data
        
        async def aiter_lines(self):
            for line in self.data:
                yield line
        
        def raise_for_status(self):
            pass
    
    class MockStreamContextManager:
        def __init__(self, response):
            self.response = response
        
        async def __aenter__(self):
            return self.response
        
        async def __aexit__(self, exc_type, exc, tb):
            return False
    
    class MockStreamClient:
        async def __aenter__(self):
            return self
        
        async def __aexit__(self, exc_type, exc, tb):
            return False
        
        def stream(self, method, url, **kwargs):
            return MockStreamContextManager(MockStreamResponse(mock_stream_data))
    
    with patch('httpx.AsyncClient', return_value=MockStreamClient()):
        resp = client.post(
            "/api/v1/summarize/stream",
            json={"text": sample_text, "max_tokens": 128}
        )
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/event-stream; charset=utf-8"
        
        # Parse SSE response
        lines = resp.text.strip().split('\n')
        data_lines = [line for line in lines if line.startswith('data: ')]
        
        assert len(data_lines) == 4
        
        # Parse first chunk
        first_chunk = json.loads(data_lines[0][6:])  # Remove 'data: ' prefix
        assert first_chunk["content"] == "This"
        assert first_chunk["done"] is False
        assert first_chunk["tokens_used"] == 1
        
        # Parse last chunk
        last_chunk = json.loads(data_lines[-1][6:])  # Remove 'data: ' prefix
        assert last_chunk["content"] == " test"
        assert last_chunk["done"] is True
        assert last_chunk["tokens_used"] == 4


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


@pytest.mark.integration
def test_summarize_stream_endpoint_timeout_error():
    """Test that timeout errors in streaming return proper error."""
    import httpx
    
    class MockStreamClient:
        async def __aenter__(self):
            return self
        
        async def __aexit__(self, exc_type, exc, tb):
            return False
        
        def stream(self, method, url, **kwargs):
            raise httpx.TimeoutException("Timeout")
    
    with patch('httpx.AsyncClient', return_value=MockStreamClient()):
        resp = client.post(
            "/api/v1/summarize/stream",
            json={"text": "Test text that will timeout"}
        )
        assert resp.status_code == 200  # SSE returns 200 even with errors
        assert resp.headers["content-type"] == "text/event-stream; charset=utf-8"
        
        # Parse SSE response
        lines = resp.text.strip().split('\n')
        data_lines = [line for line in lines if line.startswith('data: ')]
        
        assert len(data_lines) == 1
        error_chunk = json.loads(data_lines[0][6:])  # Remove 'data: ' prefix
        assert error_chunk["done"] is True
        assert "timeout" in error_chunk["error"].lower()


@pytest.mark.integration
def test_summarize_stream_endpoint_http_error():
    """Test that HTTP errors in streaming return proper error."""
    import httpx
    
    http_error = httpx.HTTPStatusError("Bad Request", request=MagicMock(), response=MagicMock())
    
    class MockStreamClient:
        async def __aenter__(self):
            return self
        
        async def __aexit__(self, exc_type, exc, tb):
            return False
        
        def stream(self, method, url, **kwargs):
            raise http_error
    
    with patch('httpx.AsyncClient', return_value=MockStreamClient()):
        resp = client.post(
            "/api/v1/summarize/stream",
            json={"text": "Test text"}
        )
        assert resp.status_code == 200  # SSE returns 200 even with errors
        assert resp.headers["content-type"] == "text/event-stream; charset=utf-8"
        
        # Parse SSE response
        lines = resp.text.strip().split('\n')
        data_lines = [line for line in lines if line.startswith('data: ')]
        
        assert len(data_lines) == 1
        error_chunk = json.loads(data_lines[0][6:])  # Remove 'data: ' prefix
        assert error_chunk["done"] is True
        assert "Summarization failed" in error_chunk["error"]


@pytest.mark.integration
def test_summarize_stream_endpoint_sse_format():
    """Test that streaming endpoint returns proper SSE format."""
    mock_stream_data = ['{"response": "Summary", "done": true, "eval_count": 1}\n']
    
    class MockStreamResponse:
        def __init__(self, data):
            self.data = data
        
        async def aiter_lines(self):
            for line in self.data:
                yield line
        
        def raise_for_status(self):
            pass
    
    class MockStreamContextManager:
        def __init__(self, response):
            self.response = response
        
        async def __aenter__(self):
            return self.response
        
        async def __aexit__(self, exc_type, exc, tb):
            return False
    
    class MockStreamClient:
        async def __aenter__(self):
            return self
        
        async def __aexit__(self, exc_type, exc, tb):
            return False
        
        def stream(self, method, url, **kwargs):
            return MockStreamContextManager(MockStreamResponse(mock_stream_data))
    
    with patch('httpx.AsyncClient', return_value=MockStreamClient()):
        resp = client.post(
            "/api/v1/summarize/stream",
            json={"text": "Test text"}
        )
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/event-stream; charset=utf-8"
        assert resp.headers["cache-control"] == "no-cache"
        assert resp.headers["connection"] == "keep-alive"
        
        # Check SSE format
        lines = resp.text.strip().split('\n')
        assert any(line.startswith('data: ') for line in lines)