Skip to content

Commit

Permalink
Coverage for server spec paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 14, 2020
1 parent 72bb4b0 commit 1739da9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/test_server_spec_paths.py
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"}},
]

0 comments on commit 1739da9

Please sign in to comment.