5
5
6
6
import pytest
7
7
from unstructured_client import UnstructuredClient
8
- from unstructured_client .models import shared
8
+ from unstructured_client .models import shared , operations
9
9
from unstructured_client .models .errors import SDKError , ServerError , HTTPValidationError
10
10
from unstructured_client .utils .retries import BackoffStrategy , RetryConfig
11
11
@@ -31,14 +31,16 @@ def test_partition_strategies(split_pdf, strategy, client, doc_path):
31
31
file_name = filename ,
32
32
)
33
33
34
- req = shared .PartitionParameters (
35
- files = files ,
36
- strategy = strategy ,
37
- languages = ["eng" ],
38
- split_pdf_page = split_pdf ,
34
+ req = operations .PartitionRequest (
35
+ partition_parameters = shared .PartitionParameters (
36
+ files = files ,
37
+ strategy = strategy ,
38
+ languages = ["eng" ],
39
+ split_pdf_page = split_pdf ,
40
+ )
39
41
)
40
42
41
- response = client .general .partition (req )
43
+ response = client .general .partition (request = req )
42
44
assert response .status_code == 200
43
45
assert len (response .elements )
44
46
@@ -55,20 +57,24 @@ def event_loop():
55
57
@pytest .mark .parametrize ("split_pdf" , [True , False ])
56
58
@pytest .mark .parametrize ("error" , [(500 , ServerError ), (403 , SDKError ), (422 , HTTPValidationError )])
57
59
def test_partition_handling_server_error (error , split_pdf , monkeypatch , doc_path , event_loop ):
60
+ """
61
+ Mock different error responses, assert that the client throws the correct error
62
+ """
58
63
filename = "layout-parser-paper-fast.pdf"
59
64
import httpx
60
- from unstructured_client .sdkconfiguration import requests_http
61
65
62
66
error_code , sdk_raises = error
63
67
64
- response = requests_http .Response ()
65
- response .status_code = error_code
66
- response .headers = {'Content-Type' : 'application/json' }
68
+ # Create the mock response
67
69
json_data = {"detail" : "An error occurred" }
68
- response ._content = bytes (json .dumps (json_data ), 'utf-8' )
70
+ response = httpx .Response (
71
+ status_code = error_code ,
72
+ headers = {'Content-Type' : 'application/json' },
73
+ content = json .dumps (json_data )
74
+ )
69
75
70
- monkeypatch .setattr (requests_http .Session , "send" , lambda * args , ** kwargs : response )
71
76
monkeypatch .setattr (httpx .AsyncClient , "send" , lambda * args , ** kwargs : response )
77
+ monkeypatch .setattr (httpx .Client , "send" , lambda * args , ** kwargs : response )
72
78
73
79
# initialize client after patching
74
80
client = UnstructuredClient (
@@ -82,15 +88,17 @@ def test_partition_handling_server_error(error, split_pdf, monkeypatch, doc_path
82
88
file_name = filename ,
83
89
)
84
90
85
- req = shared .PartitionParameters (
86
- files = files ,
87
- strategy = "fast" ,
88
- languages = ["eng" ],
89
- split_pdf_page = split_pdf ,
91
+ req = operations .PartitionRequest (
92
+ partition_parameters = shared .PartitionParameters (
93
+ files = files ,
94
+ strategy = "fast" ,
95
+ languages = ["eng" ],
96
+ split_pdf_page = split_pdf ,
97
+ )
90
98
)
91
99
92
100
with pytest .raises (sdk_raises ):
93
- response = client .general .partition (req )
101
+ response = client .general .partition (request = req )
94
102
95
103
96
104
def test_uvloop_partitions_without_errors (client , doc_path ):
@@ -102,14 +110,16 @@ async def call_api():
102
110
file_name = filename ,
103
111
)
104
112
105
- req = shared .PartitionParameters (
106
- files = files ,
107
- strategy = "fast" ,
108
- languages = ["eng" ],
109
- split_pdf_page = True ,
113
+ req = operations .PartitionRequest (
114
+ partition_parameters = shared .PartitionParameters (
115
+ files = files ,
116
+ strategy = "fast" ,
117
+ languages = ["eng" ],
118
+ split_pdf_page = True ,
119
+ )
110
120
)
111
121
112
- resp = client .general .partition (req )
122
+ resp = client .general .partition (request = req )
113
123
114
124
if resp is not None :
115
125
return resp .elements
0 commit comments