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