1
1
import asyncio
2
+ import json
2
3
import os
3
4
from pathlib import Path
4
5
5
6
import pytest
6
7
from unstructured_client import UnstructuredClient
7
8
from unstructured_client .models import shared
8
- from unstructured_client .models .errors . sdkerror import SDKError
9
+ from unstructured_client .models .errors import SDKError , ServerError , HTTPValidationError
9
10
from unstructured_client .utils .retries import BackoffStrategy , RetryConfig
10
11
11
12
@@ -52,14 +53,20 @@ def event_loop():
52
53
53
54
54
55
@pytest .mark .parametrize ("split_pdf" , [True , False ])
55
- @pytest .mark .parametrize ("error_code " , [500 , 403 ])
56
- def test_partition_handling_server_error (error_code , split_pdf , monkeypatch , doc_path , event_loop ):
56
+ @pytest .mark .parametrize ("error " , [( 500 , ServerError ), ( 403 , SDKError ), ( 422 , HTTPValidationError ) ])
57
+ def test_partition_handling_server_error (error , split_pdf , monkeypatch , doc_path , event_loop ):
57
58
filename = "layout-parser-paper-fast.pdf"
58
59
import httpx
59
60
from unstructured_client .sdkconfiguration import requests_http
60
61
62
+ error_code , sdk_raises = error
63
+
61
64
response = requests_http .Response ()
62
65
response .status_code = error_code
66
+ response .headers = {'Content-Type' : 'application/json' }
67
+ json_data = {"detail" : "An error occurred" }
68
+ response ._content = bytes (json .dumps (json_data ), 'utf-8' )
69
+
63
70
monkeypatch .setattr (requests_http .Session , "send" , lambda * args , ** kwargs : response )
64
71
monkeypatch .setattr (httpx .AsyncClient , "send" , lambda * args , ** kwargs : response )
65
72
@@ -82,5 +89,5 @@ def test_partition_handling_server_error(error_code, split_pdf, monkeypatch, doc
82
89
split_pdf_page = split_pdf ,
83
90
)
84
91
85
- with pytest .raises (SDKError , match = f"API error occurred: Status { error_code } " ):
92
+ with pytest .raises (sdk_raises ):
86
93
response = client .general .partition (req )
0 commit comments