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