Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/test_numpy_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pydantic import BaseModel, RootModel
import numpy as np
from fastapi.testclient import TestClient

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

@lt.action
def read_array(self) -> NDArray:
return np.array([1, 2])


def test_thing_description():
"""Make sure the TD validates when numpy types are used."""
Expand Down Expand Up @@ -102,3 +107,14 @@ def test_rootmodel():
m = ArrayModel(root=input)
assert isinstance(m.root, np.ndarray)
assert (m.model_dump() == [0, 1, 2]).all()


def test_numpy_over_http():
"""Read numpy array over http."""
server = lt.ThingServer({"np_thing": MyNumpyThing})
with TestClient(server.app) as client:
np_thing_client = lt.ThingClient.from_url("/np_thing/", client=client)

array = np_thing_client.read_array()
assert isinstance(array, np.ndarray)
assert np.array_equal(array, np.array([1, 2]))
Loading