openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_images.py
294lines · modeblame
08b8179aDavid Schnurr2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
08b8179aDavid Schnurr2 years ago | 7 | |
| 8 | import pytest | |
| 9 | | |
| 10 | from openai import OpenAI, AsyncOpenAI | |
| 11 | from tests.utils import assert_matches_type | |
| 12 | from openai.types import ImagesResponse | |
| 13 | | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 15 | | |
| 16 | | |
| 17 | class TestImages: | |
98d779fbStainless Bot2 years ago | 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 19 | |
| 20 | @parametrize | |
| 21 | def test_method_create_variation(self, client: OpenAI) -> None: | |
| 22 | image = client.images.create_variation( | |
| 23 | image=b"raw file contents", | |
| 24 | ) | |
| 25 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 26 | | |
| 27 | @parametrize | |
| 28 | def test_method_create_variation_with_all_params(self, client: OpenAI) -> None: | |
| 29 | image = client.images.create_variation( | |
| 30 | image=b"raw file contents", | |
baa9f07fRobert Craigie2 years ago | 31 | model="dall-e-2", |
08b8179aDavid Schnurr2 years ago | 32 | n=1, |
| 33 | response_format="url", | |
| 34 | size="1024x1024", | |
| 35 | user="user-1234", | |
| 36 | ) | |
| 37 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 38 | | |
| 39 | @parametrize | |
| 40 | def test_raw_response_create_variation(self, client: OpenAI) -> None: | |
| 41 | response = client.images.with_raw_response.create_variation( | |
| 42 | image=b"raw file contents", | |
| 43 | ) | |
86379b44Stainless Bot2 years ago | 44 | |
| 45 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 46 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 47 | image = response.parse() | |
| 48 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 49 | | |
86379b44Stainless Bot2 years ago | 50 | @parametrize |
| 51 | def test_streaming_response_create_variation(self, client: OpenAI) -> None: | |
| 52 | with client.images.with_streaming_response.create_variation( | |
| 53 | image=b"raw file contents", | |
| 54 | ) as response: | |
| 55 | assert not response.is_closed | |
| 56 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 57 | | |
| 58 | image = response.parse() | |
| 59 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 60 | | |
| 61 | assert cast(Any, response.is_closed) is True | |
| 62 | | |
08b8179aDavid Schnurr2 years ago | 63 | @parametrize |
| 64 | def test_method_edit(self, client: OpenAI) -> None: | |
| 65 | image = client.images.edit( | |
| 66 | image=b"raw file contents", | |
| 67 | prompt="A cute baby sea otter wearing a beret", | |
| 68 | ) | |
| 69 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 70 | | |
| 71 | @parametrize | |
| 72 | def test_method_edit_with_all_params(self, client: OpenAI) -> None: | |
| 73 | image = client.images.edit( | |
| 74 | image=b"raw file contents", | |
| 75 | prompt="A cute baby sea otter wearing a beret", | |
| 76 | mask=b"raw file contents", | |
baa9f07fRobert Craigie2 years ago | 77 | model="dall-e-2", |
08b8179aDavid Schnurr2 years ago | 78 | n=1, |
| 79 | response_format="url", | |
| 80 | size="1024x1024", | |
| 81 | user="user-1234", | |
| 82 | ) | |
| 83 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 84 | | |
| 85 | @parametrize | |
| 86 | def test_raw_response_edit(self, client: OpenAI) -> None: | |
| 87 | response = client.images.with_raw_response.edit( | |
| 88 | image=b"raw file contents", | |
| 89 | prompt="A cute baby sea otter wearing a beret", | |
| 90 | ) | |
86379b44Stainless Bot2 years ago | 91 | |
| 92 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 93 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 94 | image = response.parse() | |
| 95 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 96 | | |
86379b44Stainless Bot2 years ago | 97 | @parametrize |
| 98 | def test_streaming_response_edit(self, client: OpenAI) -> None: | |
| 99 | with client.images.with_streaming_response.edit( | |
| 100 | image=b"raw file contents", | |
| 101 | prompt="A cute baby sea otter wearing a beret", | |
| 102 | ) as response: | |
| 103 | assert not response.is_closed | |
| 104 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 105 | | |
| 106 | image = response.parse() | |
| 107 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 108 | | |
| 109 | assert cast(Any, response.is_closed) is True | |
| 110 | | |
08b8179aDavid Schnurr2 years ago | 111 | @parametrize |
| 112 | def test_method_generate(self, client: OpenAI) -> None: | |
| 113 | image = client.images.generate( | |
| 114 | prompt="A cute baby sea otter", | |
| 115 | ) | |
| 116 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 117 | | |
| 118 | @parametrize | |
| 119 | def test_method_generate_with_all_params(self, client: OpenAI) -> None: | |
| 120 | image = client.images.generate( | |
| 121 | prompt="A cute baby sea otter", | |
baa9f07fRobert Craigie2 years ago | 122 | model="dall-e-3", |
08b8179aDavid Schnurr2 years ago | 123 | n=1, |
baa9f07fRobert Craigie2 years ago | 124 | quality="standard", |
08b8179aDavid Schnurr2 years ago | 125 | response_format="url", |
| 126 | size="1024x1024", | |
baa9f07fRobert Craigie2 years ago | 127 | style="vivid", |
08b8179aDavid Schnurr2 years ago | 128 | user="user-1234", |
| 129 | ) | |
| 130 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 131 | | |
| 132 | @parametrize | |
| 133 | def test_raw_response_generate(self, client: OpenAI) -> None: | |
| 134 | response = client.images.with_raw_response.generate( | |
| 135 | prompt="A cute baby sea otter", | |
| 136 | ) | |
86379b44Stainless Bot2 years ago | 137 | |
| 138 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 139 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 140 | image = response.parse() | |
| 141 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 142 | | |
86379b44Stainless Bot2 years ago | 143 | @parametrize |
| 144 | def test_streaming_response_generate(self, client: OpenAI) -> None: | |
| 145 | with client.images.with_streaming_response.generate( | |
| 146 | prompt="A cute baby sea otter", | |
| 147 | ) as response: | |
| 148 | assert not response.is_closed | |
| 149 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 150 | | |
| 151 | image = response.parse() | |
| 152 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 153 | | |
| 154 | assert cast(Any, response.is_closed) is True | |
| 155 | | |
08b8179aDavid Schnurr2 years ago | 156 | |
| 157 | class TestAsyncImages: | |
98d779fbStainless Bot2 years ago | 158 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 159 | |
| 160 | @parametrize | |
98d779fbStainless Bot2 years ago | 161 | async def test_method_create_variation(self, async_client: AsyncOpenAI) -> None: |
| 162 | image = await async_client.images.create_variation( | |
08b8179aDavid Schnurr2 years ago | 163 | image=b"raw file contents", |
| 164 | ) | |
| 165 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 166 | | |
| 167 | @parametrize | |
98d779fbStainless Bot2 years ago | 168 | async def test_method_create_variation_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 169 | image = await async_client.images.create_variation( | |
08b8179aDavid Schnurr2 years ago | 170 | image=b"raw file contents", |
baa9f07fRobert Craigie2 years ago | 171 | model="dall-e-2", |
08b8179aDavid Schnurr2 years ago | 172 | n=1, |
| 173 | response_format="url", | |
| 174 | size="1024x1024", | |
| 175 | user="user-1234", | |
| 176 | ) | |
| 177 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 178 | | |
| 179 | @parametrize | |
98d779fbStainless Bot2 years ago | 180 | async def test_raw_response_create_variation(self, async_client: AsyncOpenAI) -> None: |
| 181 | response = await async_client.images.with_raw_response.create_variation( | |
08b8179aDavid Schnurr2 years ago | 182 | image=b"raw file contents", |
| 183 | ) | |
86379b44Stainless Bot2 years ago | 184 | |
| 185 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 186 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 187 | image = response.parse() | |
| 188 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 189 | | |
86379b44Stainless Bot2 years ago | 190 | @parametrize |
98d779fbStainless Bot2 years ago | 191 | async def test_streaming_response_create_variation(self, async_client: AsyncOpenAI) -> None: |
| 192 | async with async_client.images.with_streaming_response.create_variation( | |
86379b44Stainless Bot2 years ago | 193 | image=b"raw file contents", |
| 194 | ) as response: | |
| 195 | assert not response.is_closed | |
| 196 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 197 | | |
| 198 | image = await response.parse() | |
| 199 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 200 | | |
| 201 | assert cast(Any, response.is_closed) is True | |
| 202 | | |
08b8179aDavid Schnurr2 years ago | 203 | @parametrize |
98d779fbStainless Bot2 years ago | 204 | async def test_method_edit(self, async_client: AsyncOpenAI) -> None: |
| 205 | image = await async_client.images.edit( | |
08b8179aDavid Schnurr2 years ago | 206 | image=b"raw file contents", |
| 207 | prompt="A cute baby sea otter wearing a beret", | |
| 208 | ) | |
| 209 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 210 | | |
| 211 | @parametrize | |
98d779fbStainless Bot2 years ago | 212 | async def test_method_edit_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 213 | image = await async_client.images.edit( | |
08b8179aDavid Schnurr2 years ago | 214 | image=b"raw file contents", |
| 215 | prompt="A cute baby sea otter wearing a beret", | |
| 216 | mask=b"raw file contents", | |
baa9f07fRobert Craigie2 years ago | 217 | model="dall-e-2", |
08b8179aDavid Schnurr2 years ago | 218 | n=1, |
| 219 | response_format="url", | |
| 220 | size="1024x1024", | |
| 221 | user="user-1234", | |
| 222 | ) | |
| 223 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 224 | | |
| 225 | @parametrize | |
98d779fbStainless Bot2 years ago | 226 | async def test_raw_response_edit(self, async_client: AsyncOpenAI) -> None: |
| 227 | response = await async_client.images.with_raw_response.edit( | |
08b8179aDavid Schnurr2 years ago | 228 | image=b"raw file contents", |
| 229 | prompt="A cute baby sea otter wearing a beret", | |
| 230 | ) | |
86379b44Stainless Bot2 years ago | 231 | |
| 232 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 233 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 234 | image = response.parse() | |
| 235 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 236 | | |
86379b44Stainless Bot2 years ago | 237 | @parametrize |
98d779fbStainless Bot2 years ago | 238 | async def test_streaming_response_edit(self, async_client: AsyncOpenAI) -> None: |
| 239 | async with async_client.images.with_streaming_response.edit( | |
86379b44Stainless Bot2 years ago | 240 | image=b"raw file contents", |
| 241 | prompt="A cute baby sea otter wearing a beret", | |
| 242 | ) as response: | |
| 243 | assert not response.is_closed | |
| 244 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 245 | | |
| 246 | image = await response.parse() | |
| 247 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 248 | | |
| 249 | assert cast(Any, response.is_closed) is True | |
| 250 | | |
08b8179aDavid Schnurr2 years ago | 251 | @parametrize |
98d779fbStainless Bot2 years ago | 252 | async def test_method_generate(self, async_client: AsyncOpenAI) -> None: |
| 253 | image = await async_client.images.generate( | |
08b8179aDavid Schnurr2 years ago | 254 | prompt="A cute baby sea otter", |
| 255 | ) | |
| 256 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 257 | | |
| 258 | @parametrize | |
98d779fbStainless Bot2 years ago | 259 | async def test_method_generate_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 260 | image = await async_client.images.generate( | |
08b8179aDavid Schnurr2 years ago | 261 | prompt="A cute baby sea otter", |
baa9f07fRobert Craigie2 years ago | 262 | model="dall-e-3", |
08b8179aDavid Schnurr2 years ago | 263 | n=1, |
baa9f07fRobert Craigie2 years ago | 264 | quality="standard", |
08b8179aDavid Schnurr2 years ago | 265 | response_format="url", |
| 266 | size="1024x1024", | |
baa9f07fRobert Craigie2 years ago | 267 | style="vivid", |
08b8179aDavid Schnurr2 years ago | 268 | user="user-1234", |
| 269 | ) | |
| 270 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 271 | | |
| 272 | @parametrize | |
98d779fbStainless Bot2 years ago | 273 | async def test_raw_response_generate(self, async_client: AsyncOpenAI) -> None: |
| 274 | response = await async_client.images.with_raw_response.generate( | |
08b8179aDavid Schnurr2 years ago | 275 | prompt="A cute baby sea otter", |
| 276 | ) | |
86379b44Stainless Bot2 years ago | 277 | |
| 278 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 279 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 280 | image = response.parse() | |
| 281 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
86379b44Stainless Bot2 years ago | 282 | |
| 283 | @parametrize | |
98d779fbStainless Bot2 years ago | 284 | async def test_streaming_response_generate(self, async_client: AsyncOpenAI) -> None: |
| 285 | async with async_client.images.with_streaming_response.generate( | |
86379b44Stainless Bot2 years ago | 286 | prompt="A cute baby sea otter", |
| 287 | ) as response: | |
| 288 | assert not response.is_closed | |
| 289 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 290 | | |
| 291 | image = await response.parse() | |
| 292 | assert_matches_type(ImagesResponse, image, path=["response"]) | |
| 293 | | |
| 294 | assert cast(Any, response.is_closed) is True |