-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_templates.py
More file actions
145 lines (124 loc) · 4.95 KB
/
test_templates.py
File metadata and controls
145 lines (124 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from datetime import datetime
import pytest
from unstructured_client import UnstructuredClient
from unstructured_client.models import operations
from unstructured_client.models.errors import SDKError
def test_list_templates(httpx_mock, platform_client: UnstructuredClient, platform_api_url: str):
url = f"{platform_api_url}/api/v1/templates/"
httpx_mock.add_response(
method="GET",
headers={"Content-Type": "application/json"},
json={
"templates": [
{
"id": "hi_res_partition",
"name": "High Resolution Partition",
"description": "Partition documents with high resolution strategy",
"version": "1.0.0",
"nodes": [
{
"id": "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d",
"name": "partition step",
"type": "partition",
"subtype": "unstructured_api",
}
],
"edges": [],
},
{
"id": "hi_res_and_enrichment",
"name": "High Resolution and Enrichment",
"description": "Partition with enrichment",
"version": "1.0.0",
"nodes": [],
"edges": [],
},
]
},
url=url,
)
templates_response = platform_client.templates.list_templates(
request=operations.ListTemplatesRequest()
)
assert templates_response.status_code == 200
requests = httpx_mock.get_requests()
assert len(requests) == 1
request = requests[0]
assert request.method == "GET"
assert request.url == url
assert "templates" in templates_response.response_list_templates
templates = templates_response.response_list_templates["templates"]
assert len(templates) == 2
assert templates[0]["id"] == "hi_res_partition"
assert templates[0]["name"] == "High Resolution Partition"
assert templates[1]["id"] == "hi_res_and_enrichment"
def test_get_template(httpx_mock, platform_client: UnstructuredClient, platform_api_url: str):
url = f"{platform_api_url}/api/v1/templates/hi_res_partition"
httpx_mock.add_response(
method="GET",
headers={"Content-Type": "application/json"},
json={
"template": {
"id": "hi_res_partition",
"name": "High Resolution Partition",
"description": "Partition documents with high resolution strategy",
"version": "1.0.0",
"nodes": [
{
"id": "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d",
"name": "partition step",
"type": "partition",
"subtype": "unstructured_api",
"settings": {
"strategy": "fast",
"include_page_breaks": False,
},
}
],
"edges": [
{
"source_id": "00000000-0000-0000-0000-000000000001-downloader",
"destination_id": "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d",
}
],
}
},
url=url,
)
template_response = platform_client.templates.get_template(
request=operations.GetTemplateRequest(template_id="hi_res_partition")
)
assert template_response.status_code == 200
requests = httpx_mock.get_requests()
assert len(requests) == 1
request = requests[0]
assert request.method == "GET"
assert request.url == url
assert "template" in template_response.response_get_template
template = template_response.response_get_template["template"]
assert template["id"] == "hi_res_partition"
assert template["name"] == "High Resolution Partition"
assert len(template["nodes"]) == 1
assert template["nodes"][0]["id"] == "93fc2ce8-e7c8-424f-a6aa-41460fc5d35d"
def test_get_template_not_found(
httpx_mock, platform_client: UnstructuredClient, platform_api_url: str
):
url = f"{platform_api_url}/api/v1/templates/nonexistent_template"
httpx_mock.add_response(
method="GET",
status_code=404,
headers={"Content-Type": "application/json"},
json={"detail": "Template nonexistent_template not found"},
url=url,
)
with pytest.raises(SDKError) as e:
platform_client.templates.get_template(
request=operations.GetTemplateRequest(template_id="nonexistent_template")
)
assert e.value.status_code == 404
assert "API error occurred" in e.value.message
requests = httpx_mock.get_requests()
assert len(requests) == 1
request = requests[0]
assert request.method == "GET"
assert request.url == url