11"""Test the ThingServer.
22
3- Currently, this adds one trivial test for an error.
4-
53While the server is covered by many of the other tests, it would
64be helpful to have some more bottom-up unit testing in this file.
75"""
86
97import pytest
108import labthings_fastapi as lt
9+ from fastapi .testclient import TestClient
1110
1211
1312def 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