openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_files.py
255lines · 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 | @pytest.mark.skip(reason="mocked response isn't working yet") |
| 99 | @parametrize |
| 100 | @pytest.mark.respx(base_url=base_url) |
| 101 | def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 102 | respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 103 | file = client.files.content( |
| 104 | "string", |
| 105 | ) |
| 106 | assert isinstance(file, BinaryResponseContent) |
| 107 | assert file.json() == {"foo": "bar"} |
| 108 | |
| 109 | @pytest.mark.skip(reason="mocked response isn't working yet") |
| 110 | @parametrize |
| 111 | @pytest.mark.respx(base_url=base_url) |
| 112 | def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 113 | respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 114 | response = client.files.with_raw_response.content( |
| 115 | "string", |
| 116 | ) |
| 117 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 118 | file = response.parse() |
| 119 | assert isinstance(file, BinaryResponseContent) |
| 120 | assert file.json() == {"foo": "bar"} |
| 121 | |
| 122 | @parametrize |
| 123 | def test_method_retrieve_content(self, client: OpenAI) -> None: |
| 124 | with pytest.warns(DeprecationWarning): |
| 125 | file = client.files.retrieve_content( |
| 126 | "string", |
| 127 | ) |
| 128 | assert_matches_type(str, file, path=["response"]) |
| 129 | |
| 130 | @parametrize |
| 131 | def test_raw_response_retrieve_content(self, client: OpenAI) -> None: |
| 132 | with pytest.warns(DeprecationWarning): |
| 133 | response = client.files.with_raw_response.retrieve_content( |
| 134 | "string", |
| 135 | ) |
| 136 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 137 | file = response.parse() |
| 138 | assert_matches_type(str, file, path=["response"]) |
| 139 | |
| 140 | |
| 141 | class TestAsyncFiles: |
| 142 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 143 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 144 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 145 | |
| 146 | @parametrize |
| 147 | async def test_method_create(self, client: AsyncOpenAI) -> None: |
| 148 | file = await client.files.create( |
| 149 | file=b"raw file contents", |
| 150 | purpose="fine-tune", |
| 151 | ) |
| 152 | assert_matches_type(FileObject, file, path=["response"]) |
| 153 | |
| 154 | @parametrize |
| 155 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: |
| 156 | response = await client.files.with_raw_response.create( |
| 157 | file=b"raw file contents", |
| 158 | purpose="fine-tune", |
| 159 | ) |
| 160 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 161 | file = response.parse() |
| 162 | assert_matches_type(FileObject, file, path=["response"]) |
| 163 | |
| 164 | @parametrize |
| 165 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 166 | file = await client.files.retrieve( |
| 167 | "string", |
| 168 | ) |
| 169 | assert_matches_type(FileObject, file, path=["response"]) |
| 170 | |
| 171 | @parametrize |
| 172 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 173 | response = await client.files.with_raw_response.retrieve( |
| 174 | "string", |
| 175 | ) |
| 176 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 177 | file = response.parse() |
| 178 | assert_matches_type(FileObject, file, path=["response"]) |
| 179 | |
| 180 | @parametrize |
| 181 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 182 | file = await client.files.list() |
| 183 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 184 | |
| 185 | @parametrize |
| 186 | async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None: |
| 187 | file = await client.files.list( |
| 188 | purpose="string", |
| 189 | ) |
| 190 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 191 | |
| 192 | @parametrize |
| 193 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 194 | response = await client.files.with_raw_response.list() |
| 195 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 196 | file = response.parse() |
| 197 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 198 | |
| 199 | @parametrize |
| 200 | async def test_method_delete(self, client: AsyncOpenAI) -> None: |
| 201 | file = await client.files.delete( |
| 202 | "string", |
| 203 | ) |
| 204 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 205 | |
| 206 | @parametrize |
| 207 | async def test_raw_response_delete(self, client: AsyncOpenAI) -> None: |
| 208 | response = await client.files.with_raw_response.delete( |
| 209 | "string", |
| 210 | ) |
| 211 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 212 | file = response.parse() |
| 213 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 214 | |
| 215 | @pytest.mark.skip(reason="mocked response isn't working yet") |
| 216 | @parametrize |
| 217 | @pytest.mark.respx(base_url=base_url) |
| 218 | async def test_method_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 219 | respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 220 | file = await client.files.content( |
| 221 | "string", |
| 222 | ) |
| 223 | assert isinstance(file, BinaryResponseContent) |
| 224 | assert file.json() == {"foo": "bar"} |
| 225 | |
| 226 | @pytest.mark.skip(reason="mocked response isn't working yet") |
| 227 | @parametrize |
| 228 | @pytest.mark.respx(base_url=base_url) |
| 229 | async def test_raw_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 230 | respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 231 | response = await client.files.with_raw_response.content( |
| 232 | "string", |
| 233 | ) |
| 234 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 235 | file = response.parse() |
| 236 | assert isinstance(file, BinaryResponseContent) |
| 237 | assert file.json() == {"foo": "bar"} |
| 238 | |
| 239 | @parametrize |
| 240 | async def test_method_retrieve_content(self, client: AsyncOpenAI) -> None: |
| 241 | with pytest.warns(DeprecationWarning): |
| 242 | file = await client.files.retrieve_content( |
| 243 | "string", |
| 244 | ) |
| 245 | assert_matches_type(str, file, path=["response"]) |
| 246 | |
| 247 | @parametrize |
| 248 | async def test_raw_response_retrieve_content(self, client: AsyncOpenAI) -> None: |
| 249 | with pytest.warns(DeprecationWarning): |
| 250 | response = await client.files.with_raw_response.retrieve_content( |
| 251 | "string", |
| 252 | ) |
| 253 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 254 | file = response.parse() |
| 255 | assert_matches_type(str, file, path=["response"]) |
| 256 | |