openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.7.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_files.py

251lines · 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
98@parametrize
aa681899Stainless Bot2 years ago99@pytest.mark.respx(base_url=base_url)
100def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago101respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
aa681899Stainless Bot2 years ago102file = client.files.content(
08b8179aDavid Schnurr2 years ago103"string",
104)
aa681899Stainless Bot2 years ago105assert isinstance(file, BinaryResponseContent)
106assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago107
108@parametrize
aa681899Stainless Bot2 years ago109@pytest.mark.respx(base_url=base_url)
110def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago111respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
aa681899Stainless Bot2 years ago112response = client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago113"string",
114)
115assert response.http_request.headers.get("X-Stainless-Lang") == "python"
116file = response.parse()
aa681899Stainless Bot2 years ago117assert isinstance(file, BinaryResponseContent)
118assert file.json() == {"foo": "bar"}
119
120@parametrize
121def test_method_retrieve_content(self, client: OpenAI) -> None:
122with pytest.warns(DeprecationWarning):
123file = client.files.retrieve_content(
124"string",
125)
126assert_matches_type(str, file, path=["response"])
127
128@parametrize
129def test_raw_response_retrieve_content(self, client: OpenAI) -> None:
130with pytest.warns(DeprecationWarning):
131response = client.files.with_raw_response.retrieve_content(
132"string",
133)
134assert response.http_request.headers.get("X-Stainless-Lang") == "python"
135file = response.parse()
08b8179aDavid Schnurr2 years ago136assert_matches_type(str, file, path=["response"])
137
138
139class TestAsyncFiles:
140strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
141loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
142parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
143
144@parametrize
145async def test_method_create(self, client: AsyncOpenAI) -> None:
146file = await client.files.create(
147file=b"raw file contents",
baa9f07fRobert Craigie2 years ago148purpose="fine-tune",
08b8179aDavid Schnurr2 years ago149)
150assert_matches_type(FileObject, file, path=["response"])
151
152@parametrize
153async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
154response = await client.files.with_raw_response.create(
155file=b"raw file contents",
baa9f07fRobert Craigie2 years ago156purpose="fine-tune",
08b8179aDavid Schnurr2 years ago157)
158assert response.http_request.headers.get("X-Stainless-Lang") == "python"
159file = response.parse()
160assert_matches_type(FileObject, file, path=["response"])
161
162@parametrize
163async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
164file = await client.files.retrieve(
165"string",
166)
167assert_matches_type(FileObject, file, path=["response"])
168
169@parametrize
170async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
171response = await client.files.with_raw_response.retrieve(
172"string",
173)
174assert response.http_request.headers.get("X-Stainless-Lang") == "python"
175file = response.parse()
176assert_matches_type(FileObject, file, path=["response"])
177
178@parametrize
179async def test_method_list(self, client: AsyncOpenAI) -> None:
180file = await client.files.list()
181assert_matches_type(AsyncPage[FileObject], file, path=["response"])
182
baa9f07fRobert Craigie2 years ago183@parametrize
184async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None:
185file = await client.files.list(
186purpose="string",
187)
188assert_matches_type(AsyncPage[FileObject], file, path=["response"])
189
08b8179aDavid Schnurr2 years ago190@parametrize
191async def test_raw_response_list(self, client: AsyncOpenAI) -> None:
192response = await client.files.with_raw_response.list()
193assert response.http_request.headers.get("X-Stainless-Lang") == "python"
194file = response.parse()
195assert_matches_type(AsyncPage[FileObject], file, path=["response"])
196
197@parametrize
198async def test_method_delete(self, client: AsyncOpenAI) -> None:
199file = await client.files.delete(
200"string",
201)
202assert_matches_type(FileDeleted, file, path=["response"])
203
204@parametrize
205async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
206response = await client.files.with_raw_response.delete(
207"string",
208)
209assert response.http_request.headers.get("X-Stainless-Lang") == "python"
210file = response.parse()
211assert_matches_type(FileDeleted, file, path=["response"])
212
213@parametrize
aa681899Stainless Bot2 years ago214@pytest.mark.respx(base_url=base_url)
215async def test_method_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago216respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
aa681899Stainless Bot2 years ago217file = await client.files.content(
08b8179aDavid Schnurr2 years ago218"string",
219)
aa681899Stainless Bot2 years ago220assert isinstance(file, BinaryResponseContent)
221assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago222
223@parametrize
aa681899Stainless Bot2 years ago224@pytest.mark.respx(base_url=base_url)
225async def test_raw_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago226respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
aa681899Stainless Bot2 years ago227response = await client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago228"string",
229)
230assert response.http_request.headers.get("X-Stainless-Lang") == "python"
231file = response.parse()
aa681899Stainless Bot2 years ago232assert isinstance(file, BinaryResponseContent)
233assert file.json() == {"foo": "bar"}
234
235@parametrize
236async def test_method_retrieve_content(self, client: AsyncOpenAI) -> None:
237with pytest.warns(DeprecationWarning):
238file = await client.files.retrieve_content(
239"string",
240)
241assert_matches_type(str, file, path=["response"])
242
243@parametrize
244async def test_raw_response_retrieve_content(self, client: AsyncOpenAI) -> None:
245with pytest.warns(DeprecationWarning):
246response = await client.files.with_raw_response.retrieve_content(
247"string",
248)
249assert response.http_request.headers.get("X-Stainless-Lang") == "python"
250file = response.parse()
08b8179aDavid Schnurr2 years ago251assert_matches_type(str, file, path=["response"])