Skip to content

Commit ed03371

Browse files
committed
Test for cancelled and error status.
I've added actions, and now test all possible status messages.
1 parent d62cdae commit ed03371

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

tests/test_websocket.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from fastapi.testclient import TestClient
22
import pytest
33
import labthings_fastapi as lt
4-
from labthings_fastapi.exceptions import PropertyNotObservableError
4+
from labthings_fastapi.exceptions import (
5+
PropertyNotObservableError,
6+
InvocationCancelledError,
7+
)
58

69

710
class ThingWithProperties(lt.Thing):
@@ -27,6 +30,18 @@ def increment_dataprop(self):
2730
"""Increment the data property."""
2831
self.dataprop += 1
2932

33+
@lt.thing_action
34+
def raise_error(self):
35+
r"""Raise an exception to test for error status."""
36+
self.dataprop += 1
37+
raise Exception("A deliberate failure.")
38+
39+
@lt.thing_action
40+
def cancel_myself(self):
41+
"""Increment the data property, then pretend to be cancelled."""
42+
self.dataprop += 1
43+
raise InvocationCancelledError()
44+
3045

3146
@pytest.fixture
3247
def thing():
@@ -154,19 +169,27 @@ def test_observing_action_error(thing, mocker):
154169
thing.observe_action("non_property", mocker.Mock())
155170

156171

157-
def test_observing_action_with_ws(client, ws):
172+
@pytest.mark.parametrize(
173+
argnames=["name", "final_status"],
174+
argvalues=[
175+
("increment_dataprop", "completed"),
176+
("raise_error", "error"),
177+
("cancel_myself", "cancelled"),
178+
],
179+
)
180+
def test_observing_action_with_ws(client, ws, name, final_status):
158181
"""Observe an action with a websocket, checking the status changes correctly."""
159182
# Observe the property.
160183
ws.send_json(
161184
{
162185
"messageType": "addActionObservation",
163-
"data": {"increment_dataprop": True},
186+
"data": {name: True},
164187
}
165188
)
166189
# Invoke the action (via HTTP)
167-
client.post("/thing/increment_dataprop")
190+
client.post(f"/thing/{name}")
168191
# We should see the status go through the expected sequence
169-
for expected_status in ["pending", "running", "completed"]:
192+
for expected_status in ["pending", "running", final_status]:
170193
message = ws.receive_json(mode="text")
171194
assert message["messageType"] == "actionStatus"
172195
assert message["data"]["status"] == expected_status

0 commit comments

Comments
 (0)