|
| 1 | +from typing import Any, AsyncGenerator |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from uipath.runtime.base import ( |
| 6 | + UiPathExecuteOptions, |
| 7 | + UiPathRuntimeEvent, |
| 8 | + UiPathRuntimeProtocol, |
| 9 | + UiPathRuntimeResult, |
| 10 | + UiPathRuntimeSchema, |
| 11 | + UiPathStreamOptions, |
| 12 | +) |
| 13 | +from uipath.runtime.factory import UiPathRuntimeCreatorProtocol |
| 14 | + |
| 15 | + |
| 16 | +class MockRuntime: |
| 17 | + """Mock runtime that implements UiPathRuntimeProtocol.""" |
| 18 | + |
| 19 | + async def execute( |
| 20 | + self, |
| 21 | + input: dict[str, Any] | None = None, |
| 22 | + options: UiPathExecuteOptions | None = None, |
| 23 | + ) -> UiPathRuntimeResult: |
| 24 | + return UiPathRuntimeResult(status="success", output={}) |
| 25 | + |
| 26 | + async def stream( |
| 27 | + self, |
| 28 | + input: dict[str, Any] | None = None, |
| 29 | + options: UiPathStreamOptions | None = None, |
| 30 | + ) -> AsyncGenerator[UiPathRuntimeEvent, None]: |
| 31 | + yield UiPathRuntimeResult(status="success", output={}) |
| 32 | + |
| 33 | + async def get_schema(self) -> UiPathRuntimeSchema: |
| 34 | + return UiPathRuntimeSchema( |
| 35 | + entrypoint_type="test", input_schema={}, output_schema={} |
| 36 | + ) |
| 37 | + |
| 38 | + async def dispose(self) -> None: |
| 39 | + pass |
| 40 | + |
| 41 | + |
| 42 | +class CreatorWithoutKwargs: |
| 43 | + """Implementation without kwargs.""" |
| 44 | + |
| 45 | + async def new_runtime( |
| 46 | + self, entrypoint: str, runtime_id: str |
| 47 | + ) -> UiPathRuntimeProtocol: |
| 48 | + return MockRuntime() |
| 49 | + |
| 50 | + |
| 51 | +class CreatorWithKwargs: |
| 52 | + """Implementation with kwargs.""" |
| 53 | + |
| 54 | + async def new_runtime( |
| 55 | + self, entrypoint: str, runtime_id: str, **kwargs |
| 56 | + ) -> UiPathRuntimeProtocol: |
| 57 | + return MockRuntime() |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_protocol_works_without_kwargs(): |
| 62 | + """Test protocol works with implementation that doesn't have kwargs.""" |
| 63 | + creator: UiPathRuntimeCreatorProtocol = CreatorWithoutKwargs() |
| 64 | + runtime = await creator.new_runtime("main.py", "runtime-123") |
| 65 | + assert isinstance(runtime, MockRuntime) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.asyncio |
| 69 | +async def test_protocol_works_with_kwargs_not_specified(): |
| 70 | + """Test protocol works with implementation that has kwargs.""" |
| 71 | + creator: UiPathRuntimeCreatorProtocol = CreatorWithKwargs() |
| 72 | + runtime = await creator.new_runtime("main.py", "runtime-123") |
| 73 | + assert isinstance(runtime, MockRuntime) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_protocol_works_with_kwargs_specified(): |
| 78 | + """Test protocol works with implementation that has kwargs.""" |
| 79 | + creator: UiPathRuntimeCreatorProtocol = CreatorWithKwargs() |
| 80 | + runtime = await creator.new_runtime("main.py", "runtime-123", timeout=30) |
| 81 | + assert isinstance(runtime, MockRuntime) |
0 commit comments