openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.11.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/beta/threads/test_messages.py

445lines · modecode

1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.pagination import SyncCursorPage, AsyncCursorPage
13from openai.types.beta.threads import ThreadMessage
14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16
17
18class TestMessages:
19 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
20
21 @parametrize
22 def test_method_create(self, client: OpenAI) -> None:
23 message = client.beta.threads.messages.create(
24 "string",
25 content="x",
26 role="user",
27 )
28 assert_matches_type(ThreadMessage, message, path=["response"])
29
30 @parametrize
31 def test_method_create_with_all_params(self, client: OpenAI) -> None:
32 message = client.beta.threads.messages.create(
33 "string",
34 content="x",
35 role="user",
36 file_ids=["string"],
37 metadata={},
38 )
39 assert_matches_type(ThreadMessage, message, path=["response"])
40
41 @parametrize
42 def test_raw_response_create(self, client: OpenAI) -> None:
43 response = client.beta.threads.messages.with_raw_response.create(
44 "string",
45 content="x",
46 role="user",
47 )
48
49 assert response.is_closed is True
50 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
51 message = response.parse()
52 assert_matches_type(ThreadMessage, message, path=["response"])
53
54 @parametrize
55 def test_streaming_response_create(self, client: OpenAI) -> None:
56 with client.beta.threads.messages.with_streaming_response.create(
57 "string",
58 content="x",
59 role="user",
60 ) as response:
61 assert not response.is_closed
62 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
63
64 message = response.parse()
65 assert_matches_type(ThreadMessage, message, path=["response"])
66
67 assert cast(Any, response.is_closed) is True
68
69 @parametrize
70 def test_path_params_create(self, client: OpenAI) -> None:
71 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
72 client.beta.threads.messages.with_raw_response.create(
73 "",
74 content="x",
75 role="user",
76 )
77
78 @parametrize
79 def test_method_retrieve(self, client: OpenAI) -> None:
80 message = client.beta.threads.messages.retrieve(
81 "string",
82 thread_id="string",
83 )
84 assert_matches_type(ThreadMessage, message, path=["response"])
85
86 @parametrize
87 def test_raw_response_retrieve(self, client: OpenAI) -> None:
88 response = client.beta.threads.messages.with_raw_response.retrieve(
89 "string",
90 thread_id="string",
91 )
92
93 assert response.is_closed is True
94 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
95 message = response.parse()
96 assert_matches_type(ThreadMessage, message, path=["response"])
97
98 @parametrize
99 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
100 with client.beta.threads.messages.with_streaming_response.retrieve(
101 "string",
102 thread_id="string",
103 ) as response:
104 assert not response.is_closed
105 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
106
107 message = response.parse()
108 assert_matches_type(ThreadMessage, message, path=["response"])
109
110 assert cast(Any, response.is_closed) is True
111
112 @parametrize
113 def test_path_params_retrieve(self, client: OpenAI) -> None:
114 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
115 client.beta.threads.messages.with_raw_response.retrieve(
116 "string",
117 thread_id="",
118 )
119
120 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
121 client.beta.threads.messages.with_raw_response.retrieve(
122 "",
123 thread_id="string",
124 )
125
126 @parametrize
127 def test_method_update(self, client: OpenAI) -> None:
128 message = client.beta.threads.messages.update(
129 "string",
130 thread_id="string",
131 )
132 assert_matches_type(ThreadMessage, message, path=["response"])
133
134 @parametrize
135 def test_method_update_with_all_params(self, client: OpenAI) -> None:
136 message = client.beta.threads.messages.update(
137 "string",
138 thread_id="string",
139 metadata={},
140 )
141 assert_matches_type(ThreadMessage, message, path=["response"])
142
143 @parametrize
144 def test_raw_response_update(self, client: OpenAI) -> None:
145 response = client.beta.threads.messages.with_raw_response.update(
146 "string",
147 thread_id="string",
148 )
149
150 assert response.is_closed is True
151 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
152 message = response.parse()
153 assert_matches_type(ThreadMessage, message, path=["response"])
154
155 @parametrize
156 def test_streaming_response_update(self, client: OpenAI) -> None:
157 with client.beta.threads.messages.with_streaming_response.update(
158 "string",
159 thread_id="string",
160 ) as response:
161 assert not response.is_closed
162 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
163
164 message = response.parse()
165 assert_matches_type(ThreadMessage, message, path=["response"])
166
167 assert cast(Any, response.is_closed) is True
168
169 @parametrize
170 def test_path_params_update(self, client: OpenAI) -> None:
171 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
172 client.beta.threads.messages.with_raw_response.update(
173 "string",
174 thread_id="",
175 )
176
177 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
178 client.beta.threads.messages.with_raw_response.update(
179 "",
180 thread_id="string",
181 )
182
183 @parametrize
184 def test_method_list(self, client: OpenAI) -> None:
185 message = client.beta.threads.messages.list(
186 "string",
187 )
188 assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
189
190 @parametrize
191 def test_method_list_with_all_params(self, client: OpenAI) -> None:
192 message = client.beta.threads.messages.list(
193 "string",
194 after="string",
195 before="string",
196 limit=0,
197 order="asc",
198 )
199 assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
200
201 @parametrize
202 def test_raw_response_list(self, client: OpenAI) -> None:
203 response = client.beta.threads.messages.with_raw_response.list(
204 "string",
205 )
206
207 assert response.is_closed is True
208 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
209 message = response.parse()
210 assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
211
212 @parametrize
213 def test_streaming_response_list(self, client: OpenAI) -> None:
214 with client.beta.threads.messages.with_streaming_response.list(
215 "string",
216 ) as response:
217 assert not response.is_closed
218 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
219
220 message = response.parse()
221 assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
222
223 assert cast(Any, response.is_closed) is True
224
225 @parametrize
226 def test_path_params_list(self, client: OpenAI) -> None:
227 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
228 client.beta.threads.messages.with_raw_response.list(
229 "",
230 )
231
232
233class TestAsyncMessages:
234 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
235
236 @parametrize
237 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
238 message = await async_client.beta.threads.messages.create(
239 "string",
240 content="x",
241 role="user",
242 )
243 assert_matches_type(ThreadMessage, message, path=["response"])
244
245 @parametrize
246 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
247 message = await async_client.beta.threads.messages.create(
248 "string",
249 content="x",
250 role="user",
251 file_ids=["string"],
252 metadata={},
253 )
254 assert_matches_type(ThreadMessage, message, path=["response"])
255
256 @parametrize
257 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
258 response = await async_client.beta.threads.messages.with_raw_response.create(
259 "string",
260 content="x",
261 role="user",
262 )
263
264 assert response.is_closed is True
265 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
266 message = response.parse()
267 assert_matches_type(ThreadMessage, message, path=["response"])
268
269 @parametrize
270 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
271 async with async_client.beta.threads.messages.with_streaming_response.create(
272 "string",
273 content="x",
274 role="user",
275 ) as response:
276 assert not response.is_closed
277 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
278
279 message = await response.parse()
280 assert_matches_type(ThreadMessage, message, path=["response"])
281
282 assert cast(Any, response.is_closed) is True
283
284 @parametrize
285 async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
286 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
287 await async_client.beta.threads.messages.with_raw_response.create(
288 "",
289 content="x",
290 role="user",
291 )
292
293 @parametrize
294 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
295 message = await async_client.beta.threads.messages.retrieve(
296 "string",
297 thread_id="string",
298 )
299 assert_matches_type(ThreadMessage, message, path=["response"])
300
301 @parametrize
302 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
303 response = await async_client.beta.threads.messages.with_raw_response.retrieve(
304 "string",
305 thread_id="string",
306 )
307
308 assert response.is_closed is True
309 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
310 message = response.parse()
311 assert_matches_type(ThreadMessage, message, path=["response"])
312
313 @parametrize
314 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
315 async with async_client.beta.threads.messages.with_streaming_response.retrieve(
316 "string",
317 thread_id="string",
318 ) as response:
319 assert not response.is_closed
320 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
321
322 message = await response.parse()
323 assert_matches_type(ThreadMessage, message, path=["response"])
324
325 assert cast(Any, response.is_closed) is True
326
327 @parametrize
328 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
329 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
330 await async_client.beta.threads.messages.with_raw_response.retrieve(
331 "string",
332 thread_id="",
333 )
334
335 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
336 await async_client.beta.threads.messages.with_raw_response.retrieve(
337 "",
338 thread_id="string",
339 )
340
341 @parametrize
342 async def test_method_update(self, async_client: AsyncOpenAI) -> None:
343 message = await async_client.beta.threads.messages.update(
344 "string",
345 thread_id="string",
346 )
347 assert_matches_type(ThreadMessage, message, path=["response"])
348
349 @parametrize
350 async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
351 message = await async_client.beta.threads.messages.update(
352 "string",
353 thread_id="string",
354 metadata={},
355 )
356 assert_matches_type(ThreadMessage, message, path=["response"])
357
358 @parametrize
359 async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
360 response = await async_client.beta.threads.messages.with_raw_response.update(
361 "string",
362 thread_id="string",
363 )
364
365 assert response.is_closed is True
366 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
367 message = response.parse()
368 assert_matches_type(ThreadMessage, message, path=["response"])
369
370 @parametrize
371 async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
372 async with async_client.beta.threads.messages.with_streaming_response.update(
373 "string",
374 thread_id="string",
375 ) as response:
376 assert not response.is_closed
377 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
378
379 message = await response.parse()
380 assert_matches_type(ThreadMessage, message, path=["response"])
381
382 assert cast(Any, response.is_closed) is True
383
384 @parametrize
385 async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
386 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
387 await async_client.beta.threads.messages.with_raw_response.update(
388 "string",
389 thread_id="",
390 )
391
392 with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
393 await async_client.beta.threads.messages.with_raw_response.update(
394 "",
395 thread_id="string",
396 )
397
398 @parametrize
399 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
400 message = await async_client.beta.threads.messages.list(
401 "string",
402 )
403 assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
404
405 @parametrize
406 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
407 message = await async_client.beta.threads.messages.list(
408 "string",
409 after="string",
410 before="string",
411 limit=0,
412 order="asc",
413 )
414 assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
415
416 @parametrize
417 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
418 response = await async_client.beta.threads.messages.with_raw_response.list(
419 "string",
420 )
421
422 assert response.is_closed is True
423 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
424 message = response.parse()
425 assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
426
427 @parametrize
428 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
429 async with async_client.beta.threads.messages.with_streaming_response.list(
430 "string",
431 ) as response:
432 assert not response.is_closed
433 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
434
435 message = await response.parse()
436 assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
437
438 assert cast(Any, response.is_closed) is True
439
440 @parametrize
441 async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
442 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
443 await async_client.beta.threads.messages.with_raw_response.list(
444 "",
445 )
446