3
3
4
4
import pytest
5
5
import requests
6
- import requests_mock
6
+ import httpx
7
+ from httpx import Response , ConnectError
8
+
7
9
from _test_unstructured_client .unit_utils import FixtureRequest , Mock , method_mock
8
10
from unstructured_client import UnstructuredClient
9
- from unstructured_client .models import shared
11
+ from unstructured_client .models import shared , operations
10
12
from unstructured_client .models .errors import SDKError
11
13
from unstructured_client .utils .retries import BackoffStrategy , RetryConfig
12
14
@@ -23,23 +25,33 @@ def test_unit_retry_with_backoff_does_retry(caplog):
23
25
strategy = "backoff" , backoff = backoff_strategy , retry_connection_errors = True
24
26
)
25
27
26
- with requests_mock .Mocker () as mock :
27
- # mock a 502 status code for POST requests to the api
28
- mock .post ("https://api.unstructuredapp.io/general/v0/general" , status_code = 502 )
29
- session = UnstructuredClient (api_key_auth = FAKE_KEY )
28
+ # List to track the number of requests
29
+ # (Use a list so we can pass a reference into mock_post)
30
+ request_count = [0 ]
31
+
32
+ def mock_post (request ):
33
+ request_count [0 ] += 1
34
+ if request .url == "https://api.unstructuredapp.io/general/v0/general" and request .method == "POST" :
35
+ return Response (502 , request = request )
36
+
37
+ transport = httpx .MockTransport (mock_post )
38
+ client = httpx .Client (transport = transport )
39
+ session = UnstructuredClient (api_key_auth = FAKE_KEY , client = client )
30
40
31
- with open (filename , "rb" ) as f :
32
- files = shared .Files (content = f .read (), file_name = filename )
41
+ with open (filename , "rb" ) as f :
42
+ files = shared .Files (content = f .read (), file_name = filename )
33
43
34
- req = shared .PartitionParameters (files = files )
44
+ req = operations .PartitionRequest (
45
+ partition_parameters = shared .PartitionParameters (files = files )
46
+ )
35
47
36
- with pytest .raises (Exception ) as excinfo :
37
- resp = session .general .partition (req , retries = retries )
38
- assert resp .status_code == 502
39
- assert "API error occurred" in str (excinfo .value )
48
+ with pytest .raises (Exception ) as excinfo :
49
+ resp = session .general .partition (request = req , retries = retries )
50
+ assert resp .status_code == 502
51
+ assert "API error occurred" in str (excinfo .value )
40
52
41
- # the number of retries varies
42
- assert len ( mock . request_history ) > 1
53
+ # the number of retries varies
54
+ assert request_count [ 0 ] > 1
43
55
44
56
45
57
@pytest .mark .parametrize ("status_code" , [500 , 503 ])
@@ -53,17 +65,23 @@ def test_unit_backoff_strategy_logs_retries_5XX(status_code: int, caplog):
53
65
strategy = "backoff" , backoff = backoff_strategy , retry_connection_errors = True
54
66
)
55
67
56
- with requests_mock .Mocker () as mock :
57
- # mock a 500/503 status code for POST requests to the api
58
- mock .post ("https://api.unstructuredapp.io/general/v0/general" , status_code = status_code )
59
- session = UnstructuredClient (api_key_auth = FAKE_KEY )
68
+ def mock_post (request ):
69
+ if request .url == "https://api.unstructuredapp.io/general/v0/general" and request .method == "POST" :
70
+ return Response (status_code , request = request )
60
71
61
- with open (filename , "rb" ) as f :
62
- files = shared .Files (content = f .read (), file_name = filename )
72
+ transport = httpx .MockTransport (mock_post )
73
+ client = httpx .Client (transport = transport )
74
+ session = UnstructuredClient (api_key_auth = FAKE_KEY , client = client )
75
+
76
+ with open (filename , "rb" ) as f :
77
+ files = shared .Files (content = f .read (), file_name = filename )
78
+
79
+ req = operations .PartitionRequest (
80
+ partition_parameters = shared .PartitionParameters (files = files )
81
+ )
63
82
64
- req = shared .PartitionParameters (files = files )
65
- with pytest .raises (Exception ):
66
- session .general .partition (req , retries = retries )
83
+ with pytest .raises (Exception ):
84
+ session .general .partition (request = req , retries = retries )
67
85
68
86
pattern = re .compile (f"Failed to process a request due to API server error with status code { status_code } . "
69
87
"Attempting retry number 1 after sleep." )
@@ -79,22 +97,28 @@ def test_unit_backoff_strategy_logs_retries_connection_error(caplog):
79
97
retries = RetryConfig (
80
98
strategy = "backoff" , backoff = backoff_strategy , retry_connection_errors = True
81
99
)
82
- with requests_mock .Mocker () as mock :
83
- # mock a connection error response to POST request
84
- mock .post ("https://api.unstructuredapp.io/general/v0/general" , exc = requests .exceptions .ConnectionError )
85
- session = UnstructuredClient (api_key_auth = FAKE_KEY )
86
100
87
- with open ( filename , "rb" ) as f :
88
- files = shared . Files ( content = f . read (), file_name = filename )
101
+ def mock_post ( request ) :
102
+ raise ConnectError ( "Mocked connection error" , request = request )
89
103
90
- req = shared .PartitionParameters (files = files )
91
- with pytest .raises (Exception ):
92
- session .general .partition (req , retries = retries )
104
+ transport = httpx .MockTransport (mock_post )
105
+ client = httpx .Client (transport = transport )
106
+ session = UnstructuredClient (api_key_auth = FAKE_KEY , client = client )
107
+
108
+ with open (filename , "rb" ) as f :
109
+ files = shared .Files (content = f .read (), file_name = filename )
110
+
111
+ req = operations .PartitionRequest (
112
+ partition_parameters = shared .PartitionParameters (files = files )
113
+ )
114
+
115
+ with pytest .raises (Exception ):
116
+ session .general .partition (request = req , retries = retries )
93
117
94
118
pattern = re .compile (f"Failed to process a request due to connection error .*? "
95
119
"Attempting retry number 1 after sleep." )
96
120
assert bool (pattern .search (caplog .text ))
97
-
121
+
98
122
99
123
@pytest .mark .parametrize (
100
124
"server_url" ,
@@ -166,17 +190,24 @@ def test_unit_clean_server_url_fixes_malformed_urls_with_positional_arguments(se
166
190
167
191
168
192
def test_unit_issues_warning_on_a_401 (caplog , session_ : Mock , response_ : requests .Session ):
169
- client = UnstructuredClient (api_key_auth = FAKE_KEY )
170
- session_ .return_value = response_
193
+ def mock_post (request ):
194
+ return Response (401 , request = request )
195
+
196
+ transport = httpx .MockTransport (mock_post )
197
+ client = httpx .Client (transport = transport )
198
+ session = UnstructuredClient (api_key_auth = FAKE_KEY , client = client )
199
+
171
200
filename = "_sample_docs/layout-parser-paper-fast.pdf"
172
201
with open (filename , "rb" ) as f :
173
202
files = shared .Files (content = f .read (), file_name = filename )
174
203
175
- req = shared .PartitionParameters (files = files )
204
+ req = operations .PartitionRequest (
205
+ partition_parameters = shared .PartitionParameters (files = files )
206
+ )
176
207
177
208
with pytest .raises (SDKError , match = "API error occurred: Status 401" ):
178
209
with caplog .at_level (logging .WARNING ):
179
- client .general .partition (req )
210
+ session .general .partition (request = req )
180
211
181
212
assert any (
182
213
"This API key is invalid against the paid API. If intending to use the free API, please initialize UnstructuredClient with `server='free-api'`."
0 commit comments