openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6d21709610a97bd7c8575744998d0cc800e89f0c

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_files.py

184lines · modecode

1# 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:
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="string",
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="string",
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_raw_response_list(self, client: OpenAI) -> None:
65 response = client.files.with_raw_response.list()
66 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
67 file = response.parse()
68 assert_matches_type(SyncPage[FileObject], file, path=["response"])
69
70 @parametrize
71 def test_method_delete(self, client: OpenAI) -> None:
72 file = client.files.delete(
73 "string",
74 )
75 assert_matches_type(FileDeleted, file, path=["response"])
76
77 @parametrize
78 def test_raw_response_delete(self, client: OpenAI) -> None:
79 response = client.files.with_raw_response.delete(
80 "string",
81 )
82 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
83 file = response.parse()
84 assert_matches_type(FileDeleted, file, path=["response"])
85
86 @parametrize
87 def test_method_retrieve_content(self, client: OpenAI) -> None:
88 file = client.files.retrieve_content(
89 "string",
90 )
91 assert_matches_type(str, file, path=["response"])
92
93 @parametrize
94 def test_raw_response_retrieve_content(self, client: OpenAI) -> None:
95 response = client.files.with_raw_response.retrieve_content(
96 "string",
97 )
98 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
99 file = response.parse()
100 assert_matches_type(str, file, path=["response"])
101
102
103class TestAsyncFiles:
104 strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
105 loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
106 parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
107
108 @parametrize
109 async def test_method_create(self, client: AsyncOpenAI) -> None:
110 file = await client.files.create(
111 file=b"raw file contents",
112 purpose="string",
113 )
114 assert_matches_type(FileObject, file, path=["response"])
115
116 @parametrize
117 async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
118 response = await client.files.with_raw_response.create(
119 file=b"raw file contents",
120 purpose="string",
121 )
122 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
123 file = response.parse()
124 assert_matches_type(FileObject, file, path=["response"])
125
126 @parametrize
127 async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
128 file = await client.files.retrieve(
129 "string",
130 )
131 assert_matches_type(FileObject, file, path=["response"])
132
133 @parametrize
134 async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
135 response = await client.files.with_raw_response.retrieve(
136 "string",
137 )
138 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
139 file = response.parse()
140 assert_matches_type(FileObject, file, path=["response"])
141
142 @parametrize
143 async def test_method_list(self, client: AsyncOpenAI) -> None:
144 file = await client.files.list()
145 assert_matches_type(AsyncPage[FileObject], file, path=["response"])
146
147 @parametrize
148 async def test_raw_response_list(self, client: AsyncOpenAI) -> None:
149 response = await client.files.with_raw_response.list()
150 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
151 file = response.parse()
152 assert_matches_type(AsyncPage[FileObject], file, path=["response"])
153
154 @parametrize
155 async def test_method_delete(self, client: AsyncOpenAI) -> None:
156 file = await client.files.delete(
157 "string",
158 )
159 assert_matches_type(FileDeleted, file, path=["response"])
160
161 @parametrize
162 async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
163 response = await client.files.with_raw_response.delete(
164 "string",
165 )
166 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
167 file = response.parse()
168 assert_matches_type(FileDeleted, file, path=["response"])
169
170 @parametrize
171 async def test_method_retrieve_content(self, client: AsyncOpenAI) -> None:
172 file = await client.files.retrieve_content(
173 "string",
174 )
175 assert_matches_type(str, file, path=["response"])
176
177 @parametrize
178 async def test_raw_response_retrieve_content(self, client: AsyncOpenAI) -> None:
179 response = await client.files.with_raw_response.retrieve_content(
180 "string",
181 )
182 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
183 file = response.parse()
184 assert_matches_type(str, file, path=["response"])
185