|
12 | 12 | from uuid import UUID, uuid4 |
13 | 13 | from fastapi.testclient import TestClient |
14 | 14 | from labthings_fastapi import logs |
| 15 | +from labthings_fastapi.invocations import LogRecordModel |
15 | 16 | from labthings_fastapi.invocation_contexts import ( |
16 | 17 | fake_invocation_context, |
17 | 18 | set_invocation_id, |
@@ -195,13 +196,36 @@ def test_add_thing_log_destination(): |
195 | 196 | assert dest[0].getMessage() == "Test Message." |
196 | 197 |
|
197 | 198 |
|
198 | | -def test_action_can_get_logs(): |
199 | | - """Check that an action can get a copy of its own logs.""" |
| 199 | +def _call_action_can_get_logs(): |
| 200 | + """Run `log_and_capture` as an action, Return the final HTTP response.""" |
200 | 201 | server = lt.ThingServer({"logging_thing": ThingThatLogs}) |
201 | 202 | with TestClient(server.app) as client: |
202 | 203 | response = client.post("/logging_thing/log_and_capture", json={"msg": "foobar"}) |
203 | 204 | response.raise_for_status() |
204 | | - invocation = poll_task(client, response.json()) |
| 205 | + return poll_task(client, response.json()) |
| 206 | + |
| 207 | + |
| 208 | +def test_action_can_get_logs(): |
| 209 | + """Check that an action can get a copy of its own logs.""" |
| 210 | + invocation = _call_action_can_get_logs() |
205 | 211 | assert invocation["status"] == "completed" |
| 212 | + # Check the logs are returned by the action itself. |
206 | 213 | expected_message = "[INFO] foobar\n[WARNING] foobar\n[ERROR] foobar\n" |
207 | 214 | assert invocation["output"] == expected_message |
| 215 | + |
| 216 | + |
| 217 | +def test_action_logs_over_http(): |
| 218 | + """Check that the action logs are sent over HTTP in JSON.""" |
| 219 | + invocation = _call_action_can_get_logs() |
| 220 | + logs = invocation["log"] |
| 221 | + assert isinstance(logs, list) |
| 222 | + assert len(logs) == 3 |
| 223 | + assert logs[0]["levelname"] == "INFO" |
| 224 | + assert logs[0]["levelno"] == 20 |
| 225 | + assert logs[1]["levelname"] == "WARNING" |
| 226 | + assert logs[1]["levelno"] == 30 |
| 227 | + assert logs[2]["levelname"] == "ERROR" |
| 228 | + assert logs[2]["levelno"] == 40 |
| 229 | + for log in logs: |
| 230 | + log_as_model = LogRecordModel(**log) |
| 231 | + assert log_as_model.message == "foobar" |
0 commit comments