|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import unittest |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from dotenv import load_dotenv |
| 8 | +from parameterized import parameterized |
| 9 | + |
| 10 | +from unstract.sdk.ocr import OCR |
| 11 | +from unstract.sdk.tool.base import BaseTool |
| 12 | + |
| 13 | +load_dotenv() |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +def get_test_values(env_key: str) -> list[str]: |
| 19 | + values = json.loads(os.environ.get(env_key)) |
| 20 | + return values |
| 21 | + |
| 22 | + |
| 23 | +def get_env_value(env_key: str) -> str: |
| 24 | + value = os.environ.get(env_key) |
| 25 | + return value |
| 26 | + |
| 27 | + |
| 28 | +class ToolOCRTest(unittest.TestCase): |
| 29 | + class MockTool(BaseTool): |
| 30 | + def run( |
| 31 | + self, |
| 32 | + params: dict[str, Any] = {}, |
| 33 | + settings: dict[str, Any] = {}, |
| 34 | + workflow_id: str = "", |
| 35 | + ) -> None: |
| 36 | + pass |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def setUpClass(cls): |
| 40 | + cls.tool = cls.MockTool() |
| 41 | + |
| 42 | + @parameterized.expand(get_test_values("OCR_TEST_VALUES")) |
| 43 | + def test_get_ocr(self, adapter_instance_id): |
| 44 | + tool_ocr = OCR(tool=self.tool) |
| 45 | + ocr = tool_ocr.get_ocr(adapter_instance_id) |
| 46 | + result = ocr.test_connection() |
| 47 | + self.assertTrue(result) |
| 48 | + input_file = get_env_value("INPUT_FILE_PATH") |
| 49 | + output_file = get_env_value("OUTPUT_FILE_PATH") |
| 50 | + if os.path.isfile(output_file): |
| 51 | + os.remove(output_file) |
| 52 | + output = ocr.process(input_file, output_file) |
| 53 | + file_size = os.path.getsize(output_file) |
| 54 | + self.assertGreater(file_size, 0) |
| 55 | + if os.path.isfile(output_file): |
| 56 | + os.remove(output_file) |
| 57 | + with open(output_file, "w", encoding="utf-8") as f: |
| 58 | + f.write(output) |
| 59 | + f.close() |
| 60 | + file_size = os.path.getsize(output_file) |
| 61 | + self.assertGreater(file_size, 0) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + unittest.main() |
0 commit comments