openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/threads/test_messages.py
445lines · modeblame
baa9f07fRobert Craigie2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
baa9f07fRobert Craigie2 years ago | 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 ThreadMessage | |
| 14 | | |
| 15 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 16 | | |
| 17 | | |
| 18 | class TestMessages: | |
98d779fbStainless Bot2 years ago | 19 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
baa9f07fRobert Craigie2 years ago | 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(ThreadMessage, 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(ThreadMessage, 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 | ) | |
86379b44Stainless Bot2 years ago | 48 | |
| 49 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 50 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 51 | message = response.parse() | |
| 52 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 53 | | |
86379b44Stainless Bot2 years ago | 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(ThreadMessage, message, path=["response"]) | |
| 66 | | |
| 67 | assert cast(Any, response.is_closed) is True | |
| 68 | | |
023a4e66Stainless Bot2 years ago | 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 | | |
baa9f07fRobert Craigie2 years ago | 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(ThreadMessage, 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 | ) | |
86379b44Stainless Bot2 years ago | 92 | |
| 93 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 94 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 95 | message = response.parse() | |
| 96 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 97 | | |
86379b44Stainless Bot2 years ago | 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(ThreadMessage, message, path=["response"]) | |
| 109 | | |
| 110 | assert cast(Any, response.is_closed) is True | |
| 111 | | |
023a4e66Stainless Bot2 years ago | 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 | | |
baa9f07fRobert Craigie2 years ago | 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(ThreadMessage, 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(ThreadMessage, 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 | ) | |
86379b44Stainless Bot2 years ago | 149 | |
| 150 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 151 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 152 | message = response.parse() | |
| 153 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 154 | | |
86379b44Stainless Bot2 years ago | 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(ThreadMessage, message, path=["response"]) | |
| 166 | | |
| 167 | assert cast(Any, response.is_closed) is True | |
| 168 | | |
023a4e66Stainless Bot2 years ago | 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 | | |
baa9f07fRobert Craigie2 years ago | 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[ThreadMessage], 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 | ) | |
| 199 | assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"]) | |
| 200 | | |
| 201 | @parametrize | |
| 202 | def test_raw_response_list(self, client: OpenAI) -> None: | |
| 203 | response = client.beta.threads.messages.with_raw_response.list( | |
| 204 | "string", | |
| 205 | ) | |
86379b44Stainless Bot2 years ago | 206 | |
| 207 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 208 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 209 | message = response.parse() | |
| 210 | assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"]) | |
| 211 | | |
86379b44Stainless Bot2 years ago | 212 | @parametrize |
| 213 | def test_streaming_response_list(self, client: OpenAI) -> None: | |
| 214 | with client.beta.threads.messages.with_streaming_response.list( | |
| 215 | "string", | |
| 216 | ) as response: | |
| 217 | assert not response.is_closed | |
| 218 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 219 | | |
| 220 | message = response.parse() | |
| 221 | assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"]) | |
| 222 | | |
| 223 | assert cast(Any, response.is_closed) is True | |
| 224 | | |
023a4e66Stainless Bot2 years ago | 225 | @parametrize |
| 226 | def test_path_params_list(self, client: OpenAI) -> None: | |
| 227 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): | |
| 228 | client.beta.threads.messages.with_raw_response.list( | |
| 229 | "", | |
| 230 | ) | |
| 231 | | |
baa9f07fRobert Craigie2 years ago | 232 | |
| 233 | class TestAsyncMessages: | |
98d779fbStainless Bot2 years ago | 234 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
baa9f07fRobert Craigie2 years ago | 235 | |
| 236 | @parametrize | |
98d779fbStainless Bot2 years ago | 237 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 238 | message = await async_client.beta.threads.messages.create( | |
baa9f07fRobert Craigie2 years ago | 239 | "string", |
| 240 | content="x", | |
| 241 | role="user", | |
| 242 | ) | |
| 243 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 244 | | |
| 245 | @parametrize | |
98d779fbStainless Bot2 years ago | 246 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 247 | message = await async_client.beta.threads.messages.create( | |
baa9f07fRobert Craigie2 years ago | 248 | "string", |
| 249 | content="x", | |
| 250 | role="user", | |
| 251 | file_ids=["string"], | |
| 252 | metadata={}, | |
| 253 | ) | |
| 254 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 255 | | |
| 256 | @parametrize | |
98d779fbStainless Bot2 years ago | 257 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 258 | response = await async_client.beta.threads.messages.with_raw_response.create( | |
baa9f07fRobert Craigie2 years ago | 259 | "string", |
| 260 | content="x", | |
| 261 | role="user", | |
| 262 | ) | |
86379b44Stainless Bot2 years ago | 263 | |
| 264 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 265 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 266 | message = response.parse() | |
| 267 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 268 | | |
86379b44Stainless Bot2 years ago | 269 | @parametrize |
98d779fbStainless Bot2 years ago | 270 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 271 | async with async_client.beta.threads.messages.with_streaming_response.create( | |
86379b44Stainless Bot2 years ago | 272 | "string", |
| 273 | content="x", | |
| 274 | role="user", | |
| 275 | ) as response: | |
| 276 | assert not response.is_closed | |
| 277 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 278 | | |
| 279 | message = await response.parse() | |
| 280 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 281 | | |
| 282 | assert cast(Any, response.is_closed) is True | |
| 283 | | |
023a4e66Stainless Bot2 years ago | 284 | @parametrize |
98d779fbStainless Bot2 years ago | 285 | async def test_path_params_create(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 286 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
98d779fbStainless Bot2 years ago | 287 | await async_client.beta.threads.messages.with_raw_response.create( |
023a4e66Stainless Bot2 years ago | 288 | "", |
| 289 | content="x", | |
| 290 | role="user", | |
| 291 | ) | |
| 292 | | |
baa9f07fRobert Craigie2 years ago | 293 | @parametrize |
98d779fbStainless Bot2 years ago | 294 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 295 | message = await async_client.beta.threads.messages.retrieve( | |
baa9f07fRobert Craigie2 years ago | 296 | "string", |
| 297 | thread_id="string", | |
| 298 | ) | |
| 299 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 300 | | |
| 301 | @parametrize | |
98d779fbStainless Bot2 years ago | 302 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 303 | response = await async_client.beta.threads.messages.with_raw_response.retrieve( | |
baa9f07fRobert Craigie2 years ago | 304 | "string", |
| 305 | thread_id="string", | |
| 306 | ) | |
86379b44Stainless Bot2 years ago | 307 | |
| 308 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 309 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 310 | message = response.parse() | |
| 311 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 312 | | |
86379b44Stainless Bot2 years ago | 313 | @parametrize |
98d779fbStainless Bot2 years ago | 314 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 315 | async with async_client.beta.threads.messages.with_streaming_response.retrieve( | |
86379b44Stainless Bot2 years ago | 316 | "string", |
| 317 | thread_id="string", | |
| 318 | ) as response: | |
| 319 | assert not response.is_closed | |
| 320 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 321 | | |
| 322 | message = await response.parse() | |
| 323 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 324 | | |
| 325 | assert cast(Any, response.is_closed) is True | |
| 326 | | |
023a4e66Stainless Bot2 years ago | 327 | @parametrize |
98d779fbStainless Bot2 years ago | 328 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 329 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
98d779fbStainless Bot2 years ago | 330 | await async_client.beta.threads.messages.with_raw_response.retrieve( |
023a4e66Stainless Bot2 years ago | 331 | "string", |
| 332 | thread_id="", | |
| 333 | ) | |
| 334 | | |
| 335 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"): | |
98d779fbStainless Bot2 years ago | 336 | await async_client.beta.threads.messages.with_raw_response.retrieve( |
023a4e66Stainless Bot2 years ago | 337 | "", |
| 338 | thread_id="string", | |
| 339 | ) | |
| 340 | | |
baa9f07fRobert Craigie2 years ago | 341 | @parametrize |
98d779fbStainless Bot2 years ago | 342 | async def test_method_update(self, async_client: AsyncOpenAI) -> None: |
| 343 | message = await async_client.beta.threads.messages.update( | |
baa9f07fRobert Craigie2 years ago | 344 | "string", |
| 345 | thread_id="string", | |
| 346 | ) | |
| 347 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 348 | | |
| 349 | @parametrize | |
98d779fbStainless Bot2 years ago | 350 | async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 351 | message = await async_client.beta.threads.messages.update( | |
baa9f07fRobert Craigie2 years ago | 352 | "string", |
| 353 | thread_id="string", | |
| 354 | metadata={}, | |
| 355 | ) | |
| 356 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 357 | | |
| 358 | @parametrize | |
98d779fbStainless Bot2 years ago | 359 | async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None: |
| 360 | response = await async_client.beta.threads.messages.with_raw_response.update( | |
baa9f07fRobert Craigie2 years ago | 361 | "string", |
| 362 | thread_id="string", | |
| 363 | ) | |
86379b44Stainless Bot2 years ago | 364 | |
| 365 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 366 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 367 | message = response.parse() | |
| 368 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 369 | | |
86379b44Stainless Bot2 years ago | 370 | @parametrize |
98d779fbStainless Bot2 years ago | 371 | async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None: |
| 372 | async with async_client.beta.threads.messages.with_streaming_response.update( | |
86379b44Stainless Bot2 years ago | 373 | "string", |
| 374 | thread_id="string", | |
| 375 | ) as response: | |
| 376 | assert not response.is_closed | |
| 377 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 378 | | |
| 379 | message = await response.parse() | |
| 380 | assert_matches_type(ThreadMessage, message, path=["response"]) | |
| 381 | | |
| 382 | assert cast(Any, response.is_closed) is True | |
| 383 | | |
023a4e66Stainless Bot2 years ago | 384 | @parametrize |
98d779fbStainless Bot2 years ago | 385 | async def test_path_params_update(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 386 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
98d779fbStainless Bot2 years ago | 387 | await async_client.beta.threads.messages.with_raw_response.update( |
023a4e66Stainless Bot2 years ago | 388 | "string", |
| 389 | thread_id="", | |
| 390 | ) | |
| 391 | | |
| 392 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"): | |
98d779fbStainless Bot2 years ago | 393 | await async_client.beta.threads.messages.with_raw_response.update( |
023a4e66Stainless Bot2 years ago | 394 | "", |
| 395 | thread_id="string", | |
| 396 | ) | |
| 397 | | |
baa9f07fRobert Craigie2 years ago | 398 | @parametrize |
98d779fbStainless Bot2 years ago | 399 | async def test_method_list(self, async_client: AsyncOpenAI) -> None: |
| 400 | message = await async_client.beta.threads.messages.list( | |
baa9f07fRobert Craigie2 years ago | 401 | "string", |
| 402 | ) | |
| 403 | assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"]) | |
| 404 | | |
| 405 | @parametrize | |
98d779fbStainless Bot2 years ago | 406 | async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 407 | message = await async_client.beta.threads.messages.list( | |
baa9f07fRobert Craigie2 years ago | 408 | "string", |
| 409 | after="string", | |
| 410 | before="string", | |
| 411 | limit=0, | |
| 412 | order="asc", | |
| 413 | ) | |
| 414 | assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"]) | |
| 415 | | |
| 416 | @parametrize | |
98d779fbStainless Bot2 years ago | 417 | async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None: |
| 418 | response = await async_client.beta.threads.messages.with_raw_response.list( | |
baa9f07fRobert Craigie2 years ago | 419 | "string", |
| 420 | ) | |
86379b44Stainless Bot2 years ago | 421 | |
| 422 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 423 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 424 | message = response.parse() | |
| 425 | assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"]) | |
86379b44Stainless Bot2 years ago | 426 | |
| 427 | @parametrize | |
98d779fbStainless Bot2 years ago | 428 | async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None: |
| 429 | async with async_client.beta.threads.messages.with_streaming_response.list( | |
86379b44Stainless Bot2 years ago | 430 | "string", |
| 431 | ) as response: | |
| 432 | assert not response.is_closed | |
| 433 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 434 | | |
| 435 | message = await response.parse() | |
| 436 | assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"]) | |
| 437 | | |
| 438 | assert cast(Any, response.is_closed) is True | |
023a4e66Stainless Bot2 years ago | 439 | |
| 440 | @parametrize | |
98d779fbStainless Bot2 years ago | 441 | async def test_path_params_list(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 442 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
98d779fbStainless Bot2 years ago | 443 | await async_client.beta.threads.messages.with_raw_response.list( |
023a4e66Stainless Bot2 years ago | 444 | "", |
| 445 | ) |