-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_process.py
More file actions
281 lines (222 loc) · 9.54 KB
/
test_process.py
File metadata and controls
281 lines (222 loc) · 9.54 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
import pytest
import asyncio
from unittest.mock import AsyncMock, patch, MagicMock
from decart import DecartClient, models, DecartSDKError
from decart.types import MotionTrajectoryInput
@pytest.mark.asyncio
async def test_process_text_to_video() -> None:
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 video 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.video("lucy-pro-t2v"),
"prompt": "A cat walking",
}
)
assert result == b"fake video data"
@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:
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError):
await client.process(
{
"model": models.video("lucy-pro-i2v"),
}
)
@pytest.mark.asyncio
async def test_process_video_to_video() -> None:
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 video 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.video("lucy-pro-v2v"),
"prompt": "Anime style",
"data": b"fake input video",
"enhance_prompt": True,
}
)
assert result == b"fake video data"
@pytest.mark.asyncio
async def test_process_video_to_video_fast() -> None:
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 video 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.video("lucy-fast-v2v"),
"prompt": "Change the car to a motorcycle",
"data": b"fake input video",
"resolution": "480p",
"enhance_prompt": True,
"num_inference_steps": 50,
"seed": 42,
}
)
assert result == b"fake video data"
@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_image_to_motion_video() -> None:
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 video 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.video("lucy-motion"),
"data": b"fake input image",
"trajectory": [
MotionTrajectoryInput(frame=0, x=0, y=0),
MotionTrajectoryInput(frame=1, x=0.5, y=0.5),
MotionTrajectoryInput(frame=2, x=1, y=1),
MotionTrajectoryInput(frame=3, x=1.5, y=1.5),
MotionTrajectoryInput(frame=4, x=2, y=2),
],
}
)
assert result == b"fake video data"
@pytest.mark.asyncio
async def test_process_image_to_motion_video_invalid_trajectory() -> None:
client = DecartClient(api_key="test-key")
with pytest.raises(DecartSDKError) as exception:
await client.process(
{
"model": models.video("lucy-motion"),
"data": b"fake input image",
"trajectory": [
MotionTrajectoryInput(frame=0, x=0, y=0),
],
}
)
assert "Invalid inputs for lucy-motion: 1 validation error for ImageToMotionVideoInput" in str(
exception
)
@pytest.mark.asyncio
async def test_process_with_cancellation() -> None:
client = DecartClient(api_key="test-key")
cancel_token = asyncio.Event()
cancel_token.set()
with pytest.raises(asyncio.CancelledError):
await client.process(
{
"model": models.video("lucy-pro-t2v"),
"prompt": "A video 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 video 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.video("lucy-pro-t2v"),
"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 video 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.video("lucy-pro-t2v"),
"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")