openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/threads/messages/test_files.py
269lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 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._client import OpenAI, AsyncOpenAI |
| 13 | from openai.pagination import SyncCursorPage, AsyncCursorPage |
| 14 | from openai.types.beta.threads.messages import MessageFile |
| 15 | |
| 16 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 17 | api_key = "My API Key" |
| 18 | |
| 19 | |
| 20 | class TestFiles: |
| 21 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 22 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 23 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 24 | |
| 25 | @parametrize |
| 26 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 27 | file = client.beta.threads.messages.files.retrieve( |
| 28 | "file-abc123", |
| 29 | thread_id="thread_abc123", |
| 30 | message_id="msg_abc123", |
| 31 | ) |
| 32 | assert_matches_type(MessageFile, file, path=["response"]) |
| 33 | |
| 34 | @parametrize |
| 35 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 36 | response = client.beta.threads.messages.files.with_raw_response.retrieve( |
| 37 | "file-abc123", |
| 38 | thread_id="thread_abc123", |
| 39 | message_id="msg_abc123", |
| 40 | ) |
| 41 | |
| 42 | assert response.is_closed is True |
| 43 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 44 | file = response.parse() |
| 45 | assert_matches_type(MessageFile, file, path=["response"]) |
| 46 | |
| 47 | @parametrize |
| 48 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: |
| 49 | with client.beta.threads.messages.files.with_streaming_response.retrieve( |
| 50 | "file-abc123", |
| 51 | thread_id="thread_abc123", |
| 52 | message_id="msg_abc123", |
| 53 | ) as response: |
| 54 | assert not response.is_closed |
| 55 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 56 | |
| 57 | file = response.parse() |
| 58 | assert_matches_type(MessageFile, file, path=["response"]) |
| 59 | |
| 60 | assert cast(Any, response.is_closed) is True |
| 61 | |
| 62 | @parametrize |
| 63 | def test_path_params_retrieve(self, client: OpenAI) -> None: |
| 64 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 65 | client.beta.threads.messages.files.with_raw_response.retrieve( |
| 66 | "file-abc123", |
| 67 | thread_id="", |
| 68 | message_id="msg_abc123", |
| 69 | ) |
| 70 | |
| 71 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"): |
| 72 | client.beta.threads.messages.files.with_raw_response.retrieve( |
| 73 | "file-abc123", |
| 74 | thread_id="thread_abc123", |
| 75 | message_id="", |
| 76 | ) |
| 77 | |
| 78 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 79 | client.beta.threads.messages.files.with_raw_response.retrieve( |
| 80 | "", |
| 81 | thread_id="thread_abc123", |
| 82 | message_id="msg_abc123", |
| 83 | ) |
| 84 | |
| 85 | @parametrize |
| 86 | def test_method_list(self, client: OpenAI) -> None: |
| 87 | file = client.beta.threads.messages.files.list( |
| 88 | "string", |
| 89 | thread_id="string", |
| 90 | ) |
| 91 | assert_matches_type(SyncCursorPage[MessageFile], file, path=["response"]) |
| 92 | |
| 93 | @parametrize |
| 94 | def test_method_list_with_all_params(self, client: OpenAI) -> None: |
| 95 | file = client.beta.threads.messages.files.list( |
| 96 | "string", |
| 97 | thread_id="string", |
| 98 | after="string", |
| 99 | before="string", |
| 100 | limit=0, |
| 101 | order="asc", |
| 102 | ) |
| 103 | assert_matches_type(SyncCursorPage[MessageFile], file, path=["response"]) |
| 104 | |
| 105 | @parametrize |
| 106 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 107 | response = client.beta.threads.messages.files.with_raw_response.list( |
| 108 | "string", |
| 109 | thread_id="string", |
| 110 | ) |
| 111 | |
| 112 | assert response.is_closed is True |
| 113 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 114 | file = response.parse() |
| 115 | assert_matches_type(SyncCursorPage[MessageFile], file, path=["response"]) |
| 116 | |
| 117 | @parametrize |
| 118 | def test_streaming_response_list(self, client: OpenAI) -> None: |
| 119 | with client.beta.threads.messages.files.with_streaming_response.list( |
| 120 | "string", |
| 121 | thread_id="string", |
| 122 | ) as response: |
| 123 | assert not response.is_closed |
| 124 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 125 | |
| 126 | file = response.parse() |
| 127 | assert_matches_type(SyncCursorPage[MessageFile], file, path=["response"]) |
| 128 | |
| 129 | assert cast(Any, response.is_closed) is True |
| 130 | |
| 131 | @parametrize |
| 132 | def test_path_params_list(self, client: OpenAI) -> None: |
| 133 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 134 | client.beta.threads.messages.files.with_raw_response.list( |
| 135 | "string", |
| 136 | thread_id="", |
| 137 | ) |
| 138 | |
| 139 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"): |
| 140 | client.beta.threads.messages.files.with_raw_response.list( |
| 141 | "", |
| 142 | thread_id="string", |
| 143 | ) |
| 144 | |
| 145 | |
| 146 | class TestAsyncFiles: |
| 147 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 148 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 149 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 150 | |
| 151 | @parametrize |
| 152 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 153 | file = await client.beta.threads.messages.files.retrieve( |
| 154 | "file-abc123", |
| 155 | thread_id="thread_abc123", |
| 156 | message_id="msg_abc123", |
| 157 | ) |
| 158 | assert_matches_type(MessageFile, file, path=["response"]) |
| 159 | |
| 160 | @parametrize |
| 161 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 162 | response = await client.beta.threads.messages.files.with_raw_response.retrieve( |
| 163 | "file-abc123", |
| 164 | thread_id="thread_abc123", |
| 165 | message_id="msg_abc123", |
| 166 | ) |
| 167 | |
| 168 | assert response.is_closed is True |
| 169 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 170 | file = response.parse() |
| 171 | assert_matches_type(MessageFile, file, path=["response"]) |
| 172 | |
| 173 | @parametrize |
| 174 | async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 175 | async with client.beta.threads.messages.files.with_streaming_response.retrieve( |
| 176 | "file-abc123", |
| 177 | thread_id="thread_abc123", |
| 178 | message_id="msg_abc123", |
| 179 | ) as response: |
| 180 | assert not response.is_closed |
| 181 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 182 | |
| 183 | file = await response.parse() |
| 184 | assert_matches_type(MessageFile, file, path=["response"]) |
| 185 | |
| 186 | assert cast(Any, response.is_closed) is True |
| 187 | |
| 188 | @parametrize |
| 189 | async def test_path_params_retrieve(self, client: AsyncOpenAI) -> None: |
| 190 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 191 | await client.beta.threads.messages.files.with_raw_response.retrieve( |
| 192 | "file-abc123", |
| 193 | thread_id="", |
| 194 | message_id="msg_abc123", |
| 195 | ) |
| 196 | |
| 197 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"): |
| 198 | await client.beta.threads.messages.files.with_raw_response.retrieve( |
| 199 | "file-abc123", |
| 200 | thread_id="thread_abc123", |
| 201 | message_id="", |
| 202 | ) |
| 203 | |
| 204 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 205 | await client.beta.threads.messages.files.with_raw_response.retrieve( |
| 206 | "", |
| 207 | thread_id="thread_abc123", |
| 208 | message_id="msg_abc123", |
| 209 | ) |
| 210 | |
| 211 | @parametrize |
| 212 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 213 | file = await client.beta.threads.messages.files.list( |
| 214 | "string", |
| 215 | thread_id="string", |
| 216 | ) |
| 217 | assert_matches_type(AsyncCursorPage[MessageFile], file, path=["response"]) |
| 218 | |
| 219 | @parametrize |
| 220 | async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None: |
| 221 | file = await client.beta.threads.messages.files.list( |
| 222 | "string", |
| 223 | thread_id="string", |
| 224 | after="string", |
| 225 | before="string", |
| 226 | limit=0, |
| 227 | order="asc", |
| 228 | ) |
| 229 | assert_matches_type(AsyncCursorPage[MessageFile], file, path=["response"]) |
| 230 | |
| 231 | @parametrize |
| 232 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 233 | response = await client.beta.threads.messages.files.with_raw_response.list( |
| 234 | "string", |
| 235 | thread_id="string", |
| 236 | ) |
| 237 | |
| 238 | assert response.is_closed is True |
| 239 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 240 | file = response.parse() |
| 241 | assert_matches_type(AsyncCursorPage[MessageFile], file, path=["response"]) |
| 242 | |
| 243 | @parametrize |
| 244 | async def test_streaming_response_list(self, client: AsyncOpenAI) -> None: |
| 245 | async with client.beta.threads.messages.files.with_streaming_response.list( |
| 246 | "string", |
| 247 | thread_id="string", |
| 248 | ) as response: |
| 249 | assert not response.is_closed |
| 250 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 251 | |
| 252 | file = await response.parse() |
| 253 | assert_matches_type(AsyncCursorPage[MessageFile], file, path=["response"]) |
| 254 | |
| 255 | assert cast(Any, response.is_closed) is True |
| 256 | |
| 257 | @parametrize |
| 258 | async def test_path_params_list(self, client: AsyncOpenAI) -> None: |
| 259 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 260 | await client.beta.threads.messages.files.with_raw_response.list( |
| 261 | "string", |
| 262 | thread_id="", |
| 263 | ) |
| 264 | |
| 265 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"): |
| 266 | await client.beta.threads.messages.files.with_raw_response.list( |
| 267 | "", |
| 268 | thread_id="string", |
| 269 | ) |
| 270 | |