openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.17.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/threads/test_messages.py

447lines · modeblame

5cfb125aStainless Bot2 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
baa9f07fRobert Craigie2 years ago2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
baa9f07fRobert Craigie2 years ago7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.pagination import SyncCursorPage, AsyncCursorPage
5429f696Stainless Bot2 years ago13from openai.types.beta.threads import Message
baa9f07fRobert Craigie2 years ago14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16
17
18class TestMessages:
98d779fbStainless Bot2 years ago19parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago20
21@parametrize
22def test_method_create(self, client: OpenAI) -> None:
23message = client.beta.threads.messages.create(
24"string",
25content="x",
26role="user",
27)
5429f696Stainless Bot2 years ago28assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago29
30@parametrize
31def test_method_create_with_all_params(self, client: OpenAI) -> None:
32message = client.beta.threads.messages.create(
33"string",
34content="x",
35role="user",
36file_ids=["string"],
37metadata={},
38)
5429f696Stainless Bot2 years ago39assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago40
41@parametrize
42def test_raw_response_create(self, client: OpenAI) -> None:
43response = client.beta.threads.messages.with_raw_response.create(
44"string",
45content="x",
46role="user",
47)
86379b44Stainless Bot2 years ago48
49assert response.is_closed is True
baa9f07fRobert Craigie2 years ago50assert response.http_request.headers.get("X-Stainless-Lang") == "python"
51message = response.parse()
5429f696Stainless Bot2 years ago52assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago53
86379b44Stainless Bot2 years ago54@parametrize
55def test_streaming_response_create(self, client: OpenAI) -> None:
56with client.beta.threads.messages.with_streaming_response.create(
57"string",
58content="x",
59role="user",
60) as response:
61assert not response.is_closed
62assert response.http_request.headers.get("X-Stainless-Lang") == "python"
63
64message = response.parse()
5429f696Stainless Bot2 years ago65assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago66
67assert cast(Any, response.is_closed) is True
68
023a4e66Stainless Bot2 years ago69@parametrize
70def test_path_params_create(self, client: OpenAI) -> None:
71with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
72client.beta.threads.messages.with_raw_response.create(
73"",
74content="x",
75role="user",
76)
77
baa9f07fRobert Craigie2 years ago78@parametrize
79def test_method_retrieve(self, client: OpenAI) -> None:
80message = client.beta.threads.messages.retrieve(
81"string",
82thread_id="string",
83)
5429f696Stainless Bot2 years ago84assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago85
86@parametrize
87def test_raw_response_retrieve(self, client: OpenAI) -> None:
88response = client.beta.threads.messages.with_raw_response.retrieve(
89"string",
90thread_id="string",
91)
86379b44Stainless Bot2 years ago92
93assert response.is_closed is True
baa9f07fRobert Craigie2 years ago94assert response.http_request.headers.get("X-Stainless-Lang") == "python"
95message = response.parse()
5429f696Stainless Bot2 years ago96assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago97
86379b44Stainless Bot2 years ago98@parametrize
99def test_streaming_response_retrieve(self, client: OpenAI) -> None:
100with client.beta.threads.messages.with_streaming_response.retrieve(
101"string",
102thread_id="string",
103) as response:
104assert not response.is_closed
105assert response.http_request.headers.get("X-Stainless-Lang") == "python"
106
107message = response.parse()
5429f696Stainless Bot2 years ago108assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago109
110assert cast(Any, response.is_closed) is True
111
023a4e66Stainless Bot2 years ago112@parametrize
113def test_path_params_retrieve(self, client: OpenAI) -> None:
114with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
115client.beta.threads.messages.with_raw_response.retrieve(
116"string",
117thread_id="",
118)
119
120with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
121client.beta.threads.messages.with_raw_response.retrieve(
122"",
123thread_id="string",
124)
125
baa9f07fRobert Craigie2 years ago126@parametrize
127def test_method_update(self, client: OpenAI) -> None:
128message = client.beta.threads.messages.update(
129"string",
130thread_id="string",
131)
5429f696Stainless Bot2 years ago132assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago133
134@parametrize
135def test_method_update_with_all_params(self, client: OpenAI) -> None:
136message = client.beta.threads.messages.update(
137"string",
138thread_id="string",
139metadata={},
140)
5429f696Stainless Bot2 years ago141assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago142
143@parametrize
144def test_raw_response_update(self, client: OpenAI) -> None:
145response = client.beta.threads.messages.with_raw_response.update(
146"string",
147thread_id="string",
148)
86379b44Stainless Bot2 years ago149
150assert response.is_closed is True
baa9f07fRobert Craigie2 years ago151assert response.http_request.headers.get("X-Stainless-Lang") == "python"
152message = response.parse()
5429f696Stainless Bot2 years ago153assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago154
86379b44Stainless Bot2 years ago155@parametrize
156def test_streaming_response_update(self, client: OpenAI) -> None:
157with client.beta.threads.messages.with_streaming_response.update(
158"string",
159thread_id="string",
160) as response:
161assert not response.is_closed
162assert response.http_request.headers.get("X-Stainless-Lang") == "python"
163
164message = response.parse()
5429f696Stainless Bot2 years ago165assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago166
167assert cast(Any, response.is_closed) is True
168
023a4e66Stainless Bot2 years ago169@parametrize
170def test_path_params_update(self, client: OpenAI) -> None:
171with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
172client.beta.threads.messages.with_raw_response.update(
173"string",
174thread_id="",
175)
176
177with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
178client.beta.threads.messages.with_raw_response.update(
179"",
180thread_id="string",
181)
182
baa9f07fRobert Craigie2 years ago183@parametrize
184def test_method_list(self, client: OpenAI) -> None:
185message = client.beta.threads.messages.list(
186"string",
187)
5429f696Stainless Bot2 years ago188assert_matches_type(SyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago189
190@parametrize
191def test_method_list_with_all_params(self, client: OpenAI) -> None:
192message = client.beta.threads.messages.list(
193"string",
194after="string",
195before="string",
196limit=0,
197order="asc",
8efca3a3Stainless Bot2 years ago198run_id="string",
baa9f07fRobert Craigie2 years ago199)
5429f696Stainless Bot2 years ago200assert_matches_type(SyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago201
202@parametrize
203def test_raw_response_list(self, client: OpenAI) -> None:
204response = client.beta.threads.messages.with_raw_response.list(
205"string",
206)
86379b44Stainless Bot2 years ago207
208assert response.is_closed is True
baa9f07fRobert Craigie2 years ago209assert response.http_request.headers.get("X-Stainless-Lang") == "python"
210message = response.parse()
5429f696Stainless Bot2 years ago211assert_matches_type(SyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago212
86379b44Stainless Bot2 years ago213@parametrize
214def test_streaming_response_list(self, client: OpenAI) -> None:
215with client.beta.threads.messages.with_streaming_response.list(
216"string",
217) as response:
218assert not response.is_closed
219assert response.http_request.headers.get("X-Stainless-Lang") == "python"
220
221message = response.parse()
5429f696Stainless Bot2 years ago222assert_matches_type(SyncCursorPage[Message], message, path=["response"])
86379b44Stainless Bot2 years ago223
224assert cast(Any, response.is_closed) is True
225
023a4e66Stainless Bot2 years ago226@parametrize
227def test_path_params_list(self, client: OpenAI) -> None:
228with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
229client.beta.threads.messages.with_raw_response.list(
230"",
231)
232
baa9f07fRobert Craigie2 years ago233
234class TestAsyncMessages:
98d779fbStainless Bot2 years ago235parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago236
237@parametrize
98d779fbStainless Bot2 years ago238async def test_method_create(self, async_client: AsyncOpenAI) -> None:
239message = await async_client.beta.threads.messages.create(
baa9f07fRobert Craigie2 years ago240"string",
241content="x",
242role="user",
243)
5429f696Stainless Bot2 years ago244assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago245
246@parametrize
98d779fbStainless Bot2 years ago247async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
248message = await async_client.beta.threads.messages.create(
baa9f07fRobert Craigie2 years ago249"string",
250content="x",
251role="user",
252file_ids=["string"],
253metadata={},
254)
5429f696Stainless Bot2 years ago255assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago256
257@parametrize
98d779fbStainless Bot2 years ago258async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
259response = await async_client.beta.threads.messages.with_raw_response.create(
baa9f07fRobert Craigie2 years ago260"string",
261content="x",
262role="user",
263)
86379b44Stainless Bot2 years ago264
265assert response.is_closed is True
baa9f07fRobert Craigie2 years ago266assert response.http_request.headers.get("X-Stainless-Lang") == "python"
267message = response.parse()
5429f696Stainless Bot2 years ago268assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago269
86379b44Stainless Bot2 years ago270@parametrize
98d779fbStainless Bot2 years ago271async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
272async with async_client.beta.threads.messages.with_streaming_response.create(
86379b44Stainless Bot2 years ago273"string",
274content="x",
275role="user",
276) as response:
277assert not response.is_closed
278assert response.http_request.headers.get("X-Stainless-Lang") == "python"
279
280message = await response.parse()
5429f696Stainless Bot2 years ago281assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago282
283assert cast(Any, response.is_closed) is True
284
023a4e66Stainless Bot2 years ago285@parametrize
98d779fbStainless Bot2 years ago286async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago287with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago288await async_client.beta.threads.messages.with_raw_response.create(
023a4e66Stainless Bot2 years ago289"",
290content="x",
291role="user",
292)
293
baa9f07fRobert Craigie2 years ago294@parametrize
98d779fbStainless Bot2 years ago295async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
296message = await async_client.beta.threads.messages.retrieve(
baa9f07fRobert Craigie2 years ago297"string",
298thread_id="string",
299)
5429f696Stainless Bot2 years ago300assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago301
302@parametrize
98d779fbStainless Bot2 years ago303async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
304response = await async_client.beta.threads.messages.with_raw_response.retrieve(
baa9f07fRobert Craigie2 years ago305"string",
306thread_id="string",
307)
86379b44Stainless Bot2 years ago308
309assert response.is_closed is True
baa9f07fRobert Craigie2 years ago310assert response.http_request.headers.get("X-Stainless-Lang") == "python"
311message = response.parse()
5429f696Stainless Bot2 years ago312assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago313
86379b44Stainless Bot2 years ago314@parametrize
98d779fbStainless Bot2 years ago315async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
316async with async_client.beta.threads.messages.with_streaming_response.retrieve(
86379b44Stainless Bot2 years ago317"string",
318thread_id="string",
319) as response:
320assert not response.is_closed
321assert response.http_request.headers.get("X-Stainless-Lang") == "python"
322
323message = await response.parse()
5429f696Stainless Bot2 years ago324assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago325
326assert cast(Any, response.is_closed) is True
327
023a4e66Stainless Bot2 years ago328@parametrize
98d779fbStainless Bot2 years ago329async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago330with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago331await async_client.beta.threads.messages.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago332"string",
333thread_id="",
334)
335
336with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
98d779fbStainless Bot2 years ago337await async_client.beta.threads.messages.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago338"",
339thread_id="string",
340)
341
baa9f07fRobert Craigie2 years ago342@parametrize
98d779fbStainless Bot2 years ago343async def test_method_update(self, async_client: AsyncOpenAI) -> None:
344message = await async_client.beta.threads.messages.update(
baa9f07fRobert Craigie2 years ago345"string",
346thread_id="string",
347)
5429f696Stainless Bot2 years ago348assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago349
350@parametrize
98d779fbStainless Bot2 years ago351async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
352message = await async_client.beta.threads.messages.update(
baa9f07fRobert Craigie2 years ago353"string",
354thread_id="string",
355metadata={},
356)
5429f696Stainless Bot2 years ago357assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago358
359@parametrize
98d779fbStainless Bot2 years ago360async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
361response = await async_client.beta.threads.messages.with_raw_response.update(
baa9f07fRobert Craigie2 years ago362"string",
363thread_id="string",
364)
86379b44Stainless Bot2 years ago365
366assert response.is_closed is True
baa9f07fRobert Craigie2 years ago367assert response.http_request.headers.get("X-Stainless-Lang") == "python"
368message = response.parse()
5429f696Stainless Bot2 years ago369assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago370
86379b44Stainless Bot2 years ago371@parametrize
98d779fbStainless Bot2 years ago372async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
373async with async_client.beta.threads.messages.with_streaming_response.update(
86379b44Stainless Bot2 years ago374"string",
375thread_id="string",
376) as response:
377assert not response.is_closed
378assert response.http_request.headers.get("X-Stainless-Lang") == "python"
379
380message = await response.parse()
5429f696Stainless Bot2 years ago381assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago382
383assert cast(Any, response.is_closed) is True
384
023a4e66Stainless Bot2 years ago385@parametrize
98d779fbStainless Bot2 years ago386async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago387with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago388await async_client.beta.threads.messages.with_raw_response.update(
023a4e66Stainless Bot2 years ago389"string",
390thread_id="",
391)
392
393with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
98d779fbStainless Bot2 years ago394await async_client.beta.threads.messages.with_raw_response.update(
023a4e66Stainless Bot2 years ago395"",
396thread_id="string",
397)
398
baa9f07fRobert Craigie2 years ago399@parametrize
98d779fbStainless Bot2 years ago400async def test_method_list(self, async_client: AsyncOpenAI) -> None:
401message = await async_client.beta.threads.messages.list(
baa9f07fRobert Craigie2 years ago402"string",
403)
5429f696Stainless Bot2 years ago404assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago405
406@parametrize
98d779fbStainless Bot2 years ago407async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
408message = await async_client.beta.threads.messages.list(
baa9f07fRobert Craigie2 years ago409"string",
410after="string",
411before="string",
412limit=0,
413order="asc",
8efca3a3Stainless Bot2 years ago414run_id="string",
baa9f07fRobert Craigie2 years ago415)
5429f696Stainless Bot2 years ago416assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago417
418@parametrize
98d779fbStainless Bot2 years ago419async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
420response = await async_client.beta.threads.messages.with_raw_response.list(
baa9f07fRobert Craigie2 years ago421"string",
422)
86379b44Stainless Bot2 years ago423
424assert response.is_closed is True
baa9f07fRobert Craigie2 years ago425assert response.http_request.headers.get("X-Stainless-Lang") == "python"
426message = response.parse()
5429f696Stainless Bot2 years ago427assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
86379b44Stainless Bot2 years ago428
429@parametrize
98d779fbStainless Bot2 years ago430async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
431async with async_client.beta.threads.messages.with_streaming_response.list(
86379b44Stainless Bot2 years ago432"string",
433) as response:
434assert not response.is_closed
435assert response.http_request.headers.get("X-Stainless-Lang") == "python"
436
437message = await response.parse()
5429f696Stainless Bot2 years ago438assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
86379b44Stainless Bot2 years ago439
440assert cast(Any, response.is_closed) is True
023a4e66Stainless Bot2 years ago441
442@parametrize
98d779fbStainless Bot2 years ago443async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago444with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago445await async_client.beta.threads.messages.with_raw_response.list(
023a4e66Stainless Bot2 years ago446"",
447)