|
2 | 2 |
|
3 | 3 | from pydantic import BaseModel, RootModel |
4 | 4 | import numpy as np |
| 5 | +from fastapi.testclient import TestClient |
5 | 6 |
|
6 | 7 | from labthings_fastapi.testing import create_thing_without_server |
7 | 8 | from labthings_fastapi.types.numpy import NDArray, DenumpifyingDict |
@@ -70,6 +71,10 @@ class MyNumpyThing(lt.Thing): |
70 | 71 | def action_with_arrays(self, a: NDArray) -> NDArray: |
71 | 72 | return a * 2 |
72 | 73 |
|
| 74 | + @lt.action |
| 75 | + def read_array(self) -> NDArray: |
| 76 | + return np.array([1, 2]) |
| 77 | + |
73 | 78 |
|
74 | 79 | def test_thing_description(): |
75 | 80 | """Make sure the TD validates when numpy types are used.""" |
@@ -102,3 +107,14 @@ def test_rootmodel(): |
102 | 107 | m = ArrayModel(root=input) |
103 | 108 | assert isinstance(m.root, np.ndarray) |
104 | 109 | assert (m.model_dump() == [0, 1, 2]).all() |
| 110 | + |
| 111 | + |
| 112 | +def test_numpy_over_http(): |
| 113 | + """Read numpy array over http.""" |
| 114 | + server = lt.ThingServer({"np_thing": MyNumpyThing}) |
| 115 | + with TestClient(server.app) as client: |
| 116 | + np_thing_client = lt.ThingClient.from_url("/np_thing/", client=client) |
| 117 | + |
| 118 | + array = np_thing_client.read_array() |
| 119 | + assert isinstance(array, np.ndarray) |
| 120 | + assert np.array_equal(array, np.array([1, 2])) |
0 commit comments