Skip to content

Commit 097f506

Browse files
author
Joel Collins
committed
Updated APIspec tests
1 parent d84ae58 commit 097f506

File tree

1 file changed

+88
-56
lines changed

1 file changed

+88
-56
lines changed

tests/test_server_spec_apispec.py

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,78 @@
44
from labthings.server import fields
55

66

7-
def test_dict_to_apispec_operations_no_spec(spec):
7+
def test_view_to_apispec_operations_no_spec(spec):
88
class Index(View):
9+
"""Index docstring"""
10+
911
def get(self):
1012
return "GET"
1113

1214
def post(self):
1315
return "POST"
1416

15-
assert apispec.dict_to_apispec_operations(spec_dict["_operations"], spec) == {
17+
assert apispec.view_to_apispec_operations(Index, spec) == {
1618
"get": {
17-
"responses": {200: {"description": "OK"}},
18-
"description": "",
19-
"summary": "",
20-
"tags": set(),
19+
"description": "Index docstring",
20+
"summary": "Index docstring",
21+
"tags": [],
22+
"responses": {
23+
200: {"description": "OK", "content": {"application/json": {}}}
24+
},
2125
},
2226
"post": {
23-
"responses": {200: {"description": "OK"}},
24-
"description": "",
25-
"summary": "",
26-
"tags": set(),
27+
"description": "Index docstring",
28+
"summary": "Index docstring",
29+
"tags": [],
30+
"responses": {
31+
200: {"description": "OK", "content": {"application/json": {}}}
32+
},
2733
},
2834
}
2935

3036

31-
def test_dict_to_apispec_operations_params(spec):
37+
def test_view_to_apispec_tags(spec):
3238
class Index(View):
39+
"""Index docstring"""
40+
41+
tags = ["tag1", "tag2"]
42+
3343
def get(self):
3444
return "GET"
3545

36-
spec_dict["_operations"]["get"]["_params"] = {"integer": fields.Int()}
46+
def post(self):
47+
return "POST"
48+
49+
assert apispec.view_to_apispec_operations(Index, spec) == {
50+
"get": {
51+
"description": "Index docstring",
52+
"summary": "Index docstring",
53+
"tags": ["tag1", "tag2"],
54+
"responses": {
55+
200: {"description": "OK", "content": {"application/json": {}}}
56+
},
57+
},
58+
"post": {
59+
"description": "Index docstring",
60+
"summary": "Index docstring",
61+
"tags": ["tag1", "tag2"],
62+
"responses": {
63+
200: {"description": "OK", "content": {"application/json": {}}}
64+
},
65+
},
66+
}
67+
68+
69+
def test_dict_to_apispec_operations_params(spec):
70+
class Index(View):
71+
"""Index docstring"""
72+
73+
args = {"integer": fields.Int()}
74+
75+
def put(self):
76+
return "GET"
3777

38-
assert (apispec.dict_to_apispec_operations(spec_dict["_operations"], spec))["get"][
39-
"requestBody"
40-
] == {
78+
assert (apispec.view_to_apispec_operations(Index, spec))["put"]["requestBody"] == {
4179
"content": {
4280
"application/json": {
4381
"schema": {
@@ -51,14 +89,13 @@ def get(self):
5189

5290
def test_dict_to_apispec_operations_schema(spec):
5391
class Index(View):
92+
93+
schema = {"integer": fields.Int()}
94+
5495
def get(self):
5596
return "GET"
5697

57-
spec_dict["_operations"]["get"]["_schema"] = {200: {"integer": fields.Int()}}
58-
59-
assert (apispec.dict_to_apispec_operations(spec_dict["_operations"], spec))["get"][
60-
"responses"
61-
][200] == {
98+
assert apispec.view_to_apispec_operations(Index, spec)["get"]["responses"][200] == {
6299
"description": "OK",
63100
"content": {
64101
"application/json": {
@@ -71,20 +108,10 @@ def get(self):
71108
}
72109

73110

74-
def test_method_to_apispec_operation_extra_fields(spec):
75-
class Index(View):
76-
def get(self):
77-
return "GET"
78-
79-
spec_dict["_operations"]["get"]["summary"] = "A summary"
80-
81-
assert (apispec.dict_to_apispec_operations(spec_dict["_operations"], spec))["get"][
82-
"summary"
83-
] == "A summary"
84-
85-
86111
def test_rule_to_apispec_path(app, spec):
87112
class Index(View):
113+
"""Index docstring"""
114+
88115
def get(self):
89116
return "GET"
90117

@@ -94,30 +121,33 @@ def post(self):
94121
app.add_url_rule("/path", view_func=Index.as_view("index"))
95122
rule = app.url_map._rules_by_endpoint["index"][0]
96123

97-
assert apispec.rule_to_apispec_path(rule, spec_dict, spec) == {
124+
assert apispec.rule_to_apispec_path(rule, Index, spec) == {
98125
"path": "/path",
99126
"operations": {
100127
"get": {
101-
"responses": {200: {"description": "OK"}},
102-
"description": "",
103-
"summary": "",
104-
"tags": set(),
128+
"description": "Index docstring",
129+
"summary": "Index docstring",
130+
"tags": [],
131+
"responses": {
132+
200: {"description": "OK", "content": {"application/json": {}}}
133+
},
105134
},
106135
"post": {
107-
"responses": {200: {"description": "OK"}},
108-
"description": "",
109-
"summary": "",
110-
"tags": set(),
136+
"description": "Index docstring",
137+
"summary": "Index docstring",
138+
"tags": [],
139+
"responses": {
140+
200: {"description": "OK", "content": {"application/json": {}}}
141+
},
111142
},
112143
},
113-
"description": "",
114-
"summary": "",
115-
"tags": set(),
116144
}
117145

118146

119147
def test_rule_to_apispec_path_params(app, spec):
120148
class Index(View):
149+
"""Index docstring"""
150+
121151
def get(self):
122152
return "GET"
123153

@@ -127,14 +157,16 @@ def post(self):
127157
app.add_url_rule("/path/<id>/", view_func=Index.as_view("index"))
128158
rule = app.url_map._rules_by_endpoint["index"][0]
129159

130-
assert apispec.rule_to_apispec_path(rule, spec_dict, spec) == {
160+
assert apispec.rule_to_apispec_path(rule, Index, spec) == {
131161
"path": "/path/{id}/",
132162
"operations": {
133163
"get": {
134-
"responses": {200: {"description": "OK"}},
135-
"description": "",
136-
"summary": "",
137-
"tags": set(),
164+
"description": "Index docstring",
165+
"summary": "Index docstring",
166+
"tags": [],
167+
"responses": {
168+
200: {"description": "OK", "content": {"application/json": {}}}
169+
},
138170
"parameters": [
139171
{
140172
"in": "path",
@@ -145,10 +177,12 @@ def post(self):
145177
],
146178
},
147179
"post": {
148-
"responses": {200: {"description": "OK"}},
149-
"description": "",
150-
"summary": "",
151-
"tags": set(),
180+
"description": "Index docstring",
181+
"summary": "Index docstring",
182+
"tags": [],
183+
"responses": {
184+
200: {"description": "OK", "content": {"application/json": {}}}
185+
},
152186
"parameters": [
153187
{
154188
"in": "path",
@@ -159,7 +193,5 @@ def post(self):
159193
],
160194
},
161195
},
162-
"description": "",
163-
"summary": "",
164-
"tags": set(),
165196
}
197+

0 commit comments

Comments
 (0)