11import pytest
22
3- import os
4- import json
5- import jsonschema
63from labthings .server import fields
74from labthings .server .view import View
85from labthings .server .spec import td
@@ -13,17 +10,6 @@ def thing_description(thing):
1310 return thing .thing_description
1411
1512
16- def validate_thing_description (thing_description , app_ctx , schemas_path ):
17- schema = json .load (open (os .path .join (schemas_path , "td_schema.json" ), "r" ))
18- jsonschema .Draft7Validator .check_schema (schema )
19-
20- with app_ctx .test_request_context ():
21- td_json = thing_description .to_dict ()
22- assert td_json
23-
24- jsonschema .validate (instance = td_json , schema = schema )
25-
26-
2713def test_find_schema_for_view_readonly ():
2814 class ViewClass :
2915 def get (self ):
@@ -58,13 +44,13 @@ class ViewClass:
5844 assert td .find_schema_for_view (ViewClass ) == {}
5945
6046
61- def test_td_init (thing_description , app_ctx , schemas_path ):
47+ def test_td_init (helpers , thing_description , app_ctx , schemas_path ):
6248 assert thing_description
6349
64- validate_thing_description (thing_description , app_ctx , schemas_path )
50+ helpers . validate_thing_description (thing_description , app_ctx , schemas_path )
6551
6652
67- def test_td_add_link (thing_description , view_cls , app_ctx , schemas_path ):
53+ def test_td_add_link (helpers , thing_description , view_cls , app_ctx , schemas_path ):
6854 thing_description .add_link (view_cls , "rel" )
6955 assert {
7056 "rel" : "rel" ,
@@ -73,7 +59,7 @@ def test_td_add_link(thing_description, view_cls, app_ctx, schemas_path):
7359 "kwargs" : {},
7460 } in thing_description ._links
7561
76- validate_thing_description (thing_description , app_ctx , schemas_path )
62+ helpers . validate_thing_description (thing_description , app_ctx , schemas_path )
7763
7864
7965def test_td_add_link_options (thing_description , view_cls ):
@@ -99,18 +85,20 @@ def test_td_links(thing_description, app_ctx, view_cls):
9985 )
10086
10187
102- def test_td_action (app , thing_description , view_cls , app_ctx , schemas_path ):
88+ def test_td_action (helpers , app , thing_description , view_cls , app_ctx , schemas_path ):
10389 app .add_url_rule ("/" , view_func = view_cls .as_view ("index" ))
10490 rules = app .url_map ._rules_by_endpoint ["index" ]
10591
10692 thing_description .action (rules , view_cls )
10793
10894 with app_ctx .test_request_context ():
10995 assert "index" in thing_description .to_dict ().get ("actions" )
110- validate_thing_description (thing_description , app_ctx , schemas_path )
96+ helpers . validate_thing_description (thing_description , app_ctx , schemas_path )
11197
11298
113- def test_td_action_with_schema (app , thing_description , view_cls , app_ctx , schemas_path ):
99+ def test_td_action_with_schema (
100+ helpers , app , thing_description , view_cls , app_ctx , schemas_path
101+ ):
114102 view_cls .post .__apispec__ = {
115103 "_params" : {"integer" : fields .Int ()},
116104 "@type" : "ToggleAction" ,
@@ -128,10 +116,10 @@ def test_td_action_with_schema(app, thing_description, view_cls, app_ctx, schema
128116 "properties" : {"integer" : {"type" : "integer" , "format" : "int32" }},
129117 "@type" : "ToggleAction" ,
130118 }
131- validate_thing_description (thing_description , app_ctx , schemas_path )
119+ helpers . validate_thing_description (thing_description , app_ctx , schemas_path )
132120
133121
134- def test_td_property (app , thing_description , app_ctx , schemas_path ):
122+ def test_td_property (helpers , app , thing_description , app_ctx , schemas_path ):
135123 class Index (View ):
136124 def get (self ):
137125 return "GET"
@@ -143,17 +131,18 @@ def get(self):
143131
144132 with app_ctx .test_request_context ():
145133 assert "index" in thing_description .to_dict ().get ("properties" )
146- validate_thing_description (thing_description , app_ctx , schemas_path )
134+ helpers . validate_thing_description (thing_description , app_ctx , schemas_path )
147135
148136
149- def test_td_property_with_schema (app , thing_description , app_ctx , schemas_path ):
137+ def test_td_property_with_schema (
138+ helpers , app , thing_description , app_ctx , schemas_path
139+ ):
150140 class Index (View ):
151141 def get (self ):
152142 return "GET"
153143
154144 Index .__apispec__ = {
155- "_propertySchema" : {"integer" : fields .Int ()},
156- "@type" : "OnOffProperty" ,
145+ "_propertySchema" : fields .Int (required = True ),
157146 }
158147
159148 app .add_url_rule ("/" , view_func = Index .as_view ("index" ))
@@ -163,11 +152,12 @@ def get(self):
163152
164153 with app_ctx .test_request_context ():
165154 assert "index" in thing_description .to_dict ().get ("properties" )
166- assert "@type" in thing_description .to_dict ().get ("properties" ).get ("index" , {})
167- validate_thing_description (thing_description , app_ctx , schemas_path )
155+ helpers .validate_thing_description (thing_description , app_ctx , schemas_path )
168156
169157
170- def test_td_property_with_url_param (app , thing_description , app_ctx , schemas_path ):
158+ def test_td_property_with_url_param (
159+ helpers , app , thing_description , app_ctx , schemas_path
160+ ):
171161 class Index (View ):
172162 def get (self ):
173163 return "GET"
@@ -179,10 +169,10 @@ def get(self):
179169
180170 with app_ctx .test_request_context ():
181171 assert "index" in thing_description .to_dict ().get ("properties" )
182- validate_thing_description (thing_description , app_ctx , schemas_path )
172+ helpers . validate_thing_description (thing_description , app_ctx , schemas_path )
183173
184174
185- def test_td_property_write_only (app , thing_description , app_ctx , schemas_path ):
175+ def test_td_property_write_only (helpers , app , thing_description , app_ctx , schemas_path ):
186176 class Index (View ):
187177 def post (self ):
188178 return "POST"
@@ -196,4 +186,4 @@ def post(self):
196186
197187 with app_ctx .test_request_context ():
198188 assert "index" in thing_description .to_dict ().get ("properties" )
199- validate_thing_description (thing_description , app_ctx , schemas_path )
189+ helpers . validate_thing_description (thing_description , app_ctx , schemas_path )
0 commit comments