Skip to content

Commit 061b503

Browse files
committed
Migrate test_integration_freemium.py
* Update legacy partition calls that don't use PartitionRequest * Update error response mock to use httpx
1 parent 494b96d commit 061b503

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

_test_unstructured_client/integration/test_integration_freemium.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77
from unstructured_client import UnstructuredClient
8-
from unstructured_client.models import shared
8+
from unstructured_client.models import shared, operations
99
from unstructured_client.models.errors import SDKError, ServerError, HTTPValidationError
1010
from unstructured_client.utils.retries import BackoffStrategy, RetryConfig
1111

@@ -31,14 +31,16 @@ def test_partition_strategies(split_pdf, strategy, client, doc_path):
3131
file_name=filename,
3232
)
3333

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+
)
3941
)
4042

41-
response = client.general.partition(req)
43+
response = client.general.partition(request=req)
4244
assert response.status_code == 200
4345
assert len(response.elements)
4446

@@ -55,20 +57,24 @@ def event_loop():
5557
@pytest.mark.parametrize("split_pdf", [True, False])
5658
@pytest.mark.parametrize("error", [(500, ServerError), (403, SDKError), (422, HTTPValidationError)])
5759
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+
"""
5863
filename = "layout-parser-paper-fast.pdf"
5964
import httpx
60-
from unstructured_client.sdkconfiguration import requests_http
6165

6266
error_code, sdk_raises = error
6367

64-
response = requests_http.Response()
65-
response.status_code = error_code
66-
response.headers = {'Content-Type': 'application/json'}
68+
# Create the mock response
6769
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+
)
6975

70-
monkeypatch.setattr(requests_http.Session, "send", lambda *args, **kwargs: response)
7176
monkeypatch.setattr(httpx.AsyncClient, "send", lambda *args, **kwargs: response)
77+
monkeypatch.setattr(httpx.Client, "send", lambda *args, **kwargs: response)
7278

7379
# initialize client after patching
7480
client = UnstructuredClient(
@@ -82,15 +88,17 @@ def test_partition_handling_server_error(error, split_pdf, monkeypatch, doc_path
8288
file_name=filename,
8389
)
8490

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+
)
9098
)
9199

92100
with pytest.raises(sdk_raises):
93-
response = client.general.partition(req)
101+
response = client.general.partition(request=req)
94102

95103

96104
def test_uvloop_partitions_without_errors(client, doc_path):
@@ -102,14 +110,16 @@ async def call_api():
102110
file_name=filename,
103111
)
104112

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+
)
110120
)
111121

112-
resp = client.general.partition(req)
122+
resp = client.general.partition(request=req)
113123

114124
if resp is not None:
115125
return resp.elements

0 commit comments

Comments
 (0)