Skip to content

Commit f3eabe8

Browse files
committed
test: use pet store as input for test cases
1 parent a440a5d commit f3eabe8

File tree

1 file changed

+97
-113
lines changed

1 file changed

+97
-113
lines changed

tests/webframeworks/test_flask.py

Lines changed: 97 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def app():
1717
@pytest.fixture(scope="function", params=("2.0", "3.0.0"))
1818
def spec(request):
1919
return APISpec(
20-
title="Test Suite",
20+
title="Swagger Petstore",
2121
version="1.0.0",
2222
openapi_version=request.param,
2323
plugins=(FlaskPlugin(),),
@@ -26,223 +26,207 @@ def spec(request):
2626

2727
class TestFlaskPlugin:
2828
def test_function_view(self, app, spec):
29-
@app.route("/hello")
30-
def greeting():
31-
return "hello"
29+
@app.route("/pet")
30+
def pet():
31+
return "Max"
3232

3333
operations = {
3434
"get": {
3535
"parameters": [],
36-
"responses": {"200": {"description": "get a greeting"}},
36+
"responses": {"200": {"description": "get a pet"}},
3737
}
3838
}
39-
spec.path(view=greeting, operations=operations)
39+
spec.path(view=pet, operations=operations)
4040
paths = utils.get_paths(spec)
41-
assert "get" in paths["/hello"]
42-
assert paths["/hello"] == operations
41+
assert "get" in paths["/pet"]
42+
assert paths["/pet"] == operations
4343

4444
def test_method_view(self, app, spec):
45-
class GreetingView(MethodView):
45+
class PetView(MethodView):
4646
"""The greeting view.
4747
---
4848
x-extension: global metadata
4949
"""
5050

5151
def get(self):
52-
"""A greeting endpoint.
52+
"""Get a pet's name.
5353
---
54-
description: get a greeting
54+
description: get a pet's name
5555
responses:
5656
200:
57-
description: received greeting
57+
description: the pet's name
5858
"""
59-
return "hello"
59+
return "Max"
6060

6161
def post(self):
6262
return {}
6363

64-
method_view = GreetingView.as_view("hello")
65-
app.add_url_rule("/hello", view_func=method_view, methods=("GET", "POST"))
64+
method_view = PetView.as_view("pet")
65+
app.add_url_rule("/pet", view_func=method_view, methods=("GET", "POST"))
6666
spec.path(view=method_view)
6767
paths = utils.get_paths(spec)
68-
assert paths["/hello"]["get"] == {
69-
"summary": "A greeting endpoint.",
70-
"description": "get a greeting",
71-
"responses": {"200": {"description": "received greeting"}},
68+
assert paths["/pet"]["get"] == {
69+
"summary": "Get a pet's name.",
70+
"description": "get a pet's name",
71+
"responses": {"200": {"description": "the pet's name"}},
7272
}
73-
assert paths["/hello"]["post"] == {}
74-
assert paths["/hello"]["x-extension"] == "global metadata"
73+
assert paths["/pet"]["post"] == {}
74+
assert paths["/pet"]["x-extension"] == "global metadata"
7575

7676
def test_path_with_multiple_methods(self, app, spec):
77-
@app.route("/hello", methods=["GET", "POST"])
78-
def greeting():
79-
return "hello"
77+
@app.route("/pet", methods=["GET", "POST"])
78+
def pet():
79+
return "Max"
8080

8181
spec.path(
82-
view=greeting,
82+
view=pet,
8383
operations={
84-
"get": {"description": "get a greeting", "responses": {"200": {}}},
85-
"post": {"description": "post a greeting", "responses": {"200": {}}},
84+
"get": {"description": "get a pet's name", "responses": {"200": {}}},
85+
"post": {"description": "register a pet", "responses": {"200": {}}},
8686
},
8787
)
8888
paths = utils.get_paths(spec)
89-
get_op = paths["/hello"]["get"]
90-
post_op = paths["/hello"]["post"]
91-
assert get_op["description"] == "get a greeting"
92-
assert post_op["description"] == "post a greeting"
89+
get_op = paths["/pet"]["get"]
90+
post_op = paths["/pet"]["post"]
91+
assert get_op["description"] == "get a pet's name"
92+
assert post_op["description"] == "register a pet"
9393

9494
def test_methods_from_rule(self, app, spec):
95-
class GreetingView(MethodView):
96-
"""The greeting view."""
95+
class PetView(MethodView):
96+
"""A view for pets."""
9797

9898
def get(self):
99-
"""A greeting endpoint.
99+
"""Get a pet's name.
100100
---
101-
description: get a greeting
101+
description: get a pet's name
102102
responses:
103103
200:
104-
description: received greeting
104+
description: the pet's name
105105
"""
106-
return "hello"
106+
return "Max"
107107

108108
def post(self):
109109
return {}
110110

111111
def delete(self):
112112
return {}
113113

114-
method_view = GreetingView.as_view("hello")
115-
app.add_url_rule("/hello", view_func=method_view, methods=("GET", "POST"))
114+
method_view = PetView.as_view("pet")
115+
app.add_url_rule("/pet", view_func=method_view, methods=("GET", "POST"))
116116
spec.path(view=method_view)
117117
paths = utils.get_paths(spec)
118-
assert "get" in paths["/hello"]
119-
assert "post" in paths["/hello"]
120-
assert "delete" not in paths["/hello"]
118+
assert "get" in paths["/pet"]
119+
assert "post" in paths["/pet"]
120+
assert "delete" not in paths["/pet"]
121121

122122
def test_docstring_introspection(self, app, spec):
123-
@app.route("/hello")
124-
def greeting():
125-
"""A greeting endpoint.
123+
@app.route("/pet")
124+
def pet():
125+
"""Get a pet's name.
126126
---
127127
x-extension: value
128128
get:
129-
description: get a greeting
129+
description: get a pet's name
130130
responses:
131131
200:
132-
description: received greeting
132+
description: the pet's name
133133
post:
134-
description: post a greeting
134+
description: register a pet
135135
responses:
136136
200:
137-
description: delivered greeting
137+
description: the registered pet's name
138138
foo:
139139
description: not a valid operation
140140
"""
141-
return "hello"
141+
return "Max"
142142

143-
spec.path(view=greeting)
143+
spec.path(view=pet)
144144
paths = utils.get_paths(spec)
145-
assert paths["/hello"]["x-extension"] == "value"
146-
assert paths["/hello"]["get"] == {
147-
"description": "get a greeting",
148-
"responses": {"200": {"description": "received greeting"}},
145+
assert paths["/pet"]["x-extension"] == "value"
146+
assert paths["/pet"]["get"] == {
147+
"description": "get a pet's name",
148+
"responses": {"200": {"description": "the pet's name"}},
149149
}
150-
assert paths["/hello"]["post"] == {
151-
"description": "post a greeting",
152-
"responses": {"200": {"description": "delivered greeting"}},
150+
assert paths["/pet"]["post"] == {
151+
"description": "register a pet",
152+
"responses": {"200": {"description": "the registered pet's name"}},
153153
}
154-
assert "foo" not in paths["/hello"]
154+
assert "foo" not in paths["/pet"]
155155

156156
def test_specs_from_decorator(self, app, spec):
157-
class GreetingView(MethodView):
157+
class PetView(MethodView):
158158
@spec_from(
159159
{
160-
"description": "get a greeting",
161-
"responses": {200: {"description": "received greeting"}},
160+
"description": "get a pet's name",
161+
"responses": {"200": {"description": "the pet's name"}},
162162
}
163163
)
164164
def get(self):
165-
"""A greeting endpoint."""
166-
return "hello"
165+
"""Get a pet's name."""
166+
return "Max"
167167

168-
method_view = GreetingView.as_view("hello")
169-
app.add_url_rule("/hello", view_func=method_view)
168+
method_view = PetView.as_view("pet")
169+
app.add_url_rule("/pet", view_func=method_view)
170170
spec.path(view=method_view)
171171
paths = utils.get_paths(spec)
172-
assert paths["/hello"]["get"] == {
173-
"summary": "A greeting endpoint.",
174-
"description": "get a greeting",
175-
"responses": {"200": {"description": "received greeting"}},
172+
assert paths["/pet"]["get"] == {
173+
"summary": "Get a pet's name.",
174+
"description": "get a pet's name",
175+
"responses": {"200": {"description": "the pet's name"}},
176176
}
177177

178178
def test_path_is_translated_to_swagger_template(self, app, spec):
179-
@app.route("/hello/<user_id>")
180-
def hello_user(user_id):
181-
return f"greeting sent to user {user_id}"
179+
@app.route("/pet/<name>")
180+
def pet(name):
181+
return name
182182

183-
spec.path(view=hello_user)
184-
assert "/hello/{user_id}" in utils.get_paths(spec)
183+
spec.path(view=pet)
184+
assert "/pet/{name}" in utils.get_paths(spec)
185185

186186
def test_explicit_app_kwarg(self, spec):
187187
app = Flask(__name__)
188188

189-
@app.route("/bye")
190-
def bye():
191-
return "bye"
189+
@app.route("/pet")
190+
def pet():
191+
return "Max"
192192

193-
spec.path(view=bye, app=app)
194-
assert "/bye" in utils.get_paths(spec)
193+
spec.path(view=pet, app=app)
194+
assert "/pet" in utils.get_paths(spec)
195195

196196
def test_auto_responses(self, app, spec):
197-
class ResponsesView(MethodView):
198-
"""The greeting view."""
197+
class PetView(MethodView):
198+
"""A view for pets."""
199199

200200
def get(self):
201-
"""A greeting endpoint.
201+
"""Get a pet's name.
202202
---
203-
description: get a greeting
203+
description: get a pet's name
204204
responses:
205205
200:
206-
description: received greeting
206+
description: the pet's name
207207
400:
208208
404:
209-
description: greeting not found
209+
description: pet not found
210210
default:
211211
description: unexpected error
212212
"""
213-
return "hello"
213+
return "Max"
214214

215-
method_view = ResponsesView.as_view("responses")
216-
app.add_url_rule("/hello", view_func=method_view)
215+
method_view = PetView.as_view("pet")
216+
app.add_url_rule("/pet", view_func=method_view)
217217
spec.path(view=method_view)
218218
paths = utils.get_paths(spec)
219219

220-
assert paths["/hello"]["get"] == {
221-
"summary": "A greeting endpoint.",
222-
"description": "get a greeting",
220+
assert paths["/pet"]["get"] == {
221+
"summary": "Get a pet's name.",
222+
"description": "get a pet's name",
223223
"responses": {
224-
"200": {"description": "received greeting"},
225-
"400": {
226-
"$ref": "#/responses/BadRequest"
227-
if spec.openapi_version.major < 3
228-
else "#/components/responses/BadRequest"
229-
},
230-
"404": {"description": "greeting not found"},
224+
"200": {"description": "the pet's name"},
225+
"400": utils.build_ref(spec, "response", "BadRequest"),
226+
"404": {"description": "pet not found"},
231227
"default": {"description": "unexpected error"},
232228
},
233229
}
234230

235-
if spec.openapi_version.major < 3:
236-
assert spec.to_dict()["responses"] == {
237-
"BadRequest": {"schema": {"$ref": "#/definitions/HTTPResponse"}}
238-
}
239-
else:
240-
assert utils.get_responses(spec) == {
241-
"BadRequest": {
242-
"content": {
243-
"application/json": {
244-
"schema": {"$ref": "#/components/schemas/HTTPResponse"}
245-
}
246-
}
247-
},
248-
}
231+
ref = utils.build_ref(spec, "schema", "HTTPResponse")
232+
assert utils.get_schema(spec, utils.get_responses(spec)["BadRequest"]) == ref

0 commit comments

Comments
 (0)