Skip to content

Commit

Permalink
Test: Add tests for JsonBody field, List field, and Object field
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjr0719 committed Jun 20, 2019
1 parent 43b4c9d commit 2a9f4d5
Showing 1 changed file with 105 additions and 4 deletions.
109 changes: 105 additions & 4 deletions tests/test_fields.py
Expand Up @@ -7,7 +7,7 @@


@pytest.mark.parametrize(
"description, required, name, choices, field_serialize, path_parameters",
"description, required, name, choices, serialized_field, path_parameters",
[
(None, None, None, None, {}, [{"required": True, "in": "body", "name": None}]),
(
Expand Down Expand Up @@ -54,13 +54,13 @@
],
)
def test_base_field(
app, description, required, name, choices, field_serialize, path_parameters
app, description, required, name, choices, serialized_field, path_parameters
):

field = doc.Field(
description=description, required=required, name=name, choices=choices
)
assert field.serialize() == field_serialize
assert field.serialize() == serialized_field

@app.get("/")
@doc.consumes(field, location="body", required=True)
Expand Down Expand Up @@ -251,7 +251,7 @@ class TestSchema:
(datetime, {"type": "string", "format": "date-time"}),
(doc.DateTime, {"type": "string", "format": "date-time"}),
(doc.DateTime(), {"type": "string", "format": "date-time"}),
(TestSchema, {'$ref': '#/definitions/TestSchema', 'type': 'object'}),
(TestSchema, {"$ref": "#/definitions/TestSchema", "type": "object"}),
(dict, {"type": "object", "properties": {}}),
({"foo": "bar"}, {"type": "object", "properties": {"foo": {}}}),
(list, {"type": "array", "items": []}),
Expand All @@ -262,3 +262,104 @@ def test_serialize_schema(schema, expected_schema):
serialized_schema = doc.serialize_schema(schema)

assert serialized_schema == expected_schema


def test_jsonbody_field(app):
field = doc.JsonBody()
assert field.serialize() == {
"name": "body",
"schema": {"properties": {}, "type": "object"},
}

@app.get("/")
@doc.consumes(field, location="body", required=True)
def test(request):
return text("test")

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
path = swagger_json["paths"]["/"]["get"]
assert path["parameters"][0] == {
"required": True,
"in": "body",
"name": "body",
"schema": {"properties": {}, "type": "object"},
}


@pytest.mark.parametrize(
"field, serialized_field, path_parameters",
[
(
doc.List(),
{"type": "array", "items": []},
{
"required": True,
"in": "body",
"name": None,
"type": "array",
"items": [],
},
),
(
doc.List(doc.String),
{"type": "array", "items": {"type": "string"}},
{
"required": True,
"in": "body",
"name": None,
"type": "array",
"items": {"type": "string"},
},
),
],
)
def test_list_field(app, field, serialized_field, path_parameters):
assert field.serialize() == serialized_field

@app.get("/")
@doc.consumes(field, location="body", required=True)
def test(request):
return text("test")

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
path = swagger_json["paths"]["/"]["get"]
assert path["parameters"][0] == path_parameters


@pytest.mark.skip(reason='Failed due to global variables.')
def test_object_field(app):

field = doc.Object(TestSchema)
assert field.serialize() == {"type": "object", "$ref": "#/definitions/TestSchema"}

@app.get("/")
@doc.consumes(field, location="body", required=True)
def test(request):
return text("test")

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
path = swagger_json["paths"]["/"]["get"]
assert path["parameters"][0] == {
"required": True,
"in": "body",
"name": None,
"type": "object",
"schema": {"$ref": "#/definitions/TestSchema"},
}

assert swagger_json["definitions"]["TestSchema"] == {
"type": "object",
"properties": {},
}

0 comments on commit 2a9f4d5

Please sign in to comment.