openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_images.py
294lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from typing import Any, cast |
| 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: |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 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", |
| 31 | model="dall-e-2", |
| 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 | ) |
| 44 | |
| 45 | assert response.is_closed is True |
| 46 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 47 | image = response.parse() |
| 48 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 49 | |
| 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 | |
| 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", |
| 77 | model="dall-e-2", |
| 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 | ) |
| 91 | |
| 92 | assert response.is_closed is True |
| 93 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 94 | image = response.parse() |
| 95 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 96 | |
| 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 | |
| 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", |
| 122 | model="dall-e-3", |
| 123 | n=1, |
| 124 | quality="standard", |
| 125 | response_format="url", |
| 126 | size="1024x1024", |
| 127 | style="vivid", |
| 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 | ) |
| 137 | |
| 138 | assert response.is_closed is True |
| 139 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 140 | image = response.parse() |
| 141 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 142 | |
| 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 | |
| 156 | |
| 157 | class TestAsyncImages: |
| 158 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 159 | |
| 160 | @parametrize |
| 161 | async def test_method_create_variation(self, async_client: AsyncOpenAI) -> None: |
| 162 | image = await async_client.images.create_variation( |
| 163 | image=b"raw file contents", |
| 164 | ) |
| 165 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 166 | |
| 167 | @parametrize |
| 168 | async def test_method_create_variation_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 169 | image = await async_client.images.create_variation( |
| 170 | image=b"raw file contents", |
| 171 | model="dall-e-2", |
| 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 |
| 180 | async def test_raw_response_create_variation(self, async_client: AsyncOpenAI) -> None: |
| 181 | response = await async_client.images.with_raw_response.create_variation( |
| 182 | image=b"raw file contents", |
| 183 | ) |
| 184 | |
| 185 | assert response.is_closed is True |
| 186 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 187 | image = response.parse() |
| 188 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 189 | |
| 190 | @parametrize |
| 191 | async def test_streaming_response_create_variation(self, async_client: AsyncOpenAI) -> None: |
| 192 | async with async_client.images.with_streaming_response.create_variation( |
| 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 | |
| 203 | @parametrize |
| 204 | async def test_method_edit(self, async_client: AsyncOpenAI) -> None: |
| 205 | image = await async_client.images.edit( |
| 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 |
| 212 | async def test_method_edit_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 213 | image = await async_client.images.edit( |
| 214 | image=b"raw file contents", |
| 215 | prompt="A cute baby sea otter wearing a beret", |
| 216 | mask=b"raw file contents", |
| 217 | model="dall-e-2", |
| 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 |
| 226 | async def test_raw_response_edit(self, async_client: AsyncOpenAI) -> None: |
| 227 | response = await async_client.images.with_raw_response.edit( |
| 228 | image=b"raw file contents", |
| 229 | prompt="A cute baby sea otter wearing a beret", |
| 230 | ) |
| 231 | |
| 232 | assert response.is_closed is True |
| 233 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 234 | image = response.parse() |
| 235 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 236 | |
| 237 | @parametrize |
| 238 | async def test_streaming_response_edit(self, async_client: AsyncOpenAI) -> None: |
| 239 | async with async_client.images.with_streaming_response.edit( |
| 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 | |
| 251 | @parametrize |
| 252 | async def test_method_generate(self, async_client: AsyncOpenAI) -> None: |
| 253 | image = await async_client.images.generate( |
| 254 | prompt="A cute baby sea otter", |
| 255 | ) |
| 256 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 257 | |
| 258 | @parametrize |
| 259 | async def test_method_generate_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 260 | image = await async_client.images.generate( |
| 261 | prompt="A cute baby sea otter", |
| 262 | model="dall-e-3", |
| 263 | n=1, |
| 264 | quality="standard", |
| 265 | response_format="url", |
| 266 | size="1024x1024", |
| 267 | style="vivid", |
| 268 | user="user-1234", |
| 269 | ) |
| 270 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 271 | |
| 272 | @parametrize |
| 273 | async def test_raw_response_generate(self, async_client: AsyncOpenAI) -> None: |
| 274 | response = await async_client.images.with_raw_response.generate( |
| 275 | prompt="A cute baby sea otter", |
| 276 | ) |
| 277 | |
| 278 | assert response.is_closed is True |
| 279 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 280 | image = response.parse() |
| 281 | assert_matches_type(ImagesResponse, image, path=["response"]) |
| 282 | |
| 283 | @parametrize |
| 284 | async def test_streaming_response_generate(self, async_client: AsyncOpenAI) -> None: |
| 285 | async with async_client.images.with_streaming_response.generate( |
| 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 |