Skip to content

Commit 394e34e

Browse files
author
Seth Teichman
committed
Add limited tests for API docs
1 parent cc8e61f commit 394e34e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from flask_parameter_validation.docs_blueprint import get_route_docs
2+
3+
def test_http_ok(client):
4+
r = client.get("/docs/")
5+
assert r.status_code == 200
6+
r = client.get("/docs/json")
7+
assert r.status_code == 200
8+
9+
def test_routes_added(app):
10+
routes = []
11+
for rule in app.url_map.iter_rules():
12+
routes.append(str(rule))
13+
for doc in get_route_docs():
14+
assert doc["rule"] in routes
15+
16+
def test_doc_types_of_default(app):
17+
locs = {
18+
"form": "Form",
19+
"json": "Json",
20+
"query": "Query",
21+
"route": "Route"
22+
}
23+
types = {
24+
"bool": {"opt": "Optional[bool, NoneType]", "n_opt": "bool"},
25+
"date": {"opt": "Optional[date, NoneType]", "n_opt": "date"},
26+
"datetime": {"opt": "Optional[datetime, NoneType]", "n_opt": "datetime"},
27+
"dict": {"opt": "Optional[dict, NoneType]", "n_opt": "dict"},
28+
"float": {"opt": "Optional[float, NoneType]", "n_opt": "float"},
29+
"int": {"opt": "Optional[int, NoneType]", "n_opt": "int"},
30+
"int_enum": {"opt": "Optional[Binary, NoneType]", "n_opt": "Binary"},
31+
"list": {"opt": "Optional[List[int], NoneType]", "n_opt": "List[str]"},
32+
"str": {"opt": "Optional[str, NoneType]", "n_opt": "str"},
33+
"str_enum": {"opt": "Optional[Fruits, NoneType]", "n_opt": "Fruits"},
34+
"time": {"opt": "Optional[time, NoneType]", "n_opt": "time"},
35+
"union": {"opt": "Union[bool, int, NoneType]", "n_opt": "Union[bool, int]"}
36+
}
37+
route_unsupported_types = ["dict", "list"]
38+
route_docs = get_route_docs()
39+
for loc in locs.keys():
40+
for arg_type in types.keys():
41+
if loc == "route" and arg_type in route_unsupported_types:
42+
continue
43+
route_to_check = f"/{loc}/{arg_type}/default"
44+
for doc in route_docs:
45+
if doc["rule"] == route_to_check:
46+
args = doc["args"][locs[loc]]
47+
if args[0]["name"] == "n_opt":
48+
n_opt = args[0]
49+
opt = args[1]
50+
else:
51+
opt = args[0]
52+
n_opt = args[1]
53+
assert n_opt["type"] == types[arg_type]["n_opt"]
54+
assert opt["type"] == types[arg_type]["opt"]

flask_parameter_validation/test/testing_application.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from flask_parameter_validation.test.testing_blueprints.file_blueprint import get_file_blueprint
77
from flask_parameter_validation.test.testing_blueprints.multi_source_blueprint import get_multi_source_blueprint
88
from flask_parameter_validation.test.testing_blueprints.parameter_blueprint import get_parameter_blueprint
9+
from flask_parameter_validation.docs_blueprint import docs_blueprint
910

1011
multi_source_sources = [
1112
{"class": Query, "name": "query"},
@@ -22,6 +23,7 @@ def create_app():
2223
app.register_blueprint(get_parameter_blueprint(Form, "form", "form", "post"))
2324
app.register_blueprint(get_parameter_blueprint(Route, "route", "route", "get"))
2425
app.register_blueprint(get_file_blueprint("file"))
26+
app.register_blueprint(docs_blueprint)
2527
for source_a in multi_source_sources:
2628
for source_b in multi_source_sources:
2729
if source_a["name"] != source_b["name"]:

0 commit comments

Comments
 (0)