Skip to content

Commit d9c68e3

Browse files
Add test for action returning an NDArray
1 parent d96fe59 commit d9c68e3

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

tests/test_numpy_type.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pydantic import BaseModel, RootModel
44
import numpy as np
5+
from fastapi.testclient import TestClient
56

67
from labthings_fastapi.testing import create_thing_without_server
78
from labthings_fastapi.types.numpy import NDArray, DenumpifyingDict
@@ -70,6 +71,10 @@ class MyNumpyThing(lt.Thing):
7071
def action_with_arrays(self, a: NDArray) -> NDArray:
7172
return a * 2
7273

74+
@lt.action
75+
def read_array(self) -> NDArray:
76+
return np.array([1, 2])
77+
7378

7479
def test_thing_description():
7580
"""Make sure the TD validates when numpy types are used."""
@@ -102,3 +107,14 @@ def test_rootmodel():
102107
m = ArrayModel(root=input)
103108
assert isinstance(m.root, np.ndarray)
104109
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

Comments
 (0)