Skip to content

Commit

Permalink
Updated APIspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jun 23, 2020
1 parent d84ae58 commit 097f506
Showing 1 changed file with 88 additions and 56 deletions.
144 changes: 88 additions & 56 deletions tests/test_server_spec_apispec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,78 @@
from labthings.server import fields


def test_dict_to_apispec_operations_no_spec(spec):
def test_view_to_apispec_operations_no_spec(spec):
class Index(View):
"""Index docstring"""

def get(self):
return "GET"

def post(self):
return "POST"

assert apispec.dict_to_apispec_operations(spec_dict["_operations"], spec) == {
assert apispec.view_to_apispec_operations(Index, spec) == {
"get": {
"responses": {200: {"description": "OK"}},
"description": "",
"summary": "",
"tags": set(),
"description": "Index docstring",
"summary": "Index docstring",
"tags": [],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
},
"post": {
"responses": {200: {"description": "OK"}},
"description": "",
"summary": "",
"tags": set(),
"description": "Index docstring",
"summary": "Index docstring",
"tags": [],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
},
}


def test_dict_to_apispec_operations_params(spec):
def test_view_to_apispec_tags(spec):
class Index(View):
"""Index docstring"""

tags = ["tag1", "tag2"]

def get(self):
return "GET"

spec_dict["_operations"]["get"]["_params"] = {"integer": fields.Int()}
def post(self):
return "POST"

assert apispec.view_to_apispec_operations(Index, spec) == {
"get": {
"description": "Index docstring",
"summary": "Index docstring",
"tags": ["tag1", "tag2"],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
},
"post": {
"description": "Index docstring",
"summary": "Index docstring",
"tags": ["tag1", "tag2"],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
},
}


def test_dict_to_apispec_operations_params(spec):
class Index(View):
"""Index docstring"""

args = {"integer": fields.Int()}

def put(self):
return "GET"

assert (apispec.dict_to_apispec_operations(spec_dict["_operations"], spec))["get"][
"requestBody"
] == {
assert (apispec.view_to_apispec_operations(Index, spec))["put"]["requestBody"] == {
"content": {
"application/json": {
"schema": {
Expand All @@ -51,14 +89,13 @@ def get(self):

def test_dict_to_apispec_operations_schema(spec):
class Index(View):

schema = {"integer": fields.Int()}

def get(self):
return "GET"

spec_dict["_operations"]["get"]["_schema"] = {200: {"integer": fields.Int()}}

assert (apispec.dict_to_apispec_operations(spec_dict["_operations"], spec))["get"][
"responses"
][200] == {
assert apispec.view_to_apispec_operations(Index, spec)["get"]["responses"][200] == {
"description": "OK",
"content": {
"application/json": {
Expand All @@ -71,20 +108,10 @@ def get(self):
}


def test_method_to_apispec_operation_extra_fields(spec):
class Index(View):
def get(self):
return "GET"

spec_dict["_operations"]["get"]["summary"] = "A summary"

assert (apispec.dict_to_apispec_operations(spec_dict["_operations"], spec))["get"][
"summary"
] == "A summary"


def test_rule_to_apispec_path(app, spec):
class Index(View):
"""Index docstring"""

def get(self):
return "GET"

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

assert apispec.rule_to_apispec_path(rule, spec_dict, spec) == {
assert apispec.rule_to_apispec_path(rule, Index, spec) == {
"path": "/path",
"operations": {
"get": {
"responses": {200: {"description": "OK"}},
"description": "",
"summary": "",
"tags": set(),
"description": "Index docstring",
"summary": "Index docstring",
"tags": [],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
},
"post": {
"responses": {200: {"description": "OK"}},
"description": "",
"summary": "",
"tags": set(),
"description": "Index docstring",
"summary": "Index docstring",
"tags": [],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
},
},
"description": "",
"summary": "",
"tags": set(),
}


def test_rule_to_apispec_path_params(app, spec):
class Index(View):
"""Index docstring"""

def get(self):
return "GET"

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

assert apispec.rule_to_apispec_path(rule, spec_dict, spec) == {
assert apispec.rule_to_apispec_path(rule, Index, spec) == {
"path": "/path/{id}/",
"operations": {
"get": {
"responses": {200: {"description": "OK"}},
"description": "",
"summary": "",
"tags": set(),
"description": "Index docstring",
"summary": "Index docstring",
"tags": [],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
"parameters": [
{
"in": "path",
Expand All @@ -145,10 +177,12 @@ def post(self):
],
},
"post": {
"responses": {200: {"description": "OK"}},
"description": "",
"summary": "",
"tags": set(),
"description": "Index docstring",
"summary": "Index docstring",
"tags": [],
"responses": {
200: {"description": "OK", "content": {"application/json": {}}}
},
"parameters": [
{
"in": "path",
Expand All @@ -159,7 +193,5 @@ def post(self):
],
},
},
"description": "",
"summary": "",
"tags": set(),
}

0 comments on commit 097f506

Please sign in to comment.