mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-10 15:22:24 -05:00
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:
@@ -23,19 +23,14 @@ def get_all_complaints():
|
|||||||
|
|
||||||
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
|
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
|
||||||
def get_complaint_count():
|
def get_complaint_count():
|
||||||
request_json = request.args.to_dict
|
if request.args:
|
||||||
start_date = None
|
validate(request.args, complaint_count_request)
|
||||||
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 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)
|
count_of_complaints = fetch_count_of_complaints(start_date=start_date, end_date=end_date)
|
||||||
|
|
||||||
return jsonify(count_of_complaints), 200
|
return jsonify(count_of_complaints), 200
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ complaint_count_request = {
|
|||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Complaint count request",
|
"title": "Complaint count request",
|
||||||
"properties": {
|
"properties": {
|
||||||
"start_date": {"type": ["string", "null"], "format": "datetime"},
|
"start_date": {"type": ["string", "null"], "format": "date"},
|
||||||
"end_date": {"type": ["string", "null"], "format": "datetime"},
|
"end_date": {"type": ["string", "null"], "format": "date"},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
|
from datetime import date
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from flask import url_for
|
||||||
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
from tests.app.db import create_complaint, create_service, create_template, create_notification
|
from tests.app.db import create_complaint, create_service, create_template, create_notification
|
||||||
|
|
||||||
@@ -22,3 +26,39 @@ def test_get_all_complaints_returns_empty_list(client):
|
|||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json.loads(response.get_data(as_text=True)) == []
|
assert json.loads(response.get_data(as_text=True)) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_complaint_with_start_and_end_date_passes_these_to_dao_function(mocker, client):
|
||||||
|
start_date = date(2018, 6, 11)
|
||||||
|
end_date = date(2018, 6, 11)
|
||||||
|
dao_mock = mocker.patch('app.complaint.complaint_rest.fetch_count_of_complaints', return_value=3)
|
||||||
|
response = client.get(
|
||||||
|
url_for('complaint.get_complaint_count', start_date=start_date, end_date=end_date),
|
||||||
|
headers=[create_authorization_header()]
|
||||||
|
)
|
||||||
|
|
||||||
|
dao_mock.assert_called_once_with(start_date=start_date, end_date=end_date)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json.loads(response.get_data(as_text=True)) == 3
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2018-06-01 11:00:00")
|
||||||
|
def test_get_complaint_sets_start_and_end_date_to_today_if_not_specified(mocker, client):
|
||||||
|
dao_mock = mocker.patch('app.complaint.complaint_rest.fetch_count_of_complaints', return_value=5)
|
||||||
|
response = client.get(url_for('complaint.get_complaint_count'), headers=[create_authorization_header()])
|
||||||
|
|
||||||
|
dao_mock.assert_called_once_with(start_date=date.today(), end_date=date.today())
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json.loads(response.get_data(as_text=True)) == 5
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_complaint_with_invalid_data_returns_400_status_code(client):
|
||||||
|
start_date = '1234-56-78'
|
||||||
|
response = client.get(
|
||||||
|
url_for('complaint.get_complaint_count', start_date=start_date),
|
||||||
|
headers=[create_authorization_header()]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.json['errors'][0]['message'] == 'start_date time data {} does not match format %Y-%m-%d'.format(
|
||||||
|
start_date)
|
||||||
|
|||||||
Reference in New Issue
Block a user