-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_queue.py
More file actions
414 lines (317 loc) · 13.3 KB
/
test_queue.py
File metadata and controls
414 lines (317 loc) · 13.3 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
Tests for the queue API.
Note: queue API only supports video models.
Image models must use the process API.
"""
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from decart import DecartClient, models, DecartSDKError
@pytest.mark.asyncio
async def test_queue_submit_text_to_video() -> None:
"""Test text-to-video submission with queue API."""
client = DecartClient(api_key="test-key")
with patch("decart.queue.client.submit_job") as mock_submit:
mock_submit.return_value = MagicMock(job_id="job-123", status="pending")
job = await client.queue.submit(
{
"model": models.video("lucy-pro-t2v"),
"prompt": "A cat walking in a park",
"seed": 42,
}
)
assert job.job_id == "job-123"
assert job.status == "pending"
mock_submit.assert_called_once()
@pytest.mark.asyncio
async def test_queue_submit_video_to_video() -> None:
"""Test video-to-video submission with queue API."""
client = DecartClient(api_key="test-key")
with patch("decart.queue.client.submit_job") as mock_submit:
mock_submit.return_value = MagicMock(job_id="job-456", status="pending")
job = await client.queue.submit(
{
"model": models.video("lucy-pro-v2v"),
"prompt": "Anime style",
"data": b"fake video data",
"enhance_prompt": True,
}
)
assert job.job_id == "job-456"
assert job.status == "pending"
@pytest.mark.asyncio
async def test_queue_rejects_image_models() -> None:
"""Test that queue API rejects image models with helpful error message."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError) as exc_info:
await client.queue.submit(
{
"model": models.image("lucy-pro-t2i"),
"prompt": "A beautiful sunset",
}
)
assert "not supported by queue" in str(exc_info.value)
assert "process" in str(exc_info.value).lower()
@pytest.mark.asyncio
async def test_queue_missing_model() -> None:
"""Test that missing model raises an error."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError):
await client.queue.submit(
{
"prompt": "A cat walking",
}
)
@pytest.mark.asyncio
async def test_queue_status() -> None:
"""Test getting job status."""
client = DecartClient(api_key="test-key")
with patch("decart.queue.client.get_job_status") as mock_status:
mock_status.return_value = MagicMock(job_id="job-123", status="processing")
status = await client.queue.status("job-123")
assert status.job_id == "job-123"
assert status.status == "processing"
mock_status.assert_called_once()
@pytest.mark.asyncio
async def test_queue_result() -> None:
"""Test getting job result."""
client = DecartClient(api_key="test-key")
with patch("decart.queue.client.get_job_content") as mock_content:
mock_content.return_value = b"fake video content"
result = await client.queue.result("job-123")
assert result == b"fake video content"
mock_content.assert_called_once()
@pytest.mark.asyncio
async def test_queue_submit_and_poll_completed() -> None:
"""Test submit_and_poll returns completed result."""
client = DecartClient(api_key="test-key")
with (
patch("decart.queue.client.submit_job") as mock_submit,
patch("decart.queue.client.get_job_status") as mock_status,
patch("decart.queue.client.get_job_content") as mock_content,
patch("asyncio.sleep", new_callable=AsyncMock),
):
mock_submit.return_value = MagicMock(job_id="job-123", status="pending")
mock_status.return_value = MagicMock(job_id="job-123", status="completed")
mock_content.return_value = b"fake video data"
result = await client.queue.submit_and_poll(
{
"model": models.video("lucy-pro-t2v"),
"prompt": "A serene lake",
}
)
assert result.status == "completed"
assert result.data == b"fake video data"
@pytest.mark.asyncio
async def test_queue_submit_and_poll_failed() -> None:
"""Test submit_and_poll returns failed result."""
client = DecartClient(api_key="test-key")
with (
patch("decart.queue.client.submit_job") as mock_submit,
patch("decart.queue.client.get_job_status") as mock_status,
patch("asyncio.sleep", new_callable=AsyncMock),
):
mock_submit.return_value = MagicMock(job_id="job-123", status="pending")
mock_status.return_value = MagicMock(job_id="job-123", status="failed")
result = await client.queue.submit_and_poll(
{
"model": models.video("lucy-pro-t2v"),
"prompt": "A serene lake",
}
)
assert result.status == "failed"
assert result.error == "Job failed"
@pytest.mark.asyncio
async def test_queue_submit_and_poll_with_callback() -> None:
"""Test submit_and_poll calls on_status_change callback."""
client = DecartClient(api_key="test-key")
status_changes: list[str] = []
def on_status_change(job):
status_changes.append(job.status)
with (
patch("decart.queue.client.submit_job") as mock_submit,
patch("decart.queue.client.get_job_status") as mock_status,
patch("decart.queue.client.get_job_content") as mock_content,
patch("asyncio.sleep", new_callable=AsyncMock),
):
mock_submit.return_value = MagicMock(job_id="job-123", status="pending")
mock_status.side_effect = [
MagicMock(job_id="job-123", status="processing"),
MagicMock(job_id="job-123", status="completed"),
]
mock_content.return_value = b"fake video data"
await client.queue.submit_and_poll(
{
"model": models.video("lucy-pro-t2v"),
"prompt": "A serene lake",
"on_status_change": on_status_change,
}
)
assert "pending" in status_changes
assert "processing" in status_changes
assert "completed" in status_changes
@pytest.mark.asyncio
async def test_queue_submit_missing_required_field() -> None:
"""Test that missing required fields raise an error."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError):
await client.queue.submit(
{
"model": models.video("lucy-pro-v2v"),
# Missing 'prompt' and 'data' which are required for v2v
}
)
@pytest.mark.asyncio
async def test_queue_submit_max_prompt_length() -> None:
"""Test that prompt length validation works."""
client = DecartClient(api_key="test-key")
prompt = "a" * 1001
with pytest.raises(DecartSDKError) as exception:
await client.queue.submit(
{
"model": models.video("lucy-pro-t2v"),
"prompt": prompt,
}
)
assert "Invalid inputs for lucy-pro-t2v" in str(exception)
@pytest.mark.asyncio
async def test_queue_includes_user_agent_header() -> None:
"""Test that User-Agent header is included in queue requests."""
client = DecartClient(api_key="test-key")
with patch("aiohttp.ClientSession") as mock_session_cls:
mock_response = MagicMock()
mock_response.ok = True
mock_response.json = AsyncMock(return_value={"job_id": "job-123", "status": "pending"})
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.queue.submit(
{
"model": models.video("lucy-pro-t2v"),
"prompt": "Test prompt",
}
)
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/")
# Tests for lucy-restyle-v2v with reference_image
@pytest.mark.asyncio
async def test_queue_restyle_with_prompt() -> None:
"""Test lucy-restyle-v2v submission with text prompt."""
client = DecartClient(api_key="test-key")
with patch("decart.queue.client.submit_job") as mock_submit:
mock_submit.return_value = MagicMock(job_id="job-789", status="pending")
job = await client.queue.submit(
{
"model": models.video("lucy-restyle-v2v"),
"prompt": "Make it look like anime",
"data": b"fake video data",
"enhance_prompt": True,
}
)
assert job.job_id == "job-789"
assert job.status == "pending"
mock_submit.assert_called_once()
@pytest.mark.asyncio
async def test_queue_restyle_with_reference_image() -> None:
"""Test lucy-restyle-v2v submission with reference image."""
client = DecartClient(api_key="test-key")
with patch("decart.queue.client.submit_job") as mock_submit:
mock_submit.return_value = MagicMock(job_id="job-890", status="pending")
job = await client.queue.submit(
{
"model": models.video("lucy-restyle-v2v"),
"reference_image": b"fake image data",
"data": b"fake video data",
}
)
assert job.job_id == "job-890"
assert job.status == "pending"
mock_submit.assert_called_once()
@pytest.mark.asyncio
async def test_queue_restyle_rejects_both_prompt_and_reference_image() -> None:
"""Test that lucy-restyle-v2v rejects both prompt and reference_image."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError) as exc_info:
await client.queue.submit(
{
"model": models.video("lucy-restyle-v2v"),
"prompt": "Make it anime",
"reference_image": b"fake image data",
"data": b"fake video data",
}
)
assert "either 'prompt' or 'reference_image'" in str(exc_info.value).lower()
@pytest.mark.asyncio
async def test_queue_restyle_rejects_neither_prompt_nor_reference_image() -> None:
"""Test that lucy-restyle-v2v rejects when neither prompt nor reference_image provided."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError) as exc_info:
await client.queue.submit(
{
"model": models.video("lucy-restyle-v2v"),
"data": b"fake video data",
}
)
assert "either 'prompt' or 'reference_image'" in str(exc_info.value).lower()
@pytest.mark.asyncio
async def test_queue_restyle_rejects_enhance_prompt_with_reference_image() -> None:
"""Test that enhance_prompt is only valid with text prompt, not reference_image."""
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError) as exc_info:
await client.queue.submit(
{
"model": models.video("lucy-restyle-v2v"),
"reference_image": b"fake image data",
"data": b"fake video data",
"enhance_prompt": True,
}
)
assert "enhance_prompt" in str(exc_info.value).lower()
@pytest.mark.asyncio
async def test_queue_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.queue.submit(
{
"model": models.video("lucy-pro-v2v"),
"prompt": "test",
"data": large_data,
}
)
@pytest.mark.asyncio
async def test_queue_accepts_over_20mb_for_restyle() -> None:
"""Test that lucy-restyle-v2v accepts files over 20MB (up to 100MB)."""
client = DecartClient(api_key="test-key")
data_50mb = b"x" * (50 * 1024 * 1024)
with patch("decart.queue.client.submit_job") as mock_submit:
mock_submit.return_value = MagicMock(job_id="job-restyle", status="pending")
job = await client.queue.submit(
{
"model": models.video("lucy-restyle-v2v"),
"prompt": "Restyle this",
"data": data_50mb,
}
)
assert job.job_id == "job-restyle"
mock_submit.assert_called_once()
@pytest.mark.asyncio
async def test_queue_rejects_file_exceeding_100mb_for_restyle() -> None:
"""Test that lucy-restyle-v2v rejects files over 100MB."""
client = DecartClient(api_key="test-key")
data_101mb = b"x" * (101 * 1024 * 1024)
with pytest.raises(DecartSDKError, match="exceeds the maximum allowed size of 100MB"):
await client.queue.submit(
{
"model": models.video("lucy-restyle-v2v"),
"prompt": "Restyle this",
"data": data_101mb,
}
)