|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | +from fastapi.testclient import TestClient |
| 6 | +from pydantic import BaseModel |
| 7 | +from unstructured_ingest.v2.interfaces import BatchFileData, BatchItem, FileData, SourceIdentifiers |
| 8 | + |
| 9 | +from unstructured_platform_plugins.etl_uvicorn.api_generator import ( |
| 10 | + EtlApiException, |
| 11 | + UsageData, |
| 12 | + wrap_in_fastapi, |
| 13 | +) |
| 14 | +from unstructured_platform_plugins.schema.filedata_meta import FileDataMeta |
| 15 | + |
| 16 | + |
| 17 | +class InvokeResponse(BaseModel): |
| 18 | + usage: list[UsageData] |
| 19 | + status_code: int |
| 20 | + filedata_meta: FileDataMeta |
| 21 | + status_code_text: Optional[str] = None |
| 22 | + output: Optional[Any] = None |
| 23 | + |
| 24 | + def generic_validation(self): |
| 25 | + assert self.status_code == 200 |
| 26 | + assert not self.status_code_text |
| 27 | + |
| 28 | + |
| 29 | +mock_file_data = [ |
| 30 | + FileData( |
| 31 | + identifier="mock file data", |
| 32 | + connector_type="CON", |
| 33 | + source_identifiers=SourceIdentifiers(filename="n", fullpath="n"), |
| 34 | + ), |
| 35 | + BatchFileData( |
| 36 | + identifier="mock file data", connector_type="CON", batch_items=[BatchItem(identifier="bid")] |
| 37 | + ), |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data] |
| 43 | +) |
| 44 | +def test_async_sample_function(file_data): |
| 45 | + from test.assets.async_typed_dict_response import async_sample_function as test_fn |
| 46 | + |
| 47 | + client = TestClient(wrap_in_fastapi(func=test_fn, plugin_id="mock_plugin")) |
| 48 | + |
| 49 | + post_body = {"file_data": file_data.model_dump(), "content": {"a": 1, "b": 2}} |
| 50 | + resp = client.post("/invoke", json=post_body) |
| 51 | + resp_content = resp.json() |
| 52 | + invoke_response = InvokeResponse.model_validate(resp_content) |
| 53 | + invoke_response.generic_validation() |
| 54 | + output = invoke_response.output |
| 55 | + assert isinstance(output, dict) |
| 56 | + assert output == {"response": {"a_out": 1, "b_out": 2}} |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + "file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data] |
| 61 | +) |
| 62 | +def test_dataclass_response(file_data): |
| 63 | + from test.assets.dataclass_response import sample_function_with_path as test_fn |
| 64 | + |
| 65 | + client = TestClient(wrap_in_fastapi(func=test_fn, plugin_id="mock_plugin")) |
| 66 | + current_path = Path(__file__) |
| 67 | + |
| 68 | + post_body = {"file_data": file_data.model_dump(), "c": 1, "b": "2", "a": str(current_path)} |
| 69 | + resp = client.post("/invoke", json=post_body) |
| 70 | + resp_content = resp.json() |
| 71 | + invoke_response = InvokeResponse.model_validate(resp_content) |
| 72 | + invoke_response.generic_validation() |
| 73 | + output = invoke_response.output |
| 74 | + assert isinstance(output, dict) |
| 75 | + assert output == { |
| 76 | + "t": "PosixPath", |
| 77 | + "exists": True, |
| 78 | + "resolved": str(current_path.resolve()), |
| 79 | + "b": "2", |
| 80 | + "c": 1, |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data] |
| 86 | +) |
| 87 | +def test_empty_input_and_output(file_data): |
| 88 | + from test.assets.empty_input_and_response import SampleClass as TestClass |
| 89 | + |
| 90 | + test_class = TestClass() |
| 91 | + client = TestClient(wrap_in_fastapi(func=test_class.do_nothing, plugin_id="mock_plugin")) |
| 92 | + |
| 93 | + resp = client.post("/invoke", json={"file_data": file_data.model_dump()}) |
| 94 | + resp_content = resp.json() |
| 95 | + invoke_response = InvokeResponse.model_validate(resp_content) |
| 96 | + invoke_response.generic_validation() |
| 97 | + output = invoke_response.output |
| 98 | + assert not output |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize( |
| 102 | + "file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data] |
| 103 | +) |
| 104 | +def test_filedata_meta(file_data): |
| 105 | + from test.assets.filedata_meta import Input |
| 106 | + from test.assets.filedata_meta import process_input as test_fn |
| 107 | + |
| 108 | + client = TestClient(wrap_in_fastapi(func=test_fn, plugin_id="mock_plugin")) |
| 109 | + |
| 110 | + post_body = {"file_data": file_data.model_dump(), "i": Input(m=15).model_dump()} |
| 111 | + resp = client.post("/invoke", json=post_body) |
| 112 | + resp_content = resp.json() |
| 113 | + invoke_response = InvokeResponse.model_validate(resp_content) |
| 114 | + invoke_response.generic_validation() |
| 115 | + filedata_meta = invoke_response.filedata_meta |
| 116 | + assert len(filedata_meta.new_records) == 15 |
| 117 | + assert filedata_meta.terminate_current |
| 118 | + assert not invoke_response.output |
| 119 | + |
| 120 | + |
| 121 | +def test_improper_function(): |
| 122 | + from test.assets.improper_function import sample_improper_function as test_fn |
| 123 | + |
| 124 | + with pytest.raises(EtlApiException): |
| 125 | + TestClient(wrap_in_fastapi(func=test_fn, plugin_id="mock_plugin")) |
0 commit comments