openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_files.py
251lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | import httpx |
| 8 | import pytest |
| 9 | from respx import MockRouter |
| 10 | |
| 11 | from openai import OpenAI, AsyncOpenAI |
| 12 | from tests.utils import assert_matches_type |
| 13 | from openai.types import FileObject, FileDeleted |
| 14 | from openai._types import BinaryResponseContent |
| 15 | from openai._client import OpenAI, AsyncOpenAI |
| 16 | from openai.pagination import SyncPage, AsyncPage |
| 17 | |
| 18 | # pyright: reportDeprecated=false |
| 19 | |
| 20 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 21 | api_key = "My API Key" |
| 22 | |
| 23 | |
| 24 | class TestFiles: |
| 25 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 26 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 27 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 28 | |
| 29 | @parametrize |
| 30 | def test_method_create(self, client: OpenAI) -> None: |
| 31 | file = client.files.create( |
| 32 | file=b"raw file contents", |
| 33 | purpose="fine-tune", |
| 34 | ) |
| 35 | assert_matches_type(FileObject, file, path=["response"]) |
| 36 | |
| 37 | @parametrize |
| 38 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 39 | response = client.files.with_raw_response.create( |
| 40 | file=b"raw file contents", |
| 41 | purpose="fine-tune", |
| 42 | ) |
| 43 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 44 | file = response.parse() |
| 45 | assert_matches_type(FileObject, file, path=["response"]) |
| 46 | |
| 47 | @parametrize |
| 48 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 49 | file = client.files.retrieve( |
| 50 | "string", |
| 51 | ) |
| 52 | assert_matches_type(FileObject, file, path=["response"]) |
| 53 | |
| 54 | @parametrize |
| 55 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 56 | response = client.files.with_raw_response.retrieve( |
| 57 | "string", |
| 58 | ) |
| 59 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 60 | file = response.parse() |
| 61 | assert_matches_type(FileObject, file, path=["response"]) |
| 62 | |
| 63 | @parametrize |
| 64 | def test_method_list(self, client: OpenAI) -> None: |
| 65 | file = client.files.list() |
| 66 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) |
| 67 | |
| 68 | @parametrize |
| 69 | def test_method_list_with_all_params(self, client: OpenAI) -> None: |
| 70 | file = client.files.list( |
| 71 | purpose="string", |
| 72 | ) |
| 73 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) |
| 74 | |
| 75 | @parametrize |
| 76 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 77 | response = client.files.with_raw_response.list() |
| 78 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 79 | file = response.parse() |
| 80 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) |
| 81 | |
| 82 | @parametrize |
| 83 | def test_method_delete(self, client: OpenAI) -> None: |
| 84 | file = client.files.delete( |
| 85 | "string", |
| 86 | ) |
| 87 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 88 | |
| 89 | @parametrize |
| 90 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 91 | response = client.files.with_raw_response.delete( |
| 92 | "string", |
| 93 | ) |
| 94 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 95 | file = response.parse() |
| 96 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 97 | |
| 98 | @parametrize |
| 99 | @pytest.mark.respx(base_url=base_url) |
| 100 | def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 101 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 102 | file = client.files.content( |
| 103 | "string", |
| 104 | ) |
| 105 | assert isinstance(file, BinaryResponseContent) |
| 106 | assert file.json() == {"foo": "bar"} |
| 107 | |
| 108 | @parametrize |
| 109 | @pytest.mark.respx(base_url=base_url) |
| 110 | def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 111 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 112 | response = client.files.with_raw_response.content( |
| 113 | "string", |
| 114 | ) |
| 115 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 116 | file = response.parse() |
| 117 | assert isinstance(file, BinaryResponseContent) |
| 118 | assert file.json() == {"foo": "bar"} |
| 119 | |
| 120 | @parametrize |
| 121 | def test_method_retrieve_content(self, client: OpenAI) -> None: |
| 122 | with pytest.warns(DeprecationWarning): |
| 123 | file = client.files.retrieve_content( |
| 124 | "string", |
| 125 | ) |
| 126 | assert_matches_type(str, file, path=["response"]) |
| 127 | |
| 128 | @parametrize |
| 129 | def test_raw_response_retrieve_content(self, client: OpenAI) -> None: |
| 130 | with pytest.warns(DeprecationWarning): |
| 131 | response = client.files.with_raw_response.retrieve_content( |
| 132 | "string", |
| 133 | ) |
| 134 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 135 | file = response.parse() |
| 136 | assert_matches_type(str, file, path=["response"]) |
| 137 | |
| 138 | |
| 139 | class TestAsyncFiles: |
| 140 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 141 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 142 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 143 | |
| 144 | @parametrize |
| 145 | async def test_method_create(self, client: AsyncOpenAI) -> None: |
| 146 | file = await client.files.create( |
| 147 | file=b"raw file contents", |
| 148 | purpose="fine-tune", |
| 149 | ) |
| 150 | assert_matches_type(FileObject, file, path=["response"]) |
| 151 | |
| 152 | @parametrize |
| 153 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: |
| 154 | response = await client.files.with_raw_response.create( |
| 155 | file=b"raw file contents", |
| 156 | purpose="fine-tune", |
| 157 | ) |
| 158 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 159 | file = response.parse() |
| 160 | assert_matches_type(FileObject, file, path=["response"]) |
| 161 | |
| 162 | @parametrize |
| 163 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 164 | file = await client.files.retrieve( |
| 165 | "string", |
| 166 | ) |
| 167 | assert_matches_type(FileObject, file, path=["response"]) |
| 168 | |
| 169 | @parametrize |
| 170 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 171 | response = await client.files.with_raw_response.retrieve( |
| 172 | "string", |
| 173 | ) |
| 174 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 175 | file = response.parse() |
| 176 | assert_matches_type(FileObject, file, path=["response"]) |
| 177 | |
| 178 | @parametrize |
| 179 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 180 | file = await client.files.list() |
| 181 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 182 | |
| 183 | @parametrize |
| 184 | async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None: |
| 185 | file = await client.files.list( |
| 186 | purpose="string", |
| 187 | ) |
| 188 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 189 | |
| 190 | @parametrize |
| 191 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 192 | response = await client.files.with_raw_response.list() |
| 193 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 194 | file = response.parse() |
| 195 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 196 | |
| 197 | @parametrize |
| 198 | async def test_method_delete(self, client: AsyncOpenAI) -> None: |
| 199 | file = await client.files.delete( |
| 200 | "string", |
| 201 | ) |
| 202 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 203 | |
| 204 | @parametrize |
| 205 | async def test_raw_response_delete(self, client: AsyncOpenAI) -> None: |
| 206 | response = await client.files.with_raw_response.delete( |
| 207 | "string", |
| 208 | ) |
| 209 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 210 | file = response.parse() |
| 211 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 212 | |
| 213 | @parametrize |
| 214 | @pytest.mark.respx(base_url=base_url) |
| 215 | async def test_method_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 216 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 217 | file = await client.files.content( |
| 218 | "string", |
| 219 | ) |
| 220 | assert isinstance(file, BinaryResponseContent) |
| 221 | assert file.json() == {"foo": "bar"} |
| 222 | |
| 223 | @parametrize |
| 224 | @pytest.mark.respx(base_url=base_url) |
| 225 | async def test_raw_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 226 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 227 | response = await client.files.with_raw_response.content( |
| 228 | "string", |
| 229 | ) |
| 230 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 231 | file = response.parse() |
| 232 | assert isinstance(file, BinaryResponseContent) |
| 233 | assert file.json() == {"foo": "bar"} |
| 234 | |
| 235 | @parametrize |
| 236 | async def test_method_retrieve_content(self, client: AsyncOpenAI) -> None: |
| 237 | with pytest.warns(DeprecationWarning): |
| 238 | file = await client.files.retrieve_content( |
| 239 | "string", |
| 240 | ) |
| 241 | assert_matches_type(str, file, path=["response"]) |
| 242 | |
| 243 | @parametrize |
| 244 | async def test_raw_response_retrieve_content(self, client: AsyncOpenAI) -> None: |
| 245 | with pytest.warns(DeprecationWarning): |
| 246 | response = await client.files.with_raw_response.retrieve_content( |
| 247 | "string", |
| 248 | ) |
| 249 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 250 | file = response.parse() |
| 251 | assert_matches_type(str, file, path=["response"]) |
| 252 | |