From 9aedefdb8c9b54a7bf2acb6e191e768898976104 Mon Sep 17 00:00:00 2001 From: Lucas Alencar Xisto Date: 2025年8月31日 21:33:09 -0300 Subject: [PATCH] tests(images): add coverage for optional content_filter_results on Image --- tests/test_images_missing_fields.py | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/test_images_missing_fields.py diff --git a/tests/test_images_missing_fields.py b/tests/test_images_missing_fields.py new file mode 100644 index 0000000000..3baef8ce21 --- /dev/null +++ b/tests/test_images_missing_fields.py @@ -0,0 +1,50 @@ +import httpx +import pytest +from openai import AsyncOpenAI, DefaultAsyncHttpxClient + +@pytest.mark.anyio +async def test_images_generate_includes_content_filter_results_async(): + """ + Ensure the Image model exposes optional fields returned by the API, + specifically `content_filter_results` (keeping `revised_prompt` coverage). + """ + mock_json = { + "created": 1711111111, + "data": [ + { + "url": "https://example.test/cat.png", + "revised_prompt": "a cute cat wearing sunglasses", + "content_filter_results": { + "sexual_minors": {"filtered": False}, + "violence": {"filtered": False}, + }, + } + ], + } + + # Async handler because we'll use AsyncOpenAI (httpx.AsyncClient under the hood) + async def ahandler(request: httpx.Request) -> httpx.Response: + assert "images" in str(request.url).lower() + return httpx.Response(200, json=mock_json) + + atransport = httpx.MockTransport(ahandler) + + client = AsyncOpenAI( + api_key="test", + http_client=DefaultAsyncHttpxClient(transport=atransport), + timeout=10.0, + ) + + resp = await client.images.generate(model="gpt-image-1", prompt="cat with glasses") # type: ignore + + assert hasattr(resp, "data") and isinstance(resp.data, list) and resp.data + item = resp.data[0] + + # existing field + assert item.revised_prompt == "a cute cat wearing sunglasses" + + # new optional field + cfr = item.content_filter_results + assert isinstance(cfr, dict), f"content_filter_results should be dict, got {type(cfr)}" + assert cfr.get("violence", {}).get("filtered") is False + assert cfr.get("sexual_minors", {}).get("filtered") is False

AltStyle によって変換されたページ (->オリジナル) /