openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.3.6

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/test_files.py

255lines · modeblame

08b8179aDavid Schnurr2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
6
aa681899Stainless Bot2 years ago7import httpx
08b8179aDavid Schnurr2 years ago8import pytest
aa681899Stainless Bot2 years ago9from respx import MockRouter
08b8179aDavid Schnurr2 years ago10
11from openai import OpenAI, AsyncOpenAI
12from tests.utils import assert_matches_type
13from openai.types import FileObject, FileDeleted
aa681899Stainless Bot2 years ago14from openai._types import BinaryResponseContent
08b8179aDavid Schnurr2 years ago15from openai._client import OpenAI, AsyncOpenAI
16from openai.pagination import SyncPage, AsyncPage
17
aa681899Stainless Bot2 years ago18# pyright: reportDeprecated=false
19
08b8179aDavid Schnurr2 years ago20base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
21api_key = "My API Key"
22
23
24class TestFiles:
25strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
26loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
27parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
28
29@parametrize
30def test_method_create(self, client: OpenAI) -> None:
31file = client.files.create(
32file=b"raw file contents",
baa9f07fRobert Craigie2 years ago33purpose="fine-tune",
08b8179aDavid Schnurr2 years ago34)
35assert_matches_type(FileObject, file, path=["response"])
36
37@parametrize
38def test_raw_response_create(self, client: OpenAI) -> None:
39response = client.files.with_raw_response.create(
40file=b"raw file contents",
baa9f07fRobert Craigie2 years ago41purpose="fine-tune",
08b8179aDavid Schnurr2 years ago42)
43assert response.http_request.headers.get("X-Stainless-Lang") == "python"
44file = response.parse()
45assert_matches_type(FileObject, file, path=["response"])
46
47@parametrize
48def test_method_retrieve(self, client: OpenAI) -> None:
49file = client.files.retrieve(
50"string",
51)
52assert_matches_type(FileObject, file, path=["response"])
53
54@parametrize
55def test_raw_response_retrieve(self, client: OpenAI) -> None:
56response = client.files.with_raw_response.retrieve(
57"string",
58)
59assert response.http_request.headers.get("X-Stainless-Lang") == "python"
60file = response.parse()
61assert_matches_type(FileObject, file, path=["response"])
62
63@parametrize
64def test_method_list(self, client: OpenAI) -> None:
65file = client.files.list()
66assert_matches_type(SyncPage[FileObject], file, path=["response"])
67
baa9f07fRobert Craigie2 years ago68@parametrize
69def test_method_list_with_all_params(self, client: OpenAI) -> None:
70file = client.files.list(
71purpose="string",
72)
73assert_matches_type(SyncPage[FileObject], file, path=["response"])
74
08b8179aDavid Schnurr2 years ago75@parametrize
76def test_raw_response_list(self, client: OpenAI) -> None:
77response = client.files.with_raw_response.list()
78assert response.http_request.headers.get("X-Stainless-Lang") == "python"
79file = response.parse()
80assert_matches_type(SyncPage[FileObject], file, path=["response"])
81
82@parametrize
83def test_method_delete(self, client: OpenAI) -> None:
84file = client.files.delete(
85"string",
86)
87assert_matches_type(FileDeleted, file, path=["response"])
88
89@parametrize
90def test_raw_response_delete(self, client: OpenAI) -> None:
91response = client.files.with_raw_response.delete(
92"string",
93)
94assert response.http_request.headers.get("X-Stainless-Lang") == "python"
95file = response.parse()
96assert_matches_type(FileDeleted, file, path=["response"])
97
c5975bd0Stainless Bot2 years ago98@pytest.mark.skip(reason="mocked response isn't working yet")
08b8179aDavid Schnurr2 years ago99@parametrize
aa681899Stainless Bot2 years ago100@pytest.mark.respx(base_url=base_url)
101def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
102respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
103file = client.files.content(
08b8179aDavid Schnurr2 years ago104"string",
105)
aa681899Stainless Bot2 years ago106assert isinstance(file, BinaryResponseContent)
107assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago108
c5975bd0Stainless Bot2 years ago109@pytest.mark.skip(reason="mocked response isn't working yet")
08b8179aDavid Schnurr2 years ago110@parametrize
aa681899Stainless Bot2 years ago111@pytest.mark.respx(base_url=base_url)
112def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
113respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
114response = client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago115"string",
116)
117assert response.http_request.headers.get("X-Stainless-Lang") == "python"
118file = response.parse()
aa681899Stainless Bot2 years ago119assert isinstance(file, BinaryResponseContent)
120assert file.json() == {"foo": "bar"}
121
122@parametrize
123def test_method_retrieve_content(self, client: OpenAI) -> None:
124with pytest.warns(DeprecationWarning):
125file = client.files.retrieve_content(
126"string",
127)
128assert_matches_type(str, file, path=["response"])
129
130@parametrize
131def test_raw_response_retrieve_content(self, client: OpenAI) -> None:
132with pytest.warns(DeprecationWarning):
133response = client.files.with_raw_response.retrieve_content(
134"string",
135)
136assert response.http_request.headers.get("X-Stainless-Lang") == "python"
137file = response.parse()
08b8179aDavid Schnurr2 years ago138assert_matches_type(str, file, path=["response"])
139
140
141class TestAsyncFiles:
142strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
143loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
144parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
145
146@parametrize
147async def test_method_create(self, client: AsyncOpenAI) -> None:
148file = await client.files.create(
149file=b"raw file contents",
baa9f07fRobert Craigie2 years ago150purpose="fine-tune",
08b8179aDavid Schnurr2 years ago151)
152assert_matches_type(FileObject, file, path=["response"])
153
154@parametrize
155async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
156response = await client.files.with_raw_response.create(
157file=b"raw file contents",
baa9f07fRobert Craigie2 years ago158purpose="fine-tune",
08b8179aDavid Schnurr2 years ago159)
160assert response.http_request.headers.get("X-Stainless-Lang") == "python"
161file = response.parse()
162assert_matches_type(FileObject, file, path=["response"])
163
164@parametrize
165async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
166file = await client.files.retrieve(
167"string",
168)
169assert_matches_type(FileObject, file, path=["response"])
170
171@parametrize
172async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
173response = await client.files.with_raw_response.retrieve(
174"string",
175)
176assert response.http_request.headers.get("X-Stainless-Lang") == "python"
177file = response.parse()
178assert_matches_type(FileObject, file, path=["response"])
179
180@parametrize
181async def test_method_list(self, client: AsyncOpenAI) -> None:
182file = await client.files.list()
183assert_matches_type(AsyncPage[FileObject], file, path=["response"])
184
baa9f07fRobert Craigie2 years ago185@parametrize
186async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None:
187file = await client.files.list(
188purpose="string",
189)
190assert_matches_type(AsyncPage[FileObject], file, path=["response"])
191
08b8179aDavid Schnurr2 years ago192@parametrize
193async def test_raw_response_list(self, client: AsyncOpenAI) -> None:
194response = await client.files.with_raw_response.list()
195assert response.http_request.headers.get("X-Stainless-Lang") == "python"
196file = response.parse()
197assert_matches_type(AsyncPage[FileObject], file, path=["response"])
198
199@parametrize
200async def test_method_delete(self, client: AsyncOpenAI) -> None:
201file = await client.files.delete(
202"string",
203)
204assert_matches_type(FileDeleted, file, path=["response"])
205
206@parametrize
207async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
208response = await client.files.with_raw_response.delete(
209"string",
210)
211assert response.http_request.headers.get("X-Stainless-Lang") == "python"
212file = response.parse()
213assert_matches_type(FileDeleted, file, path=["response"])
214
c5975bd0Stainless Bot2 years ago215@pytest.mark.skip(reason="mocked response isn't working yet")
08b8179aDavid Schnurr2 years ago216@parametrize
aa681899Stainless Bot2 years ago217@pytest.mark.respx(base_url=base_url)
218async def test_method_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
219respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
220file = await client.files.content(
08b8179aDavid Schnurr2 years ago221"string",
222)
aa681899Stainless Bot2 years ago223assert isinstance(file, BinaryResponseContent)
224assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago225
c5975bd0Stainless Bot2 years ago226@pytest.mark.skip(reason="mocked response isn't working yet")
08b8179aDavid Schnurr2 years ago227@parametrize
aa681899Stainless Bot2 years ago228@pytest.mark.respx(base_url=base_url)
229async def test_raw_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
230respx_mock.get("/files/{file_id}/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
231response = await client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago232"string",
233)
234assert response.http_request.headers.get("X-Stainless-Lang") == "python"
235file = response.parse()
aa681899Stainless Bot2 years ago236assert isinstance(file, BinaryResponseContent)
237assert file.json() == {"foo": "bar"}
238
239@parametrize
240async def test_method_retrieve_content(self, client: AsyncOpenAI) -> None:
241with pytest.warns(DeprecationWarning):
242file = await client.files.retrieve_content(
243"string",
244)
245assert_matches_type(str, file, path=["response"])
246
247@parametrize
248async def test_raw_response_retrieve_content(self, client: AsyncOpenAI) -> None:
249with pytest.warns(DeprecationWarning):
250response = await client.files.with_raw_response.retrieve_content(
251"string",
252)
253assert response.http_request.headers.get("X-Stainless-Lang") == "python"
254file = response.parse()
08b8179aDavid Schnurr2 years ago255assert_matches_type(str, file, path=["response"])