Skip to content

Commit b5181a0

Browse files
committed
add tests
1 parent b45ba2d commit b5181a0

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed

_test_contract/platform_api/test_jobs.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,54 @@ def test_cancel_job(httpx_mock, platform_client: UnstructuredClient, platform_ap
165165
request = requests[0]
166166
assert request.method == "POST"
167167
assert request.url == url
168+
169+
170+
def test_create_job(httpx_mock, platform_client: UnstructuredClient, platform_api_url: str):
171+
url = f"{platform_api_url}/api/v1/jobs/"
172+
173+
httpx_mock.add_response(
174+
method="POST",
175+
status_code=200,
176+
headers={"Content-Type": "application/json"},
177+
json={
178+
"created_at": "2025-06-22T11:37:21.648Z",
179+
"id": "fcdc4994-eea5-425c-91fa-e03f2bd8030d",
180+
"status": "SCHEDULED",
181+
"runtime": None,
182+
"workflow_id": "16b80fee-64dc-472d-8f26-1d7729b6423d",
183+
"workflow_name": "job-fcdc4994",
184+
"input_file_ids": ["upload-test-file-123"],
185+
"output_node_files": [
186+
{
187+
"node_id": "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d",
188+
"file_id": "upload-test-file-123",
189+
"node_type": "partition",
190+
"node_subtype": "unstructured_api",
191+
}
192+
],
193+
"job_type": "template",
194+
},
195+
url=url,
196+
)
197+
198+
create_job_response = platform_client.jobs.create_job(
199+
request=operations.CreateJobRequest(
200+
body_create_job=shared.BodyCreateJob(
201+
job_type="template",
202+
template_id="hi_res_partition",
203+
)
204+
)
205+
)
206+
assert create_job_response.status_code == 200
207+
208+
requests = httpx_mock.get_requests()
209+
assert len(requests) == 1
210+
request = requests[0]
211+
assert request.method == "POST"
212+
assert request.url == url
213+
214+
job = create_job_response.job_information
215+
assert job.id == "fcdc4994-eea5-425c-91fa-e03f2bd8030d"
216+
assert job.status == "SCHEDULED"
217+
assert job.job_type == "template"
218+
assert job.created_at == datetime.fromisoformat("2025-06-22T11:37:21.648+00:00")
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
from datetime import datetime
2+
3+
import pytest
4+
5+
from unstructured_client import UnstructuredClient
6+
from unstructured_client.models import operations
7+
from unstructured_client.models.errors import SDKError
8+
9+
10+
def test_list_templates(httpx_mock, platform_client: UnstructuredClient, platform_api_url: str):
11+
url = f"{platform_api_url}/api/v1/templates/"
12+
13+
httpx_mock.add_response(
14+
method="GET",
15+
headers={"Content-Type": "application/json"},
16+
json={
17+
"templates": [
18+
{
19+
"id": "hi_res_partition",
20+
"name": "High Resolution Partition",
21+
"description": "Partition documents with high resolution strategy",
22+
"version": "1.0.0",
23+
"nodes": [
24+
{
25+
"id": "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d",
26+
"name": "partition step",
27+
"type": "partition",
28+
"subtype": "unstructured_api",
29+
}
30+
],
31+
"edges": [],
32+
},
33+
{
34+
"id": "hi_res_and_enrichment",
35+
"name": "High Resolution and Enrichment",
36+
"description": "Partition with enrichment",
37+
"version": "1.0.0",
38+
"nodes": [],
39+
"edges": [],
40+
},
41+
]
42+
},
43+
url=url,
44+
)
45+
46+
templates_response = platform_client.templates.list_templates(
47+
request=operations.ListTemplatesRequest()
48+
)
49+
assert templates_response.status_code == 200
50+
51+
requests = httpx_mock.get_requests()
52+
assert len(requests) == 1
53+
request = requests[0]
54+
assert request.method == "GET"
55+
assert request.url == url
56+
57+
assert "templates" in templates_response.response_list_templates
58+
templates = templates_response.response_list_templates["templates"]
59+
assert len(templates) == 2
60+
assert templates[0]["id"] == "hi_res_partition"
61+
assert templates[0]["name"] == "High Resolution Partition"
62+
assert templates[1]["id"] == "hi_res_and_enrichment"
63+
64+
65+
def test_get_template(httpx_mock, platform_client: UnstructuredClient, platform_api_url: str):
66+
url = f"{platform_api_url}/api/v1/templates/hi_res_partition"
67+
68+
httpx_mock.add_response(
69+
method="GET",
70+
headers={"Content-Type": "application/json"},
71+
json={
72+
"template": {
73+
"id": "hi_res_partition",
74+
"name": "High Resolution Partition",
75+
"description": "Partition documents with high resolution strategy",
76+
"version": "1.0.0",
77+
"nodes": [
78+
{
79+
"id": "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d",
80+
"name": "partition step",
81+
"type": "partition",
82+
"subtype": "unstructured_api",
83+
"settings": {
84+
"strategy": "fast",
85+
"include_page_breaks": False,
86+
},
87+
}
88+
],
89+
"edges": [
90+
{
91+
"source_id": "00000000-0000-0000-0000-000000000001-downloader",
92+
"destination_id": "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d",
93+
}
94+
],
95+
}
96+
},
97+
url=url,
98+
)
99+
100+
template_response = platform_client.templates.get_template(
101+
request=operations.GetTemplateRequest(template_id="hi_res_partition")
102+
)
103+
assert template_response.status_code == 200
104+
105+
requests = httpx_mock.get_requests()
106+
assert len(requests) == 1
107+
request = requests[0]
108+
assert request.method == "GET"
109+
assert request.url == url
110+
111+
assert "template" in template_response.response_get_template
112+
template = template_response.response_get_template["template"]
113+
assert template["id"] == "hi_res_partition"
114+
assert template["name"] == "High Resolution Partition"
115+
assert len(template["nodes"]) == 1
116+
assert template["nodes"][0]["id"] == "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d"
117+
118+
119+
def test_get_template_not_found(
120+
httpx_mock, platform_client: UnstructuredClient, platform_api_url: str
121+
):
122+
url = f"{platform_api_url}/api/v1/templates/nonexistent_template"
123+
124+
httpx_mock.add_response(
125+
method="GET",
126+
status_code=404,
127+
headers={"Content-Type": "application/json"},
128+
json={"detail": "Template nonexistent_template not found"},
129+
url=url,
130+
)
131+
132+
with pytest.raises(SDKError) as e:
133+
platform_client.templates.get_template(
134+
request=operations.GetTemplateRequest(template_id="nonexistent_template")
135+
)
136+
137+
assert e.value.status_code == 404
138+
assert "API error occurred" in e.value.message
139+
140+
requests = httpx_mock.get_requests()
141+
assert len(requests) == 1
142+
request = requests[0]
143+
assert request.method == "GET"
144+
assert request.url == url
145+

0 commit comments

Comments
 (0)