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