Add tests for get_complaint_count endpoint

* Added unit tests for the get_complaint_count endpoint
* Updated the schema to use 'date' format instead of 'datetime'
* Updated the complaint endpoint to convert start_date and end_date to
be dates instead of strings
This commit is contained in:
Katie Smith
2018-06-12 12:10:58 +01:00
parent 28beeebbf4
commit 7f4b828aff
3 changed files with 49 additions and 14 deletions

View File

@@ -23,19 +23,14 @@ def get_all_complaints():
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
def get_complaint_count():
request_json = request.args.to_dict
start_date = None
end_date = None
# TODO: unit tests have yet to be written, need to test setting start and end date
if request_json:
validate(request_json, complaint_count_request)
start_date = request_json.get('start_date', None)
end_date = request_json.get('end_date', None)
if not start_date:
start_date = datetime.utcnow().date()
if not end_date:
end_date = datetime.utcnow().date()
if request.args:
validate(request.args, complaint_count_request)
# If start and end date are not set, we are expecting today's stats.
today = str(datetime.utcnow().date())
start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
count_of_complaints = fetch_count_of_complaints(start_date=start_date, end_date=end_date)
return jsonify(count_of_complaints), 200

View File

@@ -5,7 +5,7 @@ complaint_count_request = {
"type": "object",
"title": "Complaint count request",
"properties": {
"start_date": {"type": ["string", "null"], "format": "datetime"},
"end_date": {"type": ["string", "null"], "format": "datetime"},
"start_date": {"type": ["string", "null"], "format": "date"},
"end_date": {"type": ["string", "null"], "format": "date"},
}
}