1010from labthings import fields , schema
1111from labthings .actions .thread import ActionThread
1212from labthings .extensions import BaseExtension
13+ from labthings .schema import LogRecordSchema , Schema
1314from labthings .views import ActionView , PropertyView , EventView
1415from marshmallow import validate
16+ import apispec
1517from apispec .utils import validate_spec
18+ from apispec .ext .marshmallow import MarshmallowPlugin
19+ from labthings .apispec import utilities
20+ import yaml
21+
1622
1723def test_openapi (thing ):
1824 """Make an example Thing and check its openapi description validates"""
25+
1926 class TestAction (ActionView ):
2027 args = {"n" : fields .Integer ()}
28+
2129 def post (self ):
2230 return "POST"
31+
2332 thing .add_view (TestAction , "TestAction" )
2433
2534 class TestProperty (PropertyView ):
2635 schema = {"count" : fields .Integer ()}
36+
2737 def get (self ):
2838 return 1
39+
2940 def post (self , args ):
3041 pass
42+
3143 thing .add_view (TestProperty , "TestProperty" )
3244
3345 class TestFieldProperty (PropertyView ):
3446 schema = fields .String (validate = validate .OneOf (["one" , "two" ]))
47+
3548 def get (self ):
3649 return "one"
50+
3751 def post (self , args ):
3852 pass
53+
3954 thing .add_view (TestFieldProperty , "TestFieldProperty" )
40-
41-
55+
4256 thing .spec .to_yaml ()
43- validate_spec (thing .spec )
57+ validate_spec (thing .spec )
58+
59+
60+ def test_ensure_schema_field_instance ():
61+ ret = utilities .ensure_schema (fields .Integer ())
62+ assert ret == {"type" : "integer" }
63+
64+
65+ def test_ensure_schema_field_class ():
66+ ret = utilities .ensure_schema (fields .Integer )
67+ assert ret == {"type" : "integer" }
68+
69+
70+ def test_ensure_schema_class ():
71+ ret = utilities .ensure_schema (LogRecordSchema )
72+ assert isinstance (ret , Schema )
73+
74+
75+ def test_ensure_schema_instance ():
76+ ret = utilities .ensure_schema (LogRecordSchema ())
77+ assert isinstance (ret , Schema )
78+
79+
80+ def test_ensure_schema_dict ():
81+ ret = utilities .ensure_schema (
82+ {
83+ "count" : fields .Integer (),
84+ "name" : fields .String (),
85+ }
86+ )
87+ assert isinstance (ret , Schema )
88+
89+
90+ def test_ensure_schema_none ():
91+ assert utilities .ensure_schema (None ) is None
92+
93+
94+ def test_ensure_schema_error ():
95+ with pytest .raises (TypeError ):
96+ utilities .ensure_schema (Exception )
97+
98+
99+ def test_get_marshmallow_plugin (spec ):
100+ p = utilities .get_marshmallow_plugin (spec )
101+ assert isinstance (p , MarshmallowPlugin )
102+
103+
104+ def test_get_marchmallow_plugin_empty ():
105+ spec = apispec .APISpec ("test" , "0" , "3.0" )
106+ with pytest .raises (Exception ):
107+ utilities .get_marshmallow_plugin (spec )
108+
109+
110+ def dict_is_openapi (d ):
111+ for k in ["paths" , "components" ]:
112+ assert k in d .keys ()
113+ assert d ["openapi" ].startswith ("3.0" )
114+ return True
115+
116+
117+ def test_openapi_json_endpoint (thing ):
118+ c = thing .app .test_client ()
119+ r = c .get ("/docs/openapi" )
120+ assert r .status_code == 200
121+ assert dict_is_openapi (r .get_json ())
122+
123+
124+ def test_openapi_yaml_endpoint (thing ):
125+ c = thing .app .test_client ()
126+
127+ r = c .get ("/docs/openapi.yaml" )
128+ assert r .status_code == 200
129+ assert dict_is_openapi (yaml .safe_load (r .data ))
0 commit comments