|
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,14 @@ 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 | + |
| 78 | + @lt.property |
| 79 | + def array_property(self) -> NDArray: |
| 80 | + return np.array([3, 4, 5]) |
| 81 | + |
73 | 82 |
|
74 | 83 | def test_thing_description(): |
75 | 84 | """Make sure the TD validates when numpy types are used.""" |
@@ -102,3 +111,16 @@ def test_rootmodel(): |
102 | 111 | m = ArrayModel(root=input) |
103 | 112 | assert isinstance(m.root, np.ndarray) |
104 | 113 | assert (m.model_dump() == [0, 1, 2]).all() |
| 114 | + |
| 115 | + |
| 116 | +def test_numpy_over_http(): |
| 117 | + """Read numpy array over http.""" |
| 118 | + server = lt.ThingServer({"np_thing": MyNumpyThing}) |
| 119 | + with TestClient(server.app) as client: |
| 120 | + np_thing_client = lt.ThingClient.from_url("/np_thing/", client=client) |
| 121 | + |
| 122 | + arrayprop = np_thing_client.array_property |
| 123 | + assert np.array_equal(np.asarray(arrayprop), np.array([3, 4, 5])) |
| 124 | + |
| 125 | + array = np_thing_client.read_array() |
| 126 | + assert np.array_equal(np.asarray(array), np.array([1, 2])) |
0 commit comments