openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.1.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_files.py

198lines · modeblame

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