|
| 1 | +"""Test the DirectThingClient class. |
| 2 | +
|
| 3 | +This module tests inter-Thing interactions. It does not yet test exhaustively, |
| 4 | +and has been added primarily to fix #165. |
| 5 | +""" |
| 6 | + |
| 7 | +from fastapi.testclient import TestClient |
| 8 | +import pytest |
| 9 | +import labthings_fastapi as lt |
| 10 | +from labthings_fastapi.deps import DirectThingClient, direct_thing_client_class |
| 11 | +from .temp_client import poll_task |
| 12 | + |
| 13 | + |
| 14 | +class Counter(lt.Thing): |
| 15 | + ACTION_ONE_RESULT = "Action one result!" |
| 16 | + |
| 17 | + @lt.thing_action |
| 18 | + def increment(self) -> str: |
| 19 | + """An action that takes no arguments""" |
| 20 | + return self.increment_internal() |
| 21 | + |
| 22 | + def increment_internal(self) -> str: |
| 23 | + """An action that increments the counter.""" |
| 24 | + self.count += self.step |
| 25 | + return self.ACTION_ONE_RESULT |
| 26 | + |
| 27 | + step: int = lt.property(default=1) |
| 28 | + count: int = lt.property(default=0, readonly=True) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def counter_client(mocker) -> DirectThingClient: |
| 33 | + r"""Instantiate a Counter and wrap it in a DirectThingClient. |
| 34 | +
|
| 35 | + In order to make this work without a server, ``DirectThingClient`` is |
| 36 | + subclassed, and ``__init__`` is overridden. |
| 37 | + This could be done with ``mocker`` but it would be more verbose and |
| 38 | + less clear. |
| 39 | +
|
| 40 | + :param mocker: the mocker test fixture from ``pytest-mock``\ . |
| 41 | + :returns: a ``DirectThingClient`` subclass wrapping a ``Counter``\ . |
| 42 | + """ |
| 43 | + counter = Counter() |
| 44 | + counter._labthings_blocking_portal = mocker.Mock(["start_task_soon"]) |
| 45 | + |
| 46 | + CounterClient = direct_thing_client_class(Counter, "/counter") |
| 47 | + |
| 48 | + class StandaloneCounterClient(CounterClient): |
| 49 | + def __init__(self, wrapped): |
| 50 | + self._dependencies = {} |
| 51 | + self._request = mocker.Mock() |
| 52 | + self._wrapped_thing = wrapped |
| 53 | + |
| 54 | + return StandaloneCounterClient(counter) |
| 55 | + |
| 56 | + |
| 57 | +CounterDep = lt.deps.direct_thing_client_dependency(Counter, "/counter/") |
| 58 | +RawCounterDep = lt.deps.raw_thing_dependency(Counter) |
| 59 | + |
| 60 | + |
| 61 | +class Controller(lt.Thing): |
| 62 | + """Controller is used to test a real DirectThingClient in a server. |
| 63 | +
|
| 64 | + This is used by ``test_directthingclient_in_server`` to verify the |
| 65 | + client works as expected when created normally, rather than by mocking |
| 66 | + the server. |
| 67 | + """ |
| 68 | + |
| 69 | + @lt.thing_action |
| 70 | + def count_in_twos(self, counter: CounterDep) -> str: |
| 71 | + """An action that needs a Counter and uses its affordances. |
| 72 | +
|
| 73 | + This only uses methods that are part of the HTTP API, so all |
| 74 | + of these commands should work. |
| 75 | + """ |
| 76 | + counter.step = 2 |
| 77 | + assert counter.count == 0 |
| 78 | + counter.increment() |
| 79 | + assert counter.count == 2 |
| 80 | + return "success" |
| 81 | + |
| 82 | + @lt.thing_action |
| 83 | + def count_internal(self, counter: CounterDep) -> str: |
| 84 | + """An action that tries to access local-only attributes. |
| 85 | +
|
| 86 | + This previously used `pytest.raises` but that caused the test |
| 87 | + to hang, most likely because this will run in a background thread. |
| 88 | + """ |
| 89 | + try: |
| 90 | + counter.increment_internal() |
| 91 | + raise AssertionError("Expected error was not raised!") |
| 92 | + except AttributeError: |
| 93 | + # pytest.raises seems to hang. |
| 94 | + pass |
| 95 | + try: |
| 96 | + counter.count = 4 |
| 97 | + raise AssertionError("Expected error was not raised!") |
| 98 | + except AttributeError: |
| 99 | + # pytest.raises seems to hang. |
| 100 | + pass |
| 101 | + return "success" |
| 102 | + |
| 103 | + @lt.thing_action |
| 104 | + def count_raw(self, counter: RawCounterDep) -> str: |
| 105 | + """Increment the counter using a method that is not an Action.""" |
| 106 | + counter.count = 0 |
| 107 | + counter.step = -1 |
| 108 | + counter.increment_internal() |
| 109 | + assert counter.count == -1 |
| 110 | + return "success" |
| 111 | + |
| 112 | + |
| 113 | +def test_readwrite_property(counter_client): |
| 114 | + """Test a read/write property works as expected.""" |
| 115 | + counter_client.step = 2 |
| 116 | + assert counter_client.step == 2 |
| 117 | + |
| 118 | + |
| 119 | +def test_readonly_property(counter_client): |
| 120 | + """Test a read/write property works as expected.""" |
| 121 | + assert counter_client.count == 0 |
| 122 | + with pytest.raises(AttributeError): |
| 123 | + counter_client.count = 10 |
| 124 | + |
| 125 | + |
| 126 | +def test_action(counter_client): |
| 127 | + """Test we can run an action.""" |
| 128 | + assert counter_client.count == 0 |
| 129 | + counter_client.increment() |
| 130 | + assert counter_client.count == 1 |
| 131 | + |
| 132 | + |
| 133 | +def test_method(counter_client): |
| 134 | + """Methods that are not decorated as actions should be missing.""" |
| 135 | + with pytest.raises(AttributeError): |
| 136 | + counter_client.increment_internal() |
| 137 | + # Just to double-check the line above isn't a typo... |
| 138 | + counter_client._wrapped_thing.increment_internal() |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.parametrize("action", ["count_in_twos", "count_internal", "count_raw"]) |
| 142 | +def test_directthingclient_in_server(action): |
| 143 | + """Test that a Thing can depend on another Thing |
| 144 | +
|
| 145 | + This uses the internal thing client mechanism. |
| 146 | + """ |
| 147 | + server = lt.ThingServer() |
| 148 | + server.add_thing(Counter(), "/counter") |
| 149 | + server.add_thing(Controller(), "/controller") |
| 150 | + with TestClient(server.app) as client: |
| 151 | + r = client.post(f"/controller/{action}") |
| 152 | + invocation = poll_task(client, r.json()) |
| 153 | + assert invocation["status"] == "completed" |
| 154 | + assert invocation["output"] == "success" |
0 commit comments