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