main
  1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3from __future__ import annotations
  4
  5import os
  6from typing import Any, cast
  7
  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:
 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="string",
 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_overload_1(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_overload_1(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            background="transparent",
 77            input_fidelity="high",
 78            mask=b"raw file contents",
 79            model="string",
 80            n=1,
 81            output_compression=100,
 82            output_format="png",
 83            partial_images=1,
 84            quality="high",
 85            response_format="url",
 86            size="1024x1024",
 87            stream=False,
 88            user="user-1234",
 89        )
 90        assert_matches_type(ImagesResponse, image, path=["response"])
 91
 92    @parametrize
 93    def test_raw_response_edit_overload_1(self, client: OpenAI) -> None:
 94        response = client.images.with_raw_response.edit(
 95            image=b"raw file contents",
 96            prompt="A cute baby sea otter wearing a beret",
 97        )
 98
 99        assert response.is_closed is True
100        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
101        image = response.parse()
102        assert_matches_type(ImagesResponse, image, path=["response"])
103
104    @parametrize
105    def test_streaming_response_edit_overload_1(self, client: OpenAI) -> None:
106        with client.images.with_streaming_response.edit(
107            image=b"raw file contents",
108            prompt="A cute baby sea otter wearing a beret",
109        ) as response:
110            assert not response.is_closed
111            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
112
113            image = response.parse()
114            assert_matches_type(ImagesResponse, image, path=["response"])
115
116        assert cast(Any, response.is_closed) is True
117
118    @parametrize
119    def test_method_edit_overload_2(self, client: OpenAI) -> None:
120        image_stream = client.images.edit(
121            image=b"raw file contents",
122            prompt="A cute baby sea otter wearing a beret",
123            stream=True,
124        )
125        image_stream.response.close()
126
127    @parametrize
128    def test_method_edit_with_all_params_overload_2(self, client: OpenAI) -> None:
129        image_stream = client.images.edit(
130            image=b"raw file contents",
131            prompt="A cute baby sea otter wearing a beret",
132            stream=True,
133            background="transparent",
134            input_fidelity="high",
135            mask=b"raw file contents",
136            model="string",
137            n=1,
138            output_compression=100,
139            output_format="png",
140            partial_images=1,
141            quality="high",
142            response_format="url",
143            size="1024x1024",
144            user="user-1234",
145        )
146        image_stream.response.close()
147
148    @parametrize
149    def test_raw_response_edit_overload_2(self, client: OpenAI) -> None:
150        response = client.images.with_raw_response.edit(
151            image=b"raw file contents",
152            prompt="A cute baby sea otter wearing a beret",
153            stream=True,
154        )
155
156        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
157        stream = response.parse()
158        stream.close()
159
160    @parametrize
161    def test_streaming_response_edit_overload_2(self, client: OpenAI) -> None:
162        with client.images.with_streaming_response.edit(
163            image=b"raw file contents",
164            prompt="A cute baby sea otter wearing a beret",
165            stream=True,
166        ) as response:
167            assert not response.is_closed
168            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
169
170            stream = response.parse()
171            stream.close()
172
173        assert cast(Any, response.is_closed) is True
174
175    @parametrize
176    def test_method_generate_overload_1(self, client: OpenAI) -> None:
177        image = client.images.generate(
178            prompt="A cute baby sea otter",
179        )
180        assert_matches_type(ImagesResponse, image, path=["response"])
181
182    @parametrize
183    def test_method_generate_with_all_params_overload_1(self, client: OpenAI) -> None:
184        image = client.images.generate(
185            prompt="A cute baby sea otter",
186            background="transparent",
187            model="string",
188            moderation="low",
189            n=1,
190            output_compression=100,
191            output_format="png",
192            partial_images=1,
193            quality="medium",
194            response_format="url",
195            size="1024x1024",
196            stream=False,
197            style="vivid",
198            user="user-1234",
199        )
200        assert_matches_type(ImagesResponse, image, path=["response"])
201
202    @parametrize
203    def test_raw_response_generate_overload_1(self, client: OpenAI) -> None:
204        response = client.images.with_raw_response.generate(
205            prompt="A cute baby sea otter",
206        )
207
208        assert response.is_closed is True
209        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
210        image = response.parse()
211        assert_matches_type(ImagesResponse, image, path=["response"])
212
213    @parametrize
214    def test_streaming_response_generate_overload_1(self, client: OpenAI) -> None:
215        with client.images.with_streaming_response.generate(
216            prompt="A cute baby sea otter",
217        ) as response:
218            assert not response.is_closed
219            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
220
221            image = response.parse()
222            assert_matches_type(ImagesResponse, image, path=["response"])
223
224        assert cast(Any, response.is_closed) is True
225
226    @parametrize
227    def test_method_generate_overload_2(self, client: OpenAI) -> None:
228        image_stream = client.images.generate(
229            prompt="A cute baby sea otter",
230            stream=True,
231        )
232        image_stream.response.close()
233
234    @parametrize
235    def test_method_generate_with_all_params_overload_2(self, client: OpenAI) -> None:
236        image_stream = client.images.generate(
237            prompt="A cute baby sea otter",
238            stream=True,
239            background="transparent",
240            model="string",
241            moderation="low",
242            n=1,
243            output_compression=100,
244            output_format="png",
245            partial_images=1,
246            quality="medium",
247            response_format="url",
248            size="1024x1024",
249            style="vivid",
250            user="user-1234",
251        )
252        image_stream.response.close()
253
254    @parametrize
255    def test_raw_response_generate_overload_2(self, client: OpenAI) -> None:
256        response = client.images.with_raw_response.generate(
257            prompt="A cute baby sea otter",
258            stream=True,
259        )
260
261        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
262        stream = response.parse()
263        stream.close()
264
265    @parametrize
266    def test_streaming_response_generate_overload_2(self, client: OpenAI) -> None:
267        with client.images.with_streaming_response.generate(
268            prompt="A cute baby sea otter",
269            stream=True,
270        ) as response:
271            assert not response.is_closed
272            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
273
274            stream = response.parse()
275            stream.close()
276
277        assert cast(Any, response.is_closed) is True
278
279
280class TestAsyncImages:
281    parametrize = pytest.mark.parametrize(
282        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
283    )
284
285    @parametrize
286    async def test_method_create_variation(self, async_client: AsyncOpenAI) -> None:
287        image = await async_client.images.create_variation(
288            image=b"raw file contents",
289        )
290        assert_matches_type(ImagesResponse, image, path=["response"])
291
292    @parametrize
293    async def test_method_create_variation_with_all_params(self, async_client: AsyncOpenAI) -> None:
294        image = await async_client.images.create_variation(
295            image=b"raw file contents",
296            model="string",
297            n=1,
298            response_format="url",
299            size="1024x1024",
300            user="user-1234",
301        )
302        assert_matches_type(ImagesResponse, image, path=["response"])
303
304    @parametrize
305    async def test_raw_response_create_variation(self, async_client: AsyncOpenAI) -> None:
306        response = await async_client.images.with_raw_response.create_variation(
307            image=b"raw file contents",
308        )
309
310        assert response.is_closed is True
311        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
312        image = response.parse()
313        assert_matches_type(ImagesResponse, image, path=["response"])
314
315    @parametrize
316    async def test_streaming_response_create_variation(self, async_client: AsyncOpenAI) -> None:
317        async with async_client.images.with_streaming_response.create_variation(
318            image=b"raw file contents",
319        ) as response:
320            assert not response.is_closed
321            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
322
323            image = await response.parse()
324            assert_matches_type(ImagesResponse, image, path=["response"])
325
326        assert cast(Any, response.is_closed) is True
327
328    @parametrize
329    async def test_method_edit_overload_1(self, async_client: AsyncOpenAI) -> None:
330        image = await async_client.images.edit(
331            image=b"raw file contents",
332            prompt="A cute baby sea otter wearing a beret",
333        )
334        assert_matches_type(ImagesResponse, image, path=["response"])
335
336    @parametrize
337    async def test_method_edit_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None:
338        image = await async_client.images.edit(
339            image=b"raw file contents",
340            prompt="A cute baby sea otter wearing a beret",
341            background="transparent",
342            input_fidelity="high",
343            mask=b"raw file contents",
344            model="string",
345            n=1,
346            output_compression=100,
347            output_format="png",
348            partial_images=1,
349            quality="high",
350            response_format="url",
351            size="1024x1024",
352            stream=False,
353            user="user-1234",
354        )
355        assert_matches_type(ImagesResponse, image, path=["response"])
356
357    @parametrize
358    async def test_raw_response_edit_overload_1(self, async_client: AsyncOpenAI) -> None:
359        response = await async_client.images.with_raw_response.edit(
360            image=b"raw file contents",
361            prompt="A cute baby sea otter wearing a beret",
362        )
363
364        assert response.is_closed is True
365        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
366        image = response.parse()
367        assert_matches_type(ImagesResponse, image, path=["response"])
368
369    @parametrize
370    async def test_streaming_response_edit_overload_1(self, async_client: AsyncOpenAI) -> None:
371        async with async_client.images.with_streaming_response.edit(
372            image=b"raw file contents",
373            prompt="A cute baby sea otter wearing a beret",
374        ) as response:
375            assert not response.is_closed
376            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
377
378            image = await response.parse()
379            assert_matches_type(ImagesResponse, image, path=["response"])
380
381        assert cast(Any, response.is_closed) is True
382
383    @parametrize
384    async def test_method_edit_overload_2(self, async_client: AsyncOpenAI) -> None:
385        image_stream = await async_client.images.edit(
386            image=b"raw file contents",
387            prompt="A cute baby sea otter wearing a beret",
388            stream=True,
389        )
390        await image_stream.response.aclose()
391
392    @parametrize
393    async def test_method_edit_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None:
394        image_stream = await async_client.images.edit(
395            image=b"raw file contents",
396            prompt="A cute baby sea otter wearing a beret",
397            stream=True,
398            background="transparent",
399            input_fidelity="high",
400            mask=b"raw file contents",
401            model="string",
402            n=1,
403            output_compression=100,
404            output_format="png",
405            partial_images=1,
406            quality="high",
407            response_format="url",
408            size="1024x1024",
409            user="user-1234",
410        )
411        await image_stream.response.aclose()
412
413    @parametrize
414    async def test_raw_response_edit_overload_2(self, async_client: AsyncOpenAI) -> None:
415        response = await async_client.images.with_raw_response.edit(
416            image=b"raw file contents",
417            prompt="A cute baby sea otter wearing a beret",
418            stream=True,
419        )
420
421        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
422        stream = response.parse()
423        await stream.close()
424
425    @parametrize
426    async def test_streaming_response_edit_overload_2(self, async_client: AsyncOpenAI) -> None:
427        async with async_client.images.with_streaming_response.edit(
428            image=b"raw file contents",
429            prompt="A cute baby sea otter wearing a beret",
430            stream=True,
431        ) as response:
432            assert not response.is_closed
433            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
434
435            stream = await response.parse()
436            await stream.close()
437
438        assert cast(Any, response.is_closed) is True
439
440    @parametrize
441    async def test_method_generate_overload_1(self, async_client: AsyncOpenAI) -> None:
442        image = await async_client.images.generate(
443            prompt="A cute baby sea otter",
444        )
445        assert_matches_type(ImagesResponse, image, path=["response"])
446
447    @parametrize
448    async def test_method_generate_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None:
449        image = await async_client.images.generate(
450            prompt="A cute baby sea otter",
451            background="transparent",
452            model="string",
453            moderation="low",
454            n=1,
455            output_compression=100,
456            output_format="png",
457            partial_images=1,
458            quality="medium",
459            response_format="url",
460            size="1024x1024",
461            stream=False,
462            style="vivid",
463            user="user-1234",
464        )
465        assert_matches_type(ImagesResponse, image, path=["response"])
466
467    @parametrize
468    async def test_raw_response_generate_overload_1(self, async_client: AsyncOpenAI) -> None:
469        response = await async_client.images.with_raw_response.generate(
470            prompt="A cute baby sea otter",
471        )
472
473        assert response.is_closed is True
474        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
475        image = response.parse()
476        assert_matches_type(ImagesResponse, image, path=["response"])
477
478    @parametrize
479    async def test_streaming_response_generate_overload_1(self, async_client: AsyncOpenAI) -> None:
480        async with async_client.images.with_streaming_response.generate(
481            prompt="A cute baby sea otter",
482        ) as response:
483            assert not response.is_closed
484            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
485
486            image = await response.parse()
487            assert_matches_type(ImagesResponse, image, path=["response"])
488
489        assert cast(Any, response.is_closed) is True
490
491    @parametrize
492    async def test_method_generate_overload_2(self, async_client: AsyncOpenAI) -> None:
493        image_stream = await async_client.images.generate(
494            prompt="A cute baby sea otter",
495            stream=True,
496        )
497        await image_stream.response.aclose()
498
499    @parametrize
500    async def test_method_generate_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None:
501        image_stream = await async_client.images.generate(
502            prompt="A cute baby sea otter",
503            stream=True,
504            background="transparent",
505            model="string",
506            moderation="low",
507            n=1,
508            output_compression=100,
509            output_format="png",
510            partial_images=1,
511            quality="medium",
512            response_format="url",
513            size="1024x1024",
514            style="vivid",
515            user="user-1234",
516        )
517        await image_stream.response.aclose()
518
519    @parametrize
520    async def test_raw_response_generate_overload_2(self, async_client: AsyncOpenAI) -> None:
521        response = await async_client.images.with_raw_response.generate(
522            prompt="A cute baby sea otter",
523            stream=True,
524        )
525
526        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
527        stream = response.parse()
528        await stream.close()
529
530    @parametrize
531    async def test_streaming_response_generate_overload_2(self, async_client: AsyncOpenAI) -> None:
532        async with async_client.images.with_streaming_response.generate(
533            prompt="A cute baby sea otter",
534            stream=True,
535        ) as response:
536            assert not response.is_closed
537            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
538
539            stream = await response.parse()
540            await stream.close()
541
542        assert cast(Any, response.is_closed) is True