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