openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.10.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_images.py

294lines · modeblame

08b8179aDavid Schnurr2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
08b8179aDavid Schnurr2 years ago7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.types import ImagesResponse
13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15
16
17class TestImages:
98d779fbStainless Bot2 years ago18parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago19
20@parametrize
21def test_method_create_variation(self, client: OpenAI) -> None:
22image = client.images.create_variation(
23image=b"raw file contents",
24)
25assert_matches_type(ImagesResponse, image, path=["response"])
26
27@parametrize
28def test_method_create_variation_with_all_params(self, client: OpenAI) -> None:
29image = client.images.create_variation(
30image=b"raw file contents",
baa9f07fRobert Craigie2 years ago31model="dall-e-2",
08b8179aDavid Schnurr2 years ago32n=1,
33response_format="url",
34size="1024x1024",
35user="user-1234",
36)
37assert_matches_type(ImagesResponse, image, path=["response"])
38
39@parametrize
40def test_raw_response_create_variation(self, client: OpenAI) -> None:
41response = client.images.with_raw_response.create_variation(
42image=b"raw file contents",
43)
86379b44Stainless Bot2 years ago44
45assert response.is_closed is True
08b8179aDavid Schnurr2 years ago46assert response.http_request.headers.get("X-Stainless-Lang") == "python"
47image = response.parse()
48assert_matches_type(ImagesResponse, image, path=["response"])
49
86379b44Stainless Bot2 years ago50@parametrize
51def test_streaming_response_create_variation(self, client: OpenAI) -> None:
52with client.images.with_streaming_response.create_variation(
53image=b"raw file contents",
54) as response:
55assert not response.is_closed
56assert response.http_request.headers.get("X-Stainless-Lang") == "python"
57
58image = response.parse()
59assert_matches_type(ImagesResponse, image, path=["response"])
60
61assert cast(Any, response.is_closed) is True
62
08b8179aDavid Schnurr2 years ago63@parametrize
64def test_method_edit(self, client: OpenAI) -> None:
65image = client.images.edit(
66image=b"raw file contents",
67prompt="A cute baby sea otter wearing a beret",
68)
69assert_matches_type(ImagesResponse, image, path=["response"])
70
71@parametrize
72def test_method_edit_with_all_params(self, client: OpenAI) -> None:
73image = client.images.edit(
74image=b"raw file contents",
75prompt="A cute baby sea otter wearing a beret",
76mask=b"raw file contents",
baa9f07fRobert Craigie2 years ago77model="dall-e-2",
08b8179aDavid Schnurr2 years ago78n=1,
79response_format="url",
80size="1024x1024",
81user="user-1234",
82)
83assert_matches_type(ImagesResponse, image, path=["response"])
84
85@parametrize
86def test_raw_response_edit(self, client: OpenAI) -> None:
87response = client.images.with_raw_response.edit(
88image=b"raw file contents",
89prompt="A cute baby sea otter wearing a beret",
90)
86379b44Stainless Bot2 years ago91
92assert response.is_closed is True
08b8179aDavid Schnurr2 years ago93assert response.http_request.headers.get("X-Stainless-Lang") == "python"
94image = response.parse()
95assert_matches_type(ImagesResponse, image, path=["response"])
96
86379b44Stainless Bot2 years ago97@parametrize
98def test_streaming_response_edit(self, client: OpenAI) -> None:
99with client.images.with_streaming_response.edit(
100image=b"raw file contents",
101prompt="A cute baby sea otter wearing a beret",
102) as response:
103assert not response.is_closed
104assert response.http_request.headers.get("X-Stainless-Lang") == "python"
105
106image = response.parse()
107assert_matches_type(ImagesResponse, image, path=["response"])
108
109assert cast(Any, response.is_closed) is True
110
08b8179aDavid Schnurr2 years ago111@parametrize
112def test_method_generate(self, client: OpenAI) -> None:
113image = client.images.generate(
114prompt="A cute baby sea otter",
115)
116assert_matches_type(ImagesResponse, image, path=["response"])
117
118@parametrize
119def test_method_generate_with_all_params(self, client: OpenAI) -> None:
120image = client.images.generate(
121prompt="A cute baby sea otter",
baa9f07fRobert Craigie2 years ago122model="dall-e-3",
08b8179aDavid Schnurr2 years ago123n=1,
baa9f07fRobert Craigie2 years ago124quality="standard",
08b8179aDavid Schnurr2 years ago125response_format="url",
126size="1024x1024",
baa9f07fRobert Craigie2 years ago127style="vivid",
08b8179aDavid Schnurr2 years ago128user="user-1234",
129)
130assert_matches_type(ImagesResponse, image, path=["response"])
131
132@parametrize
133def test_raw_response_generate(self, client: OpenAI) -> None:
134response = client.images.with_raw_response.generate(
135prompt="A cute baby sea otter",
136)
86379b44Stainless Bot2 years ago137
138assert response.is_closed is True
08b8179aDavid Schnurr2 years ago139assert response.http_request.headers.get("X-Stainless-Lang") == "python"
140image = response.parse()
141assert_matches_type(ImagesResponse, image, path=["response"])
142
86379b44Stainless Bot2 years ago143@parametrize
144def test_streaming_response_generate(self, client: OpenAI) -> None:
145with client.images.with_streaming_response.generate(
146prompt="A cute baby sea otter",
147) as response:
148assert not response.is_closed
149assert response.http_request.headers.get("X-Stainless-Lang") == "python"
150
151image = response.parse()
152assert_matches_type(ImagesResponse, image, path=["response"])
153
154assert cast(Any, response.is_closed) is True
155
08b8179aDavid Schnurr2 years ago156
157class TestAsyncImages:
98d779fbStainless Bot2 years ago158parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago159
160@parametrize
98d779fbStainless Bot2 years ago161async def test_method_create_variation(self, async_client: AsyncOpenAI) -> None:
162image = await async_client.images.create_variation(
08b8179aDavid Schnurr2 years ago163image=b"raw file contents",
164)
165assert_matches_type(ImagesResponse, image, path=["response"])
166
167@parametrize
98d779fbStainless Bot2 years ago168async def test_method_create_variation_with_all_params(self, async_client: AsyncOpenAI) -> None:
169image = await async_client.images.create_variation(
08b8179aDavid Schnurr2 years ago170image=b"raw file contents",
baa9f07fRobert Craigie2 years ago171model="dall-e-2",
08b8179aDavid Schnurr2 years ago172n=1,
173response_format="url",
174size="1024x1024",
175user="user-1234",
176)
177assert_matches_type(ImagesResponse, image, path=["response"])
178
179@parametrize
98d779fbStainless Bot2 years ago180async def test_raw_response_create_variation(self, async_client: AsyncOpenAI) -> None:
181response = await async_client.images.with_raw_response.create_variation(
08b8179aDavid Schnurr2 years ago182image=b"raw file contents",
183)
86379b44Stainless Bot2 years ago184
185assert response.is_closed is True
08b8179aDavid Schnurr2 years ago186assert response.http_request.headers.get("X-Stainless-Lang") == "python"
187image = response.parse()
188assert_matches_type(ImagesResponse, image, path=["response"])
189
86379b44Stainless Bot2 years ago190@parametrize
98d779fbStainless Bot2 years ago191async def test_streaming_response_create_variation(self, async_client: AsyncOpenAI) -> None:
192async with async_client.images.with_streaming_response.create_variation(
86379b44Stainless Bot2 years ago193image=b"raw file contents",
194) as response:
195assert not response.is_closed
196assert response.http_request.headers.get("X-Stainless-Lang") == "python"
197
198image = await response.parse()
199assert_matches_type(ImagesResponse, image, path=["response"])
200
201assert cast(Any, response.is_closed) is True
202
08b8179aDavid Schnurr2 years ago203@parametrize
98d779fbStainless Bot2 years ago204async def test_method_edit(self, async_client: AsyncOpenAI) -> None:
205image = await async_client.images.edit(
08b8179aDavid Schnurr2 years ago206image=b"raw file contents",
207prompt="A cute baby sea otter wearing a beret",
208)
209assert_matches_type(ImagesResponse, image, path=["response"])
210
211@parametrize
98d779fbStainless Bot2 years ago212async def test_method_edit_with_all_params(self, async_client: AsyncOpenAI) -> None:
213image = await async_client.images.edit(
08b8179aDavid Schnurr2 years ago214image=b"raw file contents",
215prompt="A cute baby sea otter wearing a beret",
216mask=b"raw file contents",
baa9f07fRobert Craigie2 years ago217model="dall-e-2",
08b8179aDavid Schnurr2 years ago218n=1,
219response_format="url",
220size="1024x1024",
221user="user-1234",
222)
223assert_matches_type(ImagesResponse, image, path=["response"])
224
225@parametrize
98d779fbStainless Bot2 years ago226async def test_raw_response_edit(self, async_client: AsyncOpenAI) -> None:
227response = await async_client.images.with_raw_response.edit(
08b8179aDavid Schnurr2 years ago228image=b"raw file contents",
229prompt="A cute baby sea otter wearing a beret",
230)
86379b44Stainless Bot2 years ago231
232assert response.is_closed is True
08b8179aDavid Schnurr2 years ago233assert response.http_request.headers.get("X-Stainless-Lang") == "python"
234image = response.parse()
235assert_matches_type(ImagesResponse, image, path=["response"])
236
86379b44Stainless Bot2 years ago237@parametrize
98d779fbStainless Bot2 years ago238async def test_streaming_response_edit(self, async_client: AsyncOpenAI) -> None:
239async with async_client.images.with_streaming_response.edit(
86379b44Stainless Bot2 years ago240image=b"raw file contents",
241prompt="A cute baby sea otter wearing a beret",
242) as response:
243assert not response.is_closed
244assert response.http_request.headers.get("X-Stainless-Lang") == "python"
245
246image = await response.parse()
247assert_matches_type(ImagesResponse, image, path=["response"])
248
249assert cast(Any, response.is_closed) is True
250
08b8179aDavid Schnurr2 years ago251@parametrize
98d779fbStainless Bot2 years ago252async def test_method_generate(self, async_client: AsyncOpenAI) -> None:
253image = await async_client.images.generate(
08b8179aDavid Schnurr2 years ago254prompt="A cute baby sea otter",
255)
256assert_matches_type(ImagesResponse, image, path=["response"])
257
258@parametrize
98d779fbStainless Bot2 years ago259async def test_method_generate_with_all_params(self, async_client: AsyncOpenAI) -> None:
260image = await async_client.images.generate(
08b8179aDavid Schnurr2 years ago261prompt="A cute baby sea otter",
baa9f07fRobert Craigie2 years ago262model="dall-e-3",
08b8179aDavid Schnurr2 years ago263n=1,
baa9f07fRobert Craigie2 years ago264quality="standard",
08b8179aDavid Schnurr2 years ago265response_format="url",
266size="1024x1024",
baa9f07fRobert Craigie2 years ago267style="vivid",
08b8179aDavid Schnurr2 years ago268user="user-1234",
269)
270assert_matches_type(ImagesResponse, image, path=["response"])
271
272@parametrize
98d779fbStainless Bot2 years ago273async def test_raw_response_generate(self, async_client: AsyncOpenAI) -> None:
274response = await async_client.images.with_raw_response.generate(
08b8179aDavid Schnurr2 years ago275prompt="A cute baby sea otter",
276)
86379b44Stainless Bot2 years ago277
278assert response.is_closed is True
08b8179aDavid Schnurr2 years ago279assert response.http_request.headers.get("X-Stainless-Lang") == "python"
280image = response.parse()
281assert_matches_type(ImagesResponse, image, path=["response"])
86379b44Stainless Bot2 years ago282
283@parametrize
98d779fbStainless Bot2 years ago284async def test_streaming_response_generate(self, async_client: AsyncOpenAI) -> None:
285async with async_client.images.with_streaming_response.generate(
86379b44Stainless Bot2 years ago286prompt="A cute baby sea otter",
287) as response:
288assert not response.is_closed
289assert response.http_request.headers.get("X-Stainless-Lang") == "python"
290
291image = await response.parse()
292assert_matches_type(ImagesResponse, image, path=["response"])
293
294assert cast(Any, response.is_closed) is True