|
| 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