Skip to content

Commit

Permalink
Further tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roo-oliv committed Jul 1, 2017
1 parent 1d47f64 commit b0a017c
Showing 1 changed file with 182 additions and 11 deletions.
193 changes: 182 additions & 11 deletions examples/custom_validation_function.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import jsonschema
try:
import simplejson as json
Expand All @@ -22,6 +23,31 @@ def validate(data, schema):
del data['_id']


def timestamping_validate(data, schema):
"""
Custom validation function which inserts a timestamp for when the
validation occurred
"""
jsonschema.validate(data, schema)
data['timestamp'] = str(time.time())


def special_validate(data, schema):
"""
Custom validation function which marks inserts an special flag
depending on the cat's name
"""
jsonschema.validate(data, schema)
data['special'] = str(data['name'] == 'Garfield').lower()


def regular_validate(data, schema):
"""
Regular validation function
"""
jsonschema.validate(data, schema)


app = Flask(__name__)
swag = Swagger(app, validation_function=validate)

Expand All @@ -30,7 +56,8 @@ def validate(data, schema):
@swag.validate('Cat')
def create_cat():
"""
Cat creation endpoint
Cat creation endpoint which drops '_id' parameters when present in
request data
---
tags:
- cat
Expand Down Expand Up @@ -68,6 +95,102 @@ def create_cat():
return jsonify(request.json), HTTPStatus.OK


@app.route('/timestamped/cat', methods=['POST'])
@swag.validate('Cat', validation_function=timestamping_validate)
def create_timestamped_cat():
"""
Cat creation endpoint which timestamps validated data
---
tags:
- cat
summary: Creates a new cat
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: body
description:
Cat object that needs to be persisted to the database
required: true
schema:
$ref: '#/definitions/Cat'
responses:
200:
description: Successful operation
schema:
$ref: '#/definitions/Cat'
400:
description: Invalid input
"""
return jsonify(request.json), HTTPStatus.OK


@app.route('/special/cat', methods=['POST'])
@swag.validate('Cat', validation_function=special_validate)
def create_special_cat():
"""
Cat creation endpoint which timestamps validated data
---
tags:
- cat
summary: Creates a new cat
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: body
description:
Cat object that needs to be persisted to the database
required: true
schema:
$ref: '#/definitions/Cat'
responses:
200:
description: Successful operation
schema:
$ref: '#/definitions/Cat'
400:
description: Invalid input
"""
return jsonify(request.json), HTTPStatus.OK


@app.route('/regular/cat', methods=['POST'])
@swag.validate('Cat', validation_function=regular_validate)
def create_regular_cat():
"""
Cat creation endpoint
---
tags:
- cat
summary: Creates a new cat
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: body
description:
Cat object that needs to be persisted to the database
required: true
schema:
$ref: '#/definitions/Cat'
responses:
200:
description: Successful operation
schema:
$ref: '#/definitions/Cat'
400:
description: Invalid input
"""
return jsonify(request.json), HTTPStatus.OK


def test_swag(client, specs_data):
"""
This test is runs automatically in Travis CI
Expand All @@ -83,16 +206,64 @@ def test_swag(client, specs_data):
"address": "MGM, 245 N. Beverly Drive, Beverly Hills, CA 90210"
}
"""
response = client.post('/cat', data=cat, content_type='application/json')
assert response.status_code == HTTPStatus.OK

sent = json.loads(cat)
received = json.loads(response.data.decode('utf-8'))
assert received.get('_id') is None
try:
assert received.viewitems() < sent.viewitems()
except AttributeError:
assert received.items() < sent.items()
with client.post(
'/cat', data=cat, content_type='application/json') as response:
assert response.status_code == HTTPStatus.OK

sent = json.loads(cat)
received = json.loads(response.data.decode('utf-8'))
assert received.get('_id') is None
assert received.get('timestamp') is None
assert received.get('special') is None
try:
assert received.viewitems() < sent.viewitems()
except AttributeError:
assert received.items() < sent.items()

with client.post(
'/timestamped/cat', data=cat,
content_type='application/json') as response:
assert response.status_code == HTTPStatus.OK

sent = json.loads(cat)
received = json.loads(response.data.decode('utf-8'))
assert received.get('_id') == sent.get('_id')
assert received.get('timestamp') is not None
assert received.get('special') is None
try:
assert received.viewitems() > sent.viewitems()
except AttributeError:
assert received.items() > sent.items()

with client.post(
'/special/cat', data=cat,
content_type='application/json') as response:
assert response.status_code == HTTPStatus.OK

sent = json.loads(cat)
received = json.loads(response.data.decode('utf-8'))
assert received.get('_id') == sent.get('_id')
assert received.get('timestamp') is None
assert received.get('special') is not None
try:
assert received.viewitems() > sent.viewitems()
except AttributeError:
assert received.items() > sent.items()

with client.post(
'/regular/cat', data=cat,
content_type='application/json') as response:
assert response.status_code == HTTPStatus.OK

sent = json.loads(cat)
received = json.loads(response.data.decode('utf-8'))
assert received.get('_id') == sent.get('_id')
assert received.get('timestamp') is None
assert received.get('special') is None
try:
assert received.viewitems() == sent.viewitems()
except AttributeError:
assert received.items() == sent.items()

if __name__ == "__main__":
app.run(debug=True)

0 comments on commit b0a017c

Please sign in to comment.