@@ -36,6 +36,14 @@ class _GraphicEndpointSpec(NamedTuple):
3636 "tiff" : _GraphicEndpointSpec ("convert_to_tiff" , TiffPdfRestPayload ),
3737}
3838
39+ _EXPECTED_FILE_FORMATS : dict [str , tuple [str , str ]] = {
40+ "png" : ("image/png" , ".png" ),
41+ "bmp" : ("image/bmp" , ".bmp" ),
42+ "gif" : ("image/gif" , ".gif" ),
43+ "jpeg" : ("image/jpeg" , ".jpg" ),
44+ "tiff" : ("image/tiff" , ".tif" ),
45+ }
46+
3947
4048def _enumerate_color_models (
4149 payload_model : type [BasePdfRestGraphicPayload [Any ]],
@@ -108,6 +116,22 @@ def _invalid_smoothing_cases() -> list[Any]:
108116 return cases
109117
110118
119+ def _expected_file_format (label : str ) -> tuple [str , str ]:
120+ return _EXPECTED_FILE_FORMATS [label ]
121+
122+
123+ def _assert_output_files (
124+ output_files : Sequence [PdfRestFile ],
125+ * ,
126+ expected_mime : str ,
127+ expected_suffix : str ,
128+ ) -> None :
129+ assert output_files
130+ assert all (file_info .name .endswith (expected_suffix ) for file_info in output_files )
131+ assert all (file_info .type == expected_mime for file_info in output_files )
132+ assert all (file_info .size > 0 for file_info in output_files )
133+
134+
111135@pytest .fixture (scope = "module" )
112136def uploaded_20_page_pdf (
113137 pdfrest_api_key : str ,
@@ -137,8 +161,11 @@ def test_live_convert_to_png_success(
137161 resolution = 150 ,
138162 )
139163
140- assert response .output_files
141- assert all (file_info .type == "image/png" for file_info in response .output_files )
164+ _assert_output_files (
165+ response .output_files ,
166+ expected_mime = "image/png" ,
167+ expected_suffix = ".png" ,
168+ )
142169 assert str (response .input_id ) == str (uploaded .id )
143170
144171
@@ -159,8 +186,11 @@ async def test_live_async_convert_to_png_success(
159186 resolution = 150 ,
160187 )
161188
162- assert response .output_files
163- assert all (file_info .type == "image/png" for file_info in response .output_files )
189+ _assert_output_files (
190+ response .output_files ,
191+ expected_mime = "image/png" ,
192+ expected_suffix = ".png" ,
193+ )
164194 assert str (response .input_id ) == str (uploaded .id )
165195
166196
@@ -183,12 +213,17 @@ def test_live_graphic_valid_color_models(
183213 ) as client :
184214 uploaded = client .files .create_from_paths ([resource ])[0 ]
185215 client_method = getattr (client , spec .method_name )
216+ expected_mime , expected_suffix = _expected_file_format (_endpoint_label )
186217 response = client_method (
187218 uploaded ,
188219 color_model = color_model ,
189220 resolution = resolution ,
190221 )
191- assert response .output_files
222+ _assert_output_files (
223+ response .output_files ,
224+ expected_mime = expected_mime ,
225+ expected_suffix = expected_suffix ,
226+ )
192227
193228
194229@pytest .mark .asyncio
@@ -212,12 +247,17 @@ async def test_live_async_graphic_valid_color_models(
212247 ) as client :
213248 uploaded = (await client .files .create_from_paths ([resource ]))[0 ]
214249 client_method = getattr (client , spec .method_name )
250+ expected_mime , expected_suffix = _expected_file_format (_endpoint_label )
215251 response = await client_method (
216252 uploaded ,
217253 color_model = color_model ,
218254 resolution = resolution ,
219255 )
220- assert response .output_files
256+ _assert_output_files (
257+ response .output_files ,
258+ expected_mime = expected_mime ,
259+ expected_suffix = expected_suffix ,
260+ )
221261
222262
223263@pytest .mark .parametrize (
@@ -318,7 +358,11 @@ def test_live_graphic_resolution_bounds(
318358 client_method (uploaded , ** call_kwargs )
319359 else :
320360 response = client_method (uploaded , ** call_kwargs )
321- assert response .output_files
361+ _assert_output_files (
362+ response .output_files ,
363+ expected_mime = "image/png" ,
364+ expected_suffix = ".png" ,
365+ )
322366
323367
324368@pytest .mark .asyncio
@@ -363,7 +407,11 @@ async def test_live_async_graphic_resolution_bounds(
363407 await client_method (uploaded , ** call_kwargs )
364408 else :
365409 response = await client_method (uploaded , ** call_kwargs )
366- assert response .output_files
410+ _assert_output_files (
411+ response .output_files ,
412+ expected_mime = "image/png" ,
413+ expected_suffix = ".png" ,
414+ )
367415
368416
369417@pytest .mark .parametrize (
@@ -383,11 +431,16 @@ def test_live_graphic_valid_smoothing(
383431 ) as client :
384432 uploaded = client .files .create_from_paths ([resource ])[0 ]
385433 client_method = getattr (client , spec .method_name )
434+ expected_mime , expected_suffix = _expected_file_format (_endpoint_label )
386435 response = client_method (
387436 uploaded ,
388437 smoothing = smoothing_value ,
389438 )
390- assert response .output_files
439+ _assert_output_files (
440+ response .output_files ,
441+ expected_mime = expected_mime ,
442+ expected_suffix = expected_suffix ,
443+ )
391444
392445
393446@pytest .mark .asyncio
@@ -408,11 +461,16 @@ async def test_live_async_graphic_valid_smoothing(
408461 ) as client :
409462 uploaded = (await client .files .create_from_paths ([resource ]))[0 ]
410463 client_method = getattr (client , spec .method_name )
464+ expected_mime , expected_suffix = _expected_file_format (_endpoint_label )
411465 response = await client_method (
412466 uploaded ,
413467 smoothing = smoothing_value ,
414468 )
415- assert response .output_files
469+ _assert_output_files (
470+ response .output_files ,
471+ expected_mime = expected_mime ,
472+ expected_suffix = expected_suffix ,
473+ )
416474
417475
418476@pytest .mark .parametrize (
@@ -504,12 +562,10 @@ def test_live_png_page_range_variants(
504562
505563 expected_pages = _expand_page_selection (page_range , total_pages = 20 )
506564 assert len (response .output_files ) == len (expected_pages )
507- assert any (
508- file_info .name .endswith (".png" ) for file_info in response .output_files
509- )
510- assert all (
511- file_info .type == "image/png" and file_info .size > 0
512- for file_info in response .output_files
565+ _assert_output_files (
566+ response .output_files ,
567+ expected_mime = "image/png" ,
568+ expected_suffix = ".png" ,
513569 )
514570 assert str (response .input_id ) == str (uploaded_20_page_pdf .id )
515571 else :
@@ -560,12 +616,10 @@ async def test_live_async_png_page_range_variants(
560616
561617 expected_pages = _expand_page_selection (page_range , total_pages = 20 )
562618 assert len (response .output_files ) == len (expected_pages )
563- assert any (
564- file_info .name .endswith (".png" ) for file_info in response .output_files
565- )
566- assert all (
567- file_info .type == "image/png" and file_info .size > 0
568- for file_info in response .output_files
619+ _assert_output_files (
620+ response .output_files ,
621+ expected_mime = "image/png" ,
622+ expected_suffix = ".png" ,
569623 )
570624 assert str (response .input_id ) == str (uploaded_20_page_pdf .id )
571625 else :
0 commit comments