|
| 1 | +import pytest |
| 2 | +from pytest_mock import MockerFixture |
| 3 | +from vbl_aquarium.models.ephys_link import EphysLinkOptions, GetManipulatorsResponse |
| 4 | + |
| 5 | +import ephys_link.back_end.server |
| 6 | +from ephys_link.back_end.platform_handler import PlatformHandler |
| 7 | +from ephys_link.back_end.server import Server |
| 8 | +from ephys_link.front_end.console import Console |
| 9 | +from tests.conftest import DUMMY_STRING, DUMMY_STRING_LIST |
| 10 | + |
| 11 | + |
| 12 | +class TestServer: |
| 13 | + @pytest.fixture |
| 14 | + def platform_handler(self, mocker: MockerFixture) -> PlatformHandler: |
| 15 | + """Fixture for mock PlatformHandler.""" |
| 16 | + return mocker.Mock(spec=PlatformHandler) |
| 17 | + |
| 18 | + @pytest.fixture |
| 19 | + def console(self, mocker: MockerFixture) -> Console: |
| 20 | + """Fixture for mock console.""" |
| 21 | + return mocker.Mock(spec=Console) |
| 22 | + |
| 23 | + @pytest.fixture |
| 24 | + def server(self, platform_handler: PlatformHandler, console: Console) -> Server: |
| 25 | + """Fixture for server.""" |
| 26 | + return Server(EphysLinkOptions(use_proxy=False), platform_handler, console) |
| 27 | + |
| 28 | + @pytest.fixture |
| 29 | + def proxy_client(self, platform_handler: PlatformHandler, console: Console) -> Server: |
| 30 | + """Fixture for server as proxy client.""" |
| 31 | + return Server(EphysLinkOptions(use_proxy=True), platform_handler, console) |
| 32 | + |
| 33 | + def test_launch_server( |
| 34 | + self, server: Server, platform_handler: PlatformHandler, console: Console, mocker: MockerFixture |
| 35 | + ) -> None: |
| 36 | + """Server should print info then start.""" |
| 37 | + # Add mocks and spies. |
| 38 | + spied_info_print = mocker.spy(console, "info_print") |
| 39 | + |
| 40 | + patched_get_display_name = mocker.patch.object(platform_handler, "get_display_name", return_value=DUMMY_STRING) |
| 41 | + |
| 42 | + # Mock out get manipulators. |
| 43 | + patched_get_manipulators = mocker.patch.object(platform_handler, "get_manipulators", new=mocker.Mock()) |
| 44 | + asyncio_loop = mocker.Mock() |
| 45 | + patched_run_until_complete = mocker.patch.object( |
| 46 | + asyncio_loop, "run_until_complete", return_value=GetManipulatorsResponse(manipulators=DUMMY_STRING_LIST) |
| 47 | + ) |
| 48 | + patched_get_event_loop = mocker.patch.object( |
| 49 | + ephys_link.back_end.server, "get_event_loop", return_value=asyncio_loop |
| 50 | + ) |
| 51 | + |
| 52 | + # Mock out run_app. |
| 53 | + mocked_run_app = mocker.patch.object(ephys_link.back_end.server, "run_app") |
| 54 | + |
| 55 | + # Act. |
| 56 | + server.launch() |
| 57 | + |
| 58 | + # Assert. |
| 59 | + patched_get_display_name.assert_called_once() |
| 60 | + patched_get_manipulators.assert_called_once() |
| 61 | + patched_run_until_complete.assert_called_once() |
| 62 | + patched_get_event_loop.assert_called_once() |
| 63 | + spied_info_print.assert_any_call("PLATFORM", platform_handler.get_display_name()) |
| 64 | + spied_info_print.assert_any_call("MANIPULATORS", str(DUMMY_STRING_LIST)) |
| 65 | + mocked_run_app.assert_called_once() |
0 commit comments