-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_process.py
More file actions
228 lines (181 loc) · 7.87 KB
/
test_process.py
File metadata and controls
228 lines (181 loc) · 7.87 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
"""
Tests for the process API.
Note: process() only supports image models (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_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": "Apply an oil-painting treatment while preserving the composition",
"data": b"fake input image",
"enhance_prompt": True,
}
)
assert result == b"fake image data"
@pytest.mark.asyncio
async def test_process_image_to_image_with_reference_image() -> None:
"""Test image-to-image with optional reference_image."""
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": "Add the object from the reference image",
"data": b"fake input image",
"reference_image": b"fake reference image",
"enhance_prompt": False,
}
)
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-v2v"),
"prompt": "Add cinematic teal-and-orange grading",
}
)
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": "Apply an editorial color grade",
}
)
@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-i2i"),
"prompt": prompt,
"data": b"fake image data",
}
)
assert "Invalid inputs for lucy-pro-i2i: 1 validation error for ImageToImageInput" 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-i2i"),
"prompt": "Apply a high-contrast editorial treatment",
"data": b"fake image data",
"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-i2i"),
"prompt": "Apply a soft watercolor treatment",
"data": b"fake image data",
}
)
# 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-i2i"),
"prompt": "Apply a soft watercolor treatment",
"data": b"fake image data",
}
)
# 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")