33import logging
44import re
55
6+ import httpx
67import pytest
78import requests
8- import httpx
9- from httpx import Response , ConnectError
9+ from httpx import ConnectError , Response
1010
1111from _test_unstructured_client .unit_utils import FixtureRequest , Mock , method_mock
1212from unstructured_client import UnstructuredClient
13- from unstructured_client .models import shared , operations
13+ from unstructured_client .models import operations , shared
1414from unstructured_client .models .errors import SDKError
1515from unstructured_client .utils .retries import BackoffStrategy , RetryConfig
1616
@@ -34,7 +34,10 @@ def test_unit_retry_with_backoff_does_retry(caplog):
3434
3535 def mock_post (request ):
3636 request_count [0 ] += 1
37- if request .url == "https://api.unstructuredapp.io/general/v0/general" and request .method == "POST" :
37+ if (
38+ request .url == "https://api.unstructuredapp.io/general/v0/general"
39+ and request .method == "POST"
40+ ):
3841 return Response (502 , request = request )
3942
4043 transport = httpx .MockTransport (mock_post )
@@ -69,7 +72,10 @@ def test_unit_backoff_strategy_logs_retries_5XX(status_code: int, caplog):
6972 )
7073
7174 def mock_post (request ):
72- if request .url == "https://api.unstructuredapp.io/general/v0/general" and request .method == "POST" :
75+ if (
76+ request .url == "https://api.unstructuredapp.io/general/v0/general"
77+ and request .method == "POST"
78+ ):
7379 return Response (status_code , request = request )
7480
7581 transport = httpx .MockTransport (mock_post )
@@ -83,11 +89,13 @@ def mock_post(request):
8389 partition_parameters = shared .PartitionParameters (files = files )
8490 )
8591
86- with pytest .raises (Exception ):
92+ with pytest .raises (Exception ): # noqa: B017
8793 session .general .partition (request = req , retries = retries )
8894
89- pattern = re .compile (f"Failed to process a request due to API server error with status code { status_code } . "
90- "Attempting retry number 1 after sleep." )
95+ pattern = re .compile (
96+ f"Failed to process a request due to API server error with status code { status_code } . "
97+ "Attempting retry number 1 after sleep."
98+ )
9199 assert bool (pattern .search (caplog .text ))
92100
93101
@@ -103,9 +111,11 @@ def mock_post(request):
103111 [502 , True ],
104112 [503 , True ],
105113 [504 , True ],
106- ]
114+ ],
107115)
108- def test_unit_number_of_retries_in_failed_requests (status_code : int , expect_retry : bool ):
116+ def test_unit_number_of_retries_in_failed_requests (
117+ status_code : int , expect_retry : bool
118+ ):
109119 filename = "README.md"
110120 backoff_strategy = BackoffStrategy (
111121 initial_interval = 1 , max_interval = 10 , exponent = 1.5 , max_elapsed_time = 300
@@ -115,17 +125,19 @@ def test_unit_number_of_retries_in_failed_requests(status_code: int, expect_retr
115125 )
116126
117127 number_of_requests = [0 ]
128+
118129 def mock_post (request ):
119- if request .url == "https://api.unstructuredapp.io/general/v0/general" and request .method == "POST" :
130+ if (
131+ request .url == "https://api.unstructuredapp.io/general/v0/general"
132+ and request .method == "POST"
133+ ):
120134 number_of_requests [0 ] += 1
121135 return Response (status_code , request = request )
122136
123-
124137 transport = httpx .MockTransport (mock_post )
125138 client = httpx .Client (transport = transport )
126139 session = UnstructuredClient (api_key_auth = FAKE_KEY , client = client )
127140
128-
129141 with open (filename , "rb" ) as f :
130142 files = shared .Files (content = f .read (), file_name = filename )
131143
@@ -166,11 +178,13 @@ def mock_post(request):
166178 partition_parameters = shared .PartitionParameters (files = files )
167179 )
168180
169- with pytest .raises (Exception ):
181+ with pytest .raises (Exception ): # noqa: B017
170182 session .general .partition (request = req , retries = retries )
171183
172- pattern = re .compile ("Failed to process a request due to transport error .*? "
173- "Attempting retry number 1 after sleep." )
184+ pattern = re .compile (
185+ "Failed to process a request due to transport error .*? "
186+ "Attempting retry number 1 after sleep."
187+ )
174188 assert bool (pattern .search (caplog .text ))
175189
176190
@@ -250,17 +264,26 @@ def test_unit_clean_server_url_leaves_lookalike_domains_alone(
250264 [
251265 ("http://localhost:8000" , "http://localhost:8000" ),
252266 ("localhost:8000" , "http://localhost:8000" ),
253- ("localhost:8000/general/v0/general" , "http://localhost:8000/general/v0/general" ),
254- ("http://localhost:8000/general/v0/general" , "http://localhost:8000/general/v0/general" ),
267+ (
268+ "localhost:8000/general/v0/general" ,
269+ "http://localhost:8000/general/v0/general" ,
270+ ),
271+ (
272+ "http://localhost:8000/general/v0/general" ,
273+ "http://localhost:8000/general/v0/general" ,
274+ ),
255275 ],
256276)
257- def test_unit_clean_server_url_fixes_non_unst_domain_url (server_url : str , expected_url : str ):
277+ def test_unit_clean_server_url_fixes_non_unst_domain_url (
278+ server_url : str , expected_url : str
279+ ):
258280 client = UnstructuredClient (
259281 server_url = server_url ,
260282 api_key_auth = FAKE_KEY ,
261283 )
262284 assert client .general .sdk_configuration .server_url == expected_url
263285
286+
264287@pytest .mark .parametrize (
265288 "server_url" ,
266289 [
@@ -270,7 +293,9 @@ def test_unit_clean_server_url_fixes_non_unst_domain_url(server_url: str, expect
270293 "unstructured-000mock.api.unstructuredapp.io/general/v0/general" ,
271294 ],
272295)
273- def test_unit_clean_server_url_fixes_malformed_urls_with_positional_arguments (server_url : str ):
296+ def test_unit_clean_server_url_fixes_malformed_urls_with_positional_arguments (
297+ server_url : str ,
298+ ):
274299 client = UnstructuredClient (FAKE_KEY , server_url = server_url )
275300 assert (
276301 client .general .sdk_configuration .server_url
@@ -295,11 +320,8 @@ def mock_post(request):
295320 )
296321 with pytest .raises (SDKError , match = "API error occurred: Status 401" ):
297322 session .general .partition (request = req )
298-
299- assert any (
300- "Server responded with 401"
301- in message for message in caplog .messages
302- )
323+
324+ assert any ("Server responded with 401" in message for message in caplog .messages )
303325
304326
305327# -- fixtures --------------------------------------------------------------------------------
0 commit comments