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