openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.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

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