main
1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
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 SyncConversationCursorPage, AsyncConversationCursorPage
13from openai.types.beta.chatkit import ChatKitThread, ThreadDeleteResponse
14from openai.types.beta.chatkit.chatkit_thread_item_list import Data
15
16base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
17
18
19class TestThreads:
20 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
21
22 @parametrize
23 def test_method_retrieve(self, client: OpenAI) -> None:
24 thread = client.beta.chatkit.threads.retrieve(
25 "cthr_123",
26 )
27 assert_matches_type(ChatKitThread, thread, path=["response"])
28
29 @parametrize
30 def test_raw_response_retrieve(self, client: OpenAI) -> None:
31 response = client.beta.chatkit.threads.with_raw_response.retrieve(
32 "cthr_123",
33 )
34
35 assert response.is_closed is True
36 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
37 thread = response.parse()
38 assert_matches_type(ChatKitThread, thread, path=["response"])
39
40 @parametrize
41 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
42 with client.beta.chatkit.threads.with_streaming_response.retrieve(
43 "cthr_123",
44 ) as response:
45 assert not response.is_closed
46 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
47
48 thread = response.parse()
49 assert_matches_type(ChatKitThread, thread, path=["response"])
50
51 assert cast(Any, response.is_closed) is True
52
53 @parametrize
54 def test_path_params_retrieve(self, client: OpenAI) -> None:
55 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
56 client.beta.chatkit.threads.with_raw_response.retrieve(
57 "",
58 )
59
60 @parametrize
61 def test_method_list(self, client: OpenAI) -> None:
62 thread = client.beta.chatkit.threads.list()
63 assert_matches_type(SyncConversationCursorPage[ChatKitThread], thread, path=["response"])
64
65 @parametrize
66 def test_method_list_with_all_params(self, client: OpenAI) -> None:
67 thread = client.beta.chatkit.threads.list(
68 after="after",
69 before="before",
70 limit=0,
71 order="asc",
72 user="x",
73 )
74 assert_matches_type(SyncConversationCursorPage[ChatKitThread], thread, path=["response"])
75
76 @parametrize
77 def test_raw_response_list(self, client: OpenAI) -> None:
78 response = client.beta.chatkit.threads.with_raw_response.list()
79
80 assert response.is_closed is True
81 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
82 thread = response.parse()
83 assert_matches_type(SyncConversationCursorPage[ChatKitThread], thread, path=["response"])
84
85 @parametrize
86 def test_streaming_response_list(self, client: OpenAI) -> None:
87 with client.beta.chatkit.threads.with_streaming_response.list() as response:
88 assert not response.is_closed
89 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
90
91 thread = response.parse()
92 assert_matches_type(SyncConversationCursorPage[ChatKitThread], thread, path=["response"])
93
94 assert cast(Any, response.is_closed) is True
95
96 @parametrize
97 def test_method_delete(self, client: OpenAI) -> None:
98 thread = client.beta.chatkit.threads.delete(
99 "cthr_123",
100 )
101 assert_matches_type(ThreadDeleteResponse, thread, path=["response"])
102
103 @parametrize
104 def test_raw_response_delete(self, client: OpenAI) -> None:
105 response = client.beta.chatkit.threads.with_raw_response.delete(
106 "cthr_123",
107 )
108
109 assert response.is_closed is True
110 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
111 thread = response.parse()
112 assert_matches_type(ThreadDeleteResponse, thread, path=["response"])
113
114 @parametrize
115 def test_streaming_response_delete(self, client: OpenAI) -> None:
116 with client.beta.chatkit.threads.with_streaming_response.delete(
117 "cthr_123",
118 ) as response:
119 assert not response.is_closed
120 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
121
122 thread = response.parse()
123 assert_matches_type(ThreadDeleteResponse, thread, path=["response"])
124
125 assert cast(Any, response.is_closed) is True
126
127 @parametrize
128 def test_path_params_delete(self, client: OpenAI) -> None:
129 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
130 client.beta.chatkit.threads.with_raw_response.delete(
131 "",
132 )
133
134 @parametrize
135 def test_method_list_items(self, client: OpenAI) -> None:
136 thread = client.beta.chatkit.threads.list_items(
137 thread_id="cthr_123",
138 )
139 assert_matches_type(SyncConversationCursorPage[Data], thread, path=["response"])
140
141 @parametrize
142 def test_method_list_items_with_all_params(self, client: OpenAI) -> None:
143 thread = client.beta.chatkit.threads.list_items(
144 thread_id="cthr_123",
145 after="after",
146 before="before",
147 limit=0,
148 order="asc",
149 )
150 assert_matches_type(SyncConversationCursorPage[Data], thread, path=["response"])
151
152 @parametrize
153 def test_raw_response_list_items(self, client: OpenAI) -> None:
154 response = client.beta.chatkit.threads.with_raw_response.list_items(
155 thread_id="cthr_123",
156 )
157
158 assert response.is_closed is True
159 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
160 thread = response.parse()
161 assert_matches_type(SyncConversationCursorPage[Data], thread, path=["response"])
162
163 @parametrize
164 def test_streaming_response_list_items(self, client: OpenAI) -> None:
165 with client.beta.chatkit.threads.with_streaming_response.list_items(
166 thread_id="cthr_123",
167 ) as response:
168 assert not response.is_closed
169 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
170
171 thread = response.parse()
172 assert_matches_type(SyncConversationCursorPage[Data], thread, path=["response"])
173
174 assert cast(Any, response.is_closed) is True
175
176 @parametrize
177 def test_path_params_list_items(self, client: OpenAI) -> None:
178 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
179 client.beta.chatkit.threads.with_raw_response.list_items(
180 thread_id="",
181 )
182
183
184class TestAsyncThreads:
185 parametrize = pytest.mark.parametrize(
186 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
187 )
188
189 @parametrize
190 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
191 thread = await async_client.beta.chatkit.threads.retrieve(
192 "cthr_123",
193 )
194 assert_matches_type(ChatKitThread, thread, path=["response"])
195
196 @parametrize
197 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
198 response = await async_client.beta.chatkit.threads.with_raw_response.retrieve(
199 "cthr_123",
200 )
201
202 assert response.is_closed is True
203 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
204 thread = response.parse()
205 assert_matches_type(ChatKitThread, thread, path=["response"])
206
207 @parametrize
208 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
209 async with async_client.beta.chatkit.threads.with_streaming_response.retrieve(
210 "cthr_123",
211 ) as response:
212 assert not response.is_closed
213 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
214
215 thread = await response.parse()
216 assert_matches_type(ChatKitThread, thread, path=["response"])
217
218 assert cast(Any, response.is_closed) is True
219
220 @parametrize
221 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
222 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
223 await async_client.beta.chatkit.threads.with_raw_response.retrieve(
224 "",
225 )
226
227 @parametrize
228 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
229 thread = await async_client.beta.chatkit.threads.list()
230 assert_matches_type(AsyncConversationCursorPage[ChatKitThread], thread, path=["response"])
231
232 @parametrize
233 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
234 thread = await async_client.beta.chatkit.threads.list(
235 after="after",
236 before="before",
237 limit=0,
238 order="asc",
239 user="x",
240 )
241 assert_matches_type(AsyncConversationCursorPage[ChatKitThread], thread, path=["response"])
242
243 @parametrize
244 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
245 response = await async_client.beta.chatkit.threads.with_raw_response.list()
246
247 assert response.is_closed is True
248 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
249 thread = response.parse()
250 assert_matches_type(AsyncConversationCursorPage[ChatKitThread], thread, path=["response"])
251
252 @parametrize
253 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
254 async with async_client.beta.chatkit.threads.with_streaming_response.list() as response:
255 assert not response.is_closed
256 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
257
258 thread = await response.parse()
259 assert_matches_type(AsyncConversationCursorPage[ChatKitThread], thread, path=["response"])
260
261 assert cast(Any, response.is_closed) is True
262
263 @parametrize
264 async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
265 thread = await async_client.beta.chatkit.threads.delete(
266 "cthr_123",
267 )
268 assert_matches_type(ThreadDeleteResponse, thread, path=["response"])
269
270 @parametrize
271 async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
272 response = await async_client.beta.chatkit.threads.with_raw_response.delete(
273 "cthr_123",
274 )
275
276 assert response.is_closed is True
277 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
278 thread = response.parse()
279 assert_matches_type(ThreadDeleteResponse, thread, path=["response"])
280
281 @parametrize
282 async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
283 async with async_client.beta.chatkit.threads.with_streaming_response.delete(
284 "cthr_123",
285 ) as response:
286 assert not response.is_closed
287 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
288
289 thread = await response.parse()
290 assert_matches_type(ThreadDeleteResponse, thread, path=["response"])
291
292 assert cast(Any, response.is_closed) is True
293
294 @parametrize
295 async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
296 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
297 await async_client.beta.chatkit.threads.with_raw_response.delete(
298 "",
299 )
300
301 @parametrize
302 async def test_method_list_items(self, async_client: AsyncOpenAI) -> None:
303 thread = await async_client.beta.chatkit.threads.list_items(
304 thread_id="cthr_123",
305 )
306 assert_matches_type(AsyncConversationCursorPage[Data], thread, path=["response"])
307
308 @parametrize
309 async def test_method_list_items_with_all_params(self, async_client: AsyncOpenAI) -> None:
310 thread = await async_client.beta.chatkit.threads.list_items(
311 thread_id="cthr_123",
312 after="after",
313 before="before",
314 limit=0,
315 order="asc",
316 )
317 assert_matches_type(AsyncConversationCursorPage[Data], thread, path=["response"])
318
319 @parametrize
320 async def test_raw_response_list_items(self, async_client: AsyncOpenAI) -> None:
321 response = await async_client.beta.chatkit.threads.with_raw_response.list_items(
322 thread_id="cthr_123",
323 )
324
325 assert response.is_closed is True
326 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
327 thread = response.parse()
328 assert_matches_type(AsyncConversationCursorPage[Data], thread, path=["response"])
329
330 @parametrize
331 async def test_streaming_response_list_items(self, async_client: AsyncOpenAI) -> None:
332 async with async_client.beta.chatkit.threads.with_streaming_response.list_items(
333 thread_id="cthr_123",
334 ) as response:
335 assert not response.is_closed
336 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
337
338 thread = await response.parse()
339 assert_matches_type(AsyncConversationCursorPage[Data], thread, path=["response"])
340
341 assert cast(Any, response.is_closed) is True
342
343 @parametrize
344 async def test_path_params_list_items(self, async_client: AsyncOpenAI) -> None:
345 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
346 await async_client.beta.chatkit.threads.with_raw_response.list_items(
347 thread_id="",
348 )