-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joel Collins
committed
Apr 14, 2020
1 parent
72bb4b0
commit 1739da9
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pytest | ||
|
||
from labthings.server.spec import paths | ||
|
||
|
||
def make_rule(app, path, **kwargs): | ||
@app.route(path, **kwargs) | ||
def view(): | ||
pass | ||
|
||
return app.url_map._rules_by_endpoint["view"][0] | ||
|
||
|
||
def make_param(in_location="path", **kwargs): | ||
ret = {"in": in_location, "required": True} | ||
ret.update(kwargs) | ||
return ret | ||
|
||
|
||
def test_rule_to_path(app): | ||
rule = make_rule(app, "/path/<id>/") | ||
assert paths.rule_to_path(rule) == "/path/{id}/" | ||
|
||
|
||
def test_rule_to_param(app): | ||
rule = make_rule(app, "/path/<id>/") | ||
assert paths.rule_to_params(rule) == [ | ||
{"in": "path", "name": "id", "required": True, "schema": {"type": "string"}} | ||
] | ||
|
||
|
||
def test_rule_to_param_typed(app): | ||
rule = make_rule(app, "/path/<int:id>/") | ||
assert paths.rule_to_params(rule) == [ | ||
{ | ||
"in": "path", | ||
"name": "id", | ||
"required": True, | ||
"schema": {"type": "integer"}, | ||
"format": "int32", | ||
} | ||
] | ||
|
||
|
||
def test_rule_to_param_typed_default(app): | ||
rule = make_rule(app, "/path/<int:id>/", defaults={"id": 1}) | ||
assert paths.rule_to_params(rule) == [ | ||
{ | ||
"in": "path", | ||
"name": "id", | ||
"required": True, | ||
"default": 1, | ||
"schema": {"type": "integer"}, | ||
"format": "int32", | ||
} | ||
] | ||
|
||
|
||
def test_rule_to_param_overrides(app): | ||
rule = make_rule(app, "/path/<id>/") | ||
overrides = {"override_key": {"in": "header", "name": "header_param"}} | ||
assert paths.rule_to_params(rule, overrides=overrides) == [ | ||
{"in": "path", "name": "id", "required": True, "schema": {"type": "string"}}, | ||
*overrides.values(), | ||
] | ||
|
||
|
||
def test_rule_to_param_overrides_invalid(app): | ||
rule = make_rule(app, "/path/<id>/") | ||
overrides = {"override_key": {"in": "invalid", "name": "header_param"}} | ||
assert paths.rule_to_params(rule, overrides=overrides) == [ | ||
{"in": "path", "name": "id", "required": True, "schema": {"type": "string"}}, | ||
] |