openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.13.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 · modeblame

baa9f07fRobert Craigie2 years ago1# File generated from our OpenAPI spec by Stainless.
2
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
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:
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)
28assert_matches_type(ThreadMessage, message, path=["response"])
29
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)
39assert_matches_type(ThreadMessage, message, path=["response"])
40
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()
52assert_matches_type(ThreadMessage, message, path=["response"])
53
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()
65assert_matches_type(ThreadMessage, message, path=["response"])
66
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)
84assert_matches_type(ThreadMessage, message, path=["response"])
85
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()
96assert_matches_type(ThreadMessage, message, path=["response"])
97
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()
108assert_matches_type(ThreadMessage, message, path=["response"])
109
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)
132assert_matches_type(ThreadMessage, message, path=["response"])
133
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)
141assert_matches_type(ThreadMessage, message, path=["response"])
142
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()
153assert_matches_type(ThreadMessage, message, path=["response"])
154
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()
165assert_matches_type(ThreadMessage, message, path=["response"])
166
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)
188assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
189
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",
198)
199assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
200
201@parametrize
202def test_raw_response_list(self, client: OpenAI) -> None:
203response = client.beta.threads.messages.with_raw_response.list(
204"string",
205)
86379b44Stainless Bot2 years ago206
207assert response.is_closed is True
baa9f07fRobert Craigie2 years ago208assert response.http_request.headers.get("X-Stainless-Lang") == "python"
209message = response.parse()
210assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
211
86379b44Stainless Bot2 years ago212@parametrize
213def test_streaming_response_list(self, client: OpenAI) -> None:
214with client.beta.threads.messages.with_streaming_response.list(
215"string",
216) as response:
217assert not response.is_closed
218assert response.http_request.headers.get("X-Stainless-Lang") == "python"
219
220message = response.parse()
221assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"])
222
223assert cast(Any, response.is_closed) is True
224
023a4e66Stainless Bot2 years ago225@parametrize
226def test_path_params_list(self, client: OpenAI) -> None:
227with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
228client.beta.threads.messages.with_raw_response.list(
229"",
230)
231
baa9f07fRobert Craigie2 years ago232
233class TestAsyncMessages:
98d779fbStainless Bot2 years ago234parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago235
236@parametrize
98d779fbStainless Bot2 years ago237async def test_method_create(self, async_client: AsyncOpenAI) -> None:
238message = await async_client.beta.threads.messages.create(
baa9f07fRobert Craigie2 years ago239"string",
240content="x",
241role="user",
242)
243assert_matches_type(ThreadMessage, message, path=["response"])
244
245@parametrize
98d779fbStainless Bot2 years ago246async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
247message = await async_client.beta.threads.messages.create(
baa9f07fRobert Craigie2 years ago248"string",
249content="x",
250role="user",
251file_ids=["string"],
252metadata={},
253)
254assert_matches_type(ThreadMessage, message, path=["response"])
255
256@parametrize
98d779fbStainless Bot2 years ago257async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
258response = await async_client.beta.threads.messages.with_raw_response.create(
baa9f07fRobert Craigie2 years ago259"string",
260content="x",
261role="user",
262)
86379b44Stainless Bot2 years ago263
264assert response.is_closed is True
baa9f07fRobert Craigie2 years ago265assert response.http_request.headers.get("X-Stainless-Lang") == "python"
266message = response.parse()
267assert_matches_type(ThreadMessage, message, path=["response"])
268
86379b44Stainless Bot2 years ago269@parametrize
98d779fbStainless Bot2 years ago270async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
271async with async_client.beta.threads.messages.with_streaming_response.create(
86379b44Stainless Bot2 years ago272"string",
273content="x",
274role="user",
275) as response:
276assert not response.is_closed
277assert response.http_request.headers.get("X-Stainless-Lang") == "python"
278
279message = await response.parse()
280assert_matches_type(ThreadMessage, message, path=["response"])
281
282assert cast(Any, response.is_closed) is True
283
023a4e66Stainless Bot2 years ago284@parametrize
98d779fbStainless Bot2 years ago285async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago286with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago287await async_client.beta.threads.messages.with_raw_response.create(
023a4e66Stainless Bot2 years ago288"",
289content="x",
290role="user",
291)
292
baa9f07fRobert Craigie2 years ago293@parametrize
98d779fbStainless Bot2 years ago294async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
295message = await async_client.beta.threads.messages.retrieve(
baa9f07fRobert Craigie2 years ago296"string",
297thread_id="string",
298)
299assert_matches_type(ThreadMessage, message, path=["response"])
300
301@parametrize
98d779fbStainless Bot2 years ago302async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
303response = await async_client.beta.threads.messages.with_raw_response.retrieve(
baa9f07fRobert Craigie2 years ago304"string",
305thread_id="string",
306)
86379b44Stainless Bot2 years ago307
308assert response.is_closed is True
baa9f07fRobert Craigie2 years ago309assert response.http_request.headers.get("X-Stainless-Lang") == "python"
310message = response.parse()
311assert_matches_type(ThreadMessage, message, path=["response"])
312
86379b44Stainless Bot2 years ago313@parametrize
98d779fbStainless Bot2 years ago314async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
315async with async_client.beta.threads.messages.with_streaming_response.retrieve(
86379b44Stainless Bot2 years ago316"string",
317thread_id="string",
318) as response:
319assert not response.is_closed
320assert response.http_request.headers.get("X-Stainless-Lang") == "python"
321
322message = await response.parse()
323assert_matches_type(ThreadMessage, message, path=["response"])
324
325assert cast(Any, response.is_closed) is True
326
023a4e66Stainless Bot2 years ago327@parametrize
98d779fbStainless Bot2 years ago328async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago329with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago330await async_client.beta.threads.messages.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago331"string",
332thread_id="",
333)
334
335with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
98d779fbStainless Bot2 years ago336await async_client.beta.threads.messages.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago337"",
338thread_id="string",
339)
340
baa9f07fRobert Craigie2 years ago341@parametrize
98d779fbStainless Bot2 years ago342async def test_method_update(self, async_client: AsyncOpenAI) -> None:
343message = await async_client.beta.threads.messages.update(
baa9f07fRobert Craigie2 years ago344"string",
345thread_id="string",
346)
347assert_matches_type(ThreadMessage, message, path=["response"])
348
349@parametrize
98d779fbStainless Bot2 years ago350async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
351message = await async_client.beta.threads.messages.update(
baa9f07fRobert Craigie2 years ago352"string",
353thread_id="string",
354metadata={},
355)
356assert_matches_type(ThreadMessage, message, path=["response"])
357
358@parametrize
98d779fbStainless Bot2 years ago359async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
360response = await async_client.beta.threads.messages.with_raw_response.update(
baa9f07fRobert Craigie2 years ago361"string",
362thread_id="string",
363)
86379b44Stainless Bot2 years ago364
365assert response.is_closed is True
baa9f07fRobert Craigie2 years ago366assert response.http_request.headers.get("X-Stainless-Lang") == "python"
367message = response.parse()
368assert_matches_type(ThreadMessage, message, path=["response"])
369
86379b44Stainless Bot2 years ago370@parametrize
98d779fbStainless Bot2 years ago371async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
372async with async_client.beta.threads.messages.with_streaming_response.update(
86379b44Stainless Bot2 years ago373"string",
374thread_id="string",
375) as response:
376assert not response.is_closed
377assert response.http_request.headers.get("X-Stainless-Lang") == "python"
378
379message = await response.parse()
380assert_matches_type(ThreadMessage, message, path=["response"])
381
382assert cast(Any, response.is_closed) is True
383
023a4e66Stainless Bot2 years ago384@parametrize
98d779fbStainless Bot2 years ago385async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago386with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago387await async_client.beta.threads.messages.with_raw_response.update(
023a4e66Stainless Bot2 years ago388"string",
389thread_id="",
390)
391
392with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
98d779fbStainless Bot2 years ago393await async_client.beta.threads.messages.with_raw_response.update(
023a4e66Stainless Bot2 years ago394"",
395thread_id="string",
396)
397
baa9f07fRobert Craigie2 years ago398@parametrize
98d779fbStainless Bot2 years ago399async def test_method_list(self, async_client: AsyncOpenAI) -> None:
400message = await async_client.beta.threads.messages.list(
baa9f07fRobert Craigie2 years ago401"string",
402)
403assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
404
405@parametrize
98d779fbStainless Bot2 years ago406async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
407message = await async_client.beta.threads.messages.list(
baa9f07fRobert Craigie2 years ago408"string",
409after="string",
410before="string",
411limit=0,
412order="asc",
413)
414assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
415
416@parametrize
98d779fbStainless Bot2 years ago417async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
418response = await async_client.beta.threads.messages.with_raw_response.list(
baa9f07fRobert Craigie2 years ago419"string",
420)
86379b44Stainless Bot2 years ago421
422assert response.is_closed is True
baa9f07fRobert Craigie2 years ago423assert response.http_request.headers.get("X-Stainless-Lang") == "python"
424message = response.parse()
425assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
86379b44Stainless Bot2 years ago426
427@parametrize
98d779fbStainless Bot2 years ago428async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
429async with async_client.beta.threads.messages.with_streaming_response.list(
86379b44Stainless Bot2 years ago430"string",
431) as response:
432assert not response.is_closed
433assert response.http_request.headers.get("X-Stainless-Lang") == "python"
434
435message = await response.parse()
436assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"])
437
438assert cast(Any, response.is_closed) is True
023a4e66Stainless Bot2 years ago439
440@parametrize
98d779fbStainless Bot2 years ago441async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago442with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago443await async_client.beta.threads.messages.with_raw_response.list(
023a4e66Stainless Bot2 years ago444"",
445)