-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_process.py
More file actions
270 lines (211 loc) · 8.98 KB
/
test_process.py
File metadata and controls
270 lines (211 loc) · 8.98 KB
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
"""
Tests for the process API.
Note: process() only supports image models (t2i, i2i).
Video models must use the queue API.
"""
import pytest
import asyncio
from unittest.mock import AsyncMock, patch, MagicMock
from decart import DecartClient, models, DecartSDKError
@pytest.mark.asyncio
async def test_process_text_to_image() -> None:
"""Test text-to-image generation with process API."""
client = DecartClient(api_key="test-key")
with patch("aiohttp.ClientSession") as mock_session_cls:
mock_response = MagicMock()
mock_response.ok = True
mock_response.read = AsyncMock(return_value=b"fake image data")
mock_session = MagicMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=None)
mock_session.post = MagicMock()
mock_session.post.return_value.__aenter__ = AsyncMock(return_value=mock_response)
mock_session.post.return_value.__aexit__ = AsyncMock(return_value=None)
mock_session_cls.return_value = mock_session
result = await client.process(
{
"model": models.image("lucy-pro-t2i"),
"prompt": "A cat walking",
}
)
assert result == b"fake image data"
@pytest.mark.asyncio
async def test_process_image_to_image() -> None:
"""Test image-to-image transformation with process API."""
client = DecartClient(api_key="test-key")
with patch("aiohttp.ClientSession") as mock_session_cls:
mock_response = MagicMock()
mock_response.ok = True
mock_response.read = AsyncMock(return_value=b"fake image data")
mock_session = MagicMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=None)
mock_session.post = MagicMock()
mock_session.post.return_value.__aenter__ = AsyncMock(return_value=mock_response)
mock_session.post.return_value.__aexit__ = AsyncMock(return_value=None)
mock_session_cls.return_value = mock_session
result = await client.process(
{
"model": models.image("lucy-pro-i2i"),
"prompt": "Oil painting style",
"data": b"fake input image",
"enhance_prompt": True,
}
)
assert result == b"fake image data"
@pytest.mark.asyncio
async def test_process_rejects_video_models() -> None:
"""Test that process() rejects video models with helpful error message."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError) as exc_info:
await client.process(
{
"model": models.video("lucy-pro-t2v"),
"prompt": "A cat walking",
}
)
assert "not supported by process()" in str(exc_info.value)
assert "queue" in str(exc_info.value).lower()
@pytest.mark.asyncio
async def test_process_missing_model() -> None:
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError):
await client.process(
{
"prompt": "A cat walking",
}
)
@pytest.mark.asyncio
async def test_process_missing_required_field() -> None:
"""Test that missing required fields raise an error."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError):
await client.process(
{
"model": models.image("lucy-pro-i2i"),
# Missing 'data' field which is required for i2i
}
)
@pytest.mark.asyncio
async def test_process_max_prompt_length() -> None:
client = DecartClient(api_key="test-key")
prompt = "a" * 1001
with pytest.raises(DecartSDKError) as exception:
await client.process(
{
"model": models.image("lucy-pro-t2i"),
"prompt": prompt,
}
)
assert "Invalid inputs for lucy-pro-t2i: 1 validation error for TextToImageInput" in str(
exception
)
@pytest.mark.asyncio
async def test_process_with_cancellation() -> None:
"""Test that process() respects cancellation token."""
client = DecartClient(api_key="test-key")
cancel_token = asyncio.Event()
cancel_token.set()
with pytest.raises(asyncio.CancelledError):
await client.process(
{
"model": models.image("lucy-pro-t2i"),
"prompt": "An image that will be cancelled",
"cancel_token": cancel_token,
}
)
@pytest.mark.asyncio
async def test_process_includes_user_agent_header() -> None:
"""Test that User-Agent header is included in requests."""
client = DecartClient(api_key="test-key")
with patch("aiohttp.ClientSession") as mock_session_cls:
mock_response = MagicMock()
mock_response.ok = True
mock_response.read = AsyncMock(return_value=b"fake image data")
mock_session = MagicMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=None)
mock_session.post = MagicMock()
mock_session.post.return_value.__aenter__ = AsyncMock(return_value=mock_response)
mock_session.post.return_value.__aexit__ = AsyncMock(return_value=None)
mock_session_cls.return_value = mock_session
await client.process(
{
"model": models.image("lucy-pro-t2i"),
"prompt": "Test prompt",
}
)
# Verify post was called with User-Agent header
mock_session.post.assert_called_once()
call_kwargs = mock_session.post.call_args[1]
headers = call_kwargs.get("headers", {})
assert "User-Agent" in headers
assert headers["User-Agent"].startswith("decart-python-sdk/")
assert "lang/py" in headers["User-Agent"]
@pytest.mark.asyncio
async def test_process_includes_integration_in_user_agent() -> None:
"""Test that integration parameter is included in User-Agent header."""
client = DecartClient(api_key="test-key", integration="langchain/0.1.0")
with patch("aiohttp.ClientSession") as mock_session_cls:
mock_response = MagicMock()
mock_response.ok = True
mock_response.read = AsyncMock(return_value=b"fake image data")
mock_session = MagicMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=None)
mock_session.post = MagicMock()
mock_session.post.return_value.__aenter__ = AsyncMock(return_value=mock_response)
mock_session.post.return_value.__aexit__ = AsyncMock(return_value=None)
mock_session_cls.return_value = mock_session
await client.process(
{
"model": models.image("lucy-pro-t2i"),
"prompt": "Test prompt",
}
)
# Verify post was called with User-Agent header including integration
mock_session.post.assert_called_once()
call_kwargs = mock_session.post.call_args[1]
headers = call_kwargs.get("headers", {})
assert "User-Agent" in headers
assert headers["User-Agent"].startswith("decart-python-sdk/")
assert "lang/py" in headers["User-Agent"]
assert "langchain/0.1.0" in headers["User-Agent"]
assert headers["User-Agent"].endswith(" langchain/0.1.0")
@pytest.mark.asyncio
async def test_process_rejects_file_exceeding_20mb() -> None:
"""Test that files exceeding 20MB are rejected before upload."""
client = DecartClient(api_key="test-key")
large_data = b"x" * (21 * 1024 * 1024)
with pytest.raises(DecartSDKError, match="exceeds the maximum allowed size of 20MB"):
await client.process(
{
"model": models.image("lucy-pro-i2i"),
"prompt": "test",
"data": large_data,
}
)
@pytest.mark.asyncio
async def test_process_accepts_file_at_20mb_limit() -> None:
"""Test that files at exactly 20MB are accepted."""
client = DecartClient(api_key="test-key")
exact_data = b"x" * (20 * 1024 * 1024)
with patch("aiohttp.ClientSession") as mock_session_cls:
mock_response = MagicMock()
mock_response.ok = True
mock_response.read = AsyncMock(return_value=b"fake image data")
mock_session = MagicMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=None)
mock_session.post = MagicMock()
mock_session.post.return_value.__aenter__ = AsyncMock(return_value=mock_response)
mock_session.post.return_value.__aexit__ = AsyncMock(return_value=None)
mock_session_cls.return_value = mock_session
result = await client.process(
{
"model": models.image("lucy-pro-i2i"),
"prompt": "test",
"data": exact_data,
}
)
assert result == b"fake image data"