|
| 1 | +"""Unit tests for orchestrator-backed request and machine list methods on ORBClient.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from orb.sdk.client import ORBClient |
| 8 | +from orb.sdk.exceptions import SDKError |
| 9 | + |
| 10 | +# --------------------------------------------------------------------------- |
| 11 | +# Helpers |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | + |
| 14 | + |
| 15 | +def _initialized_sdk() -> ORBClient: |
| 16 | + sdk = ORBClient(config={"provider": "aws"}) |
| 17 | + sdk._initialized = True |
| 18 | + sdk._container = MagicMock() |
| 19 | + return sdk |
| 20 | + |
| 21 | + |
| 22 | +def _mock_container(sdk: ORBClient, orchestrator_class, orchestrator, scheduler=None): |
| 23 | + """Wire container.get() for one orchestrator and container.get_optional() for scheduler.""" |
| 24 | + |
| 25 | + def _get(cls): |
| 26 | + if cls is orchestrator_class: |
| 27 | + return orchestrator |
| 28 | + raise KeyError(cls) |
| 29 | + |
| 30 | + sdk._container.get.side_effect = _get |
| 31 | + sdk._container.get_optional.return_value = scheduler |
| 32 | + |
| 33 | + |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | +# list_requests |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | + |
| 38 | + |
| 39 | +class TestListRequests: |
| 40 | + @pytest.mark.asyncio |
| 41 | + @pytest.mark.unit |
| 42 | + async def test_list_requests_forwards_offset(self): |
| 43 | + from orb.application.services.orchestration.dtos import ( |
| 44 | + ListRequestsInput, |
| 45 | + ListRequestsOutput, |
| 46 | + ) |
| 47 | + from orb.application.services.orchestration.list_requests import ( |
| 48 | + ListRequestsOrchestrator, |
| 49 | + ) |
| 50 | + |
| 51 | + mock_orch = MagicMock() |
| 52 | + mock_orch.execute = AsyncMock(return_value=ListRequestsOutput(requests=[])) |
| 53 | + |
| 54 | + sdk = _initialized_sdk() |
| 55 | + _mock_container(sdk, ListRequestsOrchestrator, mock_orch) |
| 56 | + |
| 57 | + await sdk.list_requests(offset=5) |
| 58 | + |
| 59 | + mock_orch.execute.assert_called_once() |
| 60 | + call_input: ListRequestsInput = mock_orch.execute.call_args[0][0] |
| 61 | + assert call_input.offset == 5 |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + @pytest.mark.unit |
| 65 | + async def test_list_requests_default_offset_is_zero(self): |
| 66 | + from orb.application.services.orchestration.dtos import ( |
| 67 | + ListRequestsInput, |
| 68 | + ListRequestsOutput, |
| 69 | + ) |
| 70 | + from orb.application.services.orchestration.list_requests import ( |
| 71 | + ListRequestsOrchestrator, |
| 72 | + ) |
| 73 | + |
| 74 | + mock_orch = MagicMock() |
| 75 | + mock_orch.execute = AsyncMock(return_value=ListRequestsOutput(requests=[])) |
| 76 | + |
| 77 | + sdk = _initialized_sdk() |
| 78 | + _mock_container(sdk, ListRequestsOrchestrator, mock_orch) |
| 79 | + |
| 80 | + await sdk.list_requests() |
| 81 | + |
| 82 | + call_input: ListRequestsInput = mock_orch.execute.call_args[0][0] |
| 83 | + assert call_input.offset == 0 |
| 84 | + |
| 85 | + @pytest.mark.asyncio |
| 86 | + @pytest.mark.unit |
| 87 | + async def test_list_requests_forwards_offset_and_limit(self): |
| 88 | + from orb.application.services.orchestration.dtos import ( |
| 89 | + ListRequestsInput, |
| 90 | + ListRequestsOutput, |
| 91 | + ) |
| 92 | + from orb.application.services.orchestration.list_requests import ( |
| 93 | + ListRequestsOrchestrator, |
| 94 | + ) |
| 95 | + |
| 96 | + mock_orch = MagicMock() |
| 97 | + mock_orch.execute = AsyncMock(return_value=ListRequestsOutput(requests=[])) |
| 98 | + |
| 99 | + sdk = _initialized_sdk() |
| 100 | + _mock_container(sdk, ListRequestsOrchestrator, mock_orch) |
| 101 | + |
| 102 | + await sdk.list_requests(offset=5, limit=10) |
| 103 | + |
| 104 | + call_input: ListRequestsInput = mock_orch.execute.call_args[0][0] |
| 105 | + assert call_input.offset == 5 |
| 106 | + assert call_input.limit == 10 |
| 107 | + |
| 108 | + @pytest.mark.asyncio |
| 109 | + @pytest.mark.unit |
| 110 | + async def test_list_requests_not_initialized_raises(self): |
| 111 | + sdk = ORBClient(config={"provider": "aws"}) |
| 112 | + with pytest.raises(SDKError): |
| 113 | + await sdk.list_requests() |
| 114 | + |
| 115 | + |
| 116 | +# --------------------------------------------------------------------------- |
| 117 | +# list_machines |
| 118 | +# --------------------------------------------------------------------------- |
| 119 | + |
| 120 | + |
| 121 | +class TestListMachines: |
| 122 | + @pytest.mark.asyncio |
| 123 | + @pytest.mark.unit |
| 124 | + async def test_list_machines_forwards_offset(self): |
| 125 | + from orb.application.services.orchestration.dtos import ( |
| 126 | + ListMachinesInput, |
| 127 | + ListMachinesOutput, |
| 128 | + ) |
| 129 | + from orb.application.services.orchestration.list_machines import ( |
| 130 | + ListMachinesOrchestrator, |
| 131 | + ) |
| 132 | + |
| 133 | + mock_orch = MagicMock() |
| 134 | + mock_orch.execute = AsyncMock(return_value=ListMachinesOutput(machines=[])) |
| 135 | + |
| 136 | + sdk = _initialized_sdk() |
| 137 | + _mock_container(sdk, ListMachinesOrchestrator, mock_orch) |
| 138 | + |
| 139 | + await sdk.list_machines(offset=10) |
| 140 | + |
| 141 | + call_input: ListMachinesInput = mock_orch.execute.call_args[0][0] |
| 142 | + assert call_input.offset == 10 |
| 143 | + |
| 144 | + @pytest.mark.asyncio |
| 145 | + @pytest.mark.unit |
| 146 | + async def test_list_machines_forwards_limit(self): |
| 147 | + from orb.application.services.orchestration.dtos import ( |
| 148 | + ListMachinesInput, |
| 149 | + ListMachinesOutput, |
| 150 | + ) |
| 151 | + from orb.application.services.orchestration.list_machines import ( |
| 152 | + ListMachinesOrchestrator, |
| 153 | + ) |
| 154 | + |
| 155 | + mock_orch = MagicMock() |
| 156 | + mock_orch.execute = AsyncMock(return_value=ListMachinesOutput(machines=[])) |
| 157 | + |
| 158 | + sdk = _initialized_sdk() |
| 159 | + _mock_container(sdk, ListMachinesOrchestrator, mock_orch) |
| 160 | + |
| 161 | + await sdk.list_machines(limit=25) |
| 162 | + |
| 163 | + call_input: ListMachinesInput = mock_orch.execute.call_args[0][0] |
| 164 | + assert call_input.limit == 25 |
| 165 | + |
| 166 | + @pytest.mark.asyncio |
| 167 | + @pytest.mark.unit |
| 168 | + async def test_list_machines_default_offset_and_limit(self): |
| 169 | + from orb.application.services.orchestration.dtos import ( |
| 170 | + ListMachinesInput, |
| 171 | + ListMachinesOutput, |
| 172 | + ) |
| 173 | + from orb.application.services.orchestration.list_machines import ( |
| 174 | + ListMachinesOrchestrator, |
| 175 | + ) |
| 176 | + |
| 177 | + mock_orch = MagicMock() |
| 178 | + mock_orch.execute = AsyncMock(return_value=ListMachinesOutput(machines=[])) |
| 179 | + |
| 180 | + sdk = _initialized_sdk() |
| 181 | + _mock_container(sdk, ListMachinesOrchestrator, mock_orch) |
| 182 | + |
| 183 | + await sdk.list_machines() |
| 184 | + |
| 185 | + call_input: ListMachinesInput = mock_orch.execute.call_args[0][0] |
| 186 | + assert call_input.offset == 0 |
| 187 | + assert call_input.limit == 100 |
| 188 | + |
| 189 | + @pytest.mark.asyncio |
| 190 | + @pytest.mark.unit |
| 191 | + async def test_list_machines_not_initialized_raises(self): |
| 192 | + sdk = ORBClient(config={"provider": "aws"}) |
| 193 | + with pytest.raises(SDKError): |
| 194 | + await sdk.list_machines() |
0 commit comments