10
10
from labthings import fields , schema
11
11
from labthings .actions .thread import ActionThread
12
12
from labthings .extensions import BaseExtension
13
+ from labthings .schema import LogRecordSchema , Schema
13
14
from labthings .views import ActionView , PropertyView , EventView
14
15
from marshmallow import validate
16
+ import apispec
15
17
from apispec .utils import validate_spec
18
+ from apispec .ext .marshmallow import MarshmallowPlugin
19
+ from labthings .apispec import utilities
20
+ import yaml
21
+
16
22
17
23
def test_openapi (thing ):
18
24
"""Make an example Thing and check its openapi description validates"""
25
+
19
26
class TestAction (ActionView ):
20
27
args = {"n" : fields .Integer ()}
28
+
21
29
def post (self ):
22
30
return "POST"
31
+
23
32
thing .add_view (TestAction , "TestAction" )
24
33
25
34
class TestProperty (PropertyView ):
26
35
schema = {"count" : fields .Integer ()}
36
+
27
37
def get (self ):
28
38
return 1
39
+
29
40
def post (self , args ):
30
41
pass
42
+
31
43
thing .add_view (TestProperty , "TestProperty" )
32
44
33
45
class TestFieldProperty (PropertyView ):
34
46
schema = fields .String (validate = validate .OneOf (["one" , "two" ]))
47
+
35
48
def get (self ):
36
49
return "one"
50
+
37
51
def post (self , args ):
38
52
pass
53
+
39
54
thing .add_view (TestFieldProperty , "TestFieldProperty" )
40
-
41
-
55
+
42
56
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