Skip to content

Commit 2cd3eaf

Browse files
authored
Merge pull request #224 from labthings/bugfix--server-thing-descriptions
Fix urls for affordances in the server thing descriptions
2 parents 794c402 + e6ca439 commit 2cd3eaf

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/labthings_fastapi/server/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ def thing_descriptions(request: Request) -> Mapping[str, ThingDescription]:
301301
dictionaries.
302302
"""
303303
return {
304-
path: thing.thing_description(path, base=str(request.base_url))
305-
for path, thing in thing_server.things.items()
304+
name: thing.thing_description(name + "/", base=str(request.base_url))
305+
for name, thing in thing_server.things.items()
306306
}
307307

308308
@self.app.get("/things/")

tests/test_server.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Test the ThingServer.
22
3-
Currently, this adds one trivial test for an error.
4-
53
While the server is covered by many of the other tests, it would
64
be helpful to have some more bottom-up unit testing in this file.
75
"""
86

97
import pytest
108
import labthings_fastapi as lt
9+
from fastapi.testclient import TestClient
1110

1211

1312
def test_server_from_config_non_thing_error():
@@ -18,3 +17,49 @@ def test_server_from_config_non_thing_error():
1817
things={"thingone": lt.ThingConfig(cls="builtins:object")}
1918
)
2019
)
20+
21+
22+
def test_server_thing_descriptions():
23+
"""Check the server ThingDescriptions.
24+
25+
Check the expected Action and Properties and their URLS are in the server
26+
thing_descriptions.
27+
"""
28+
conf = {
29+
"things": {
30+
"thing1": "labthings_fastapi.example_things:MyThing",
31+
"thing2": {
32+
"class": "labthings_fastapi.example_things:MyThing",
33+
"kwargs": {},
34+
},
35+
}
36+
}
37+
38+
thing_names = ["thing1", "thing2"]
39+
props = ["counter", "foo"]
40+
actions = [
41+
"action_with_only_kwargs",
42+
"action_without_arguments",
43+
"anaction",
44+
"increment_counter",
45+
"make_a_dict",
46+
"slowly_increase_counter",
47+
]
48+
49+
server = lt.ThingServer.from_config(conf)
50+
with TestClient(server.app) as client:
51+
response = client.get("/thing_descriptions/")
52+
response.raise_for_status()
53+
thing_descriptions = response.json()
54+
55+
for thing_name in thing_names:
56+
thing_description = thing_descriptions[thing_name]
57+
for action_name in actions:
58+
action = thing_description["actions"][action_name]
59+
expected_href = thing_name + "/" + action_name
60+
assert action["forms"][0]["href"] == expected_href
61+
62+
for prop_name in props:
63+
prop = thing_description["properties"][prop_name]
64+
expected_href = thing_name + "/" + prop_name
65+
assert prop["forms"][0]["href"] == expected_href

0 commit comments

Comments
 (0)