@@ -322,3 +322,151 @@ def test_streaming_exception_with_valid_status_code():
322322 # Should use the exception's status_code
323323 assert invoke_response .status_code == 422
324324 assert "ExceptionWithValidStatusCode" in invoke_response .status_code_text
325+
326+
327+ @pytest .mark .parametrize (
328+ "file_data" , mock_file_data , ids = [type (fd ).__name__ for fd in mock_file_data ]
329+ )
330+ def test_unstructured_ingest_error_with_status_code (file_data ):
331+ """Test that UnstructuredIngestError with status_code is handled correctly."""
332+ from test .assets .exception_status_code import (
333+ function_raises_unstructured_ingest_error_with_status_code as test_fn ,
334+ )
335+
336+ client = TestClient (wrap_in_fastapi (func = test_fn , plugin_id = "mock_plugin" ))
337+
338+ post_body = {"file_data" : file_data .model_dump ()}
339+ resp = client .post ("/invoke" , json = post_body )
340+ resp_content = resp .json ()
341+ invoke_response = InvokeResponse .model_validate (resp_content )
342+
343+ # Should use the UnstructuredIngestError's status_code
344+ assert invoke_response .status_code == 400
345+ assert invoke_response .status_code_text == "Test UnstructuredIngestError with status_code"
346+
347+
348+ @pytest .mark .parametrize (
349+ "file_data" , mock_file_data , ids = [type (fd ).__name__ for fd in mock_file_data ]
350+ )
351+ def test_unstructured_ingest_error_without_status_code (file_data ):
352+ """Test that UnstructuredIngestError without status_code defaults to 500."""
353+ from test .assets .exception_status_code import (
354+ function_raises_unstructured_ingest_error_without_status_code as test_fn ,
355+ )
356+
357+ client = TestClient (wrap_in_fastapi (func = test_fn , plugin_id = "mock_plugin" ))
358+
359+ post_body = {"file_data" : file_data .model_dump ()}
360+ resp = client .post ("/invoke" , json = post_body )
361+ resp_content = resp .json ()
362+ invoke_response = InvokeResponse .model_validate (resp_content )
363+
364+ # Should default to 500 when UnstructuredIngestError has no status_code
365+ assert invoke_response .status_code == 500
366+ assert invoke_response .status_code_text == "Test UnstructuredIngestError without status_code"
367+
368+
369+ @pytest .mark .parametrize (
370+ "file_data" , mock_file_data , ids = [type (fd ).__name__ for fd in mock_file_data ]
371+ )
372+ def test_unstructured_ingest_error_with_none_status_code (file_data ):
373+ """Test that UnstructuredIngestError with None status_code defaults to 500."""
374+ from test .assets .exception_status_code import (
375+ function_raises_unstructured_ingest_error_with_none_status_code as test_fn ,
376+ )
377+
378+ client = TestClient (wrap_in_fastapi (func = test_fn , plugin_id = "mock_plugin" ))
379+
380+ post_body = {"file_data" : file_data .model_dump ()}
381+ resp = client .post ("/invoke" , json = post_body )
382+ resp_content = resp .json ()
383+ invoke_response = InvokeResponse .model_validate (resp_content )
384+
385+ # Should default to 500 when UnstructuredIngestError status_code is None
386+ assert invoke_response .status_code == 500
387+ assert invoke_response .status_code_text == "Test UnstructuredIngestError with None status_code"
388+
389+
390+ @pytest .mark .parametrize (
391+ "file_data" , mock_file_data , ids = [type (fd ).__name__ for fd in mock_file_data ]
392+ )
393+ def test_async_unstructured_ingest_error (file_data ):
394+ """Test that async functions with UnstructuredIngestError are handled correctly."""
395+ from test .assets .exception_status_code import (
396+ async_function_raises_unstructured_ingest_error as test_fn ,
397+ )
398+
399+ client = TestClient (wrap_in_fastapi (func = test_fn , plugin_id = "mock_plugin" ))
400+
401+ post_body = {"file_data" : file_data .model_dump ()}
402+ resp = client .post ("/invoke" , json = post_body )
403+ resp_content = resp .json ()
404+ invoke_response = InvokeResponse .model_validate (resp_content )
405+
406+ # Should use the UnstructuredIngestError's status_code
407+ assert invoke_response .status_code == 503
408+ assert invoke_response .status_code_text == "Async test UnstructuredIngestError"
409+
410+
411+ def test_streaming_unstructured_ingest_error ():
412+ """Test that async generator functions with UnstructuredIngestError are handled correctly."""
413+ from test .assets .exception_status_code import (
414+ async_gen_function_raises_unstructured_ingest_error as test_fn ,
415+ )
416+
417+ client = TestClient (wrap_in_fastapi (func = test_fn , plugin_id = "mock_plugin" ))
418+
419+ post_body = {"file_data" : mock_file_data [0 ].model_dump ()}
420+ resp = client .post ("/invoke" , json = post_body )
421+
422+ # For streaming responses, we get NDJSON
423+ assert resp .status_code == 200
424+ assert resp .headers ["content-type" ] == "application/x-ndjson"
425+
426+ # Parse the streaming response - should be a single error response
427+ lines = resp .content .decode ().strip ().split ("\n " )
428+ assert len (lines ) == 1 # Only error response since no items were yielded
429+
430+ # Parse the error response
431+ import json
432+
433+ error_response = json .loads (lines [0 ])
434+ invoke_response = InvokeResponse .model_validate (error_response )
435+
436+ # Should use the UnstructuredIngestError's status_code
437+ assert invoke_response .status_code == 502
438+ assert "Async gen test UnstructuredIngestError" in invoke_response .status_code_text
439+
440+
441+ def test_streaming_unstructured_ingest_error_with_none_status_code ():
442+ """Test that async generator functions with UnstructuredIngestError
443+ with None status_code are handled correctly."""
444+ from test .assets .exception_status_code import (
445+ async_gen_function_raises_unstructured_ingest_error_with_none_status_code as test_fn ,
446+ )
447+
448+ client = TestClient (wrap_in_fastapi (func = test_fn , plugin_id = "mock_plugin" ))
449+
450+ post_body = {"file_data" : mock_file_data [0 ].model_dump ()}
451+ resp = client .post ("/invoke" , json = post_body )
452+
453+ # For streaming responses, we get NDJSON
454+ assert resp .status_code == 200
455+ assert resp .headers ["content-type" ] == "application/x-ndjson"
456+
457+ # Parse the streaming response - should be a single error response
458+ lines = resp .content .decode ().strip ().split ("\n " )
459+ assert len (lines ) == 1 # Only error response since no items were yielded
460+
461+ # Parse the error response
462+ import json
463+
464+ error_response = json .loads (lines [0 ])
465+ invoke_response = InvokeResponse .model_validate (error_response )
466+
467+ # Should default to 500 when UnstructuredIngestError status_code is None
468+ assert invoke_response .status_code == 500
469+ assert (
470+ "Async gen test UnstructuredIngestError with None status_code"
471+ in invoke_response .status_code_text
472+ )
0 commit comments