Simplify the code in the query.

The date in the notifications table should always be the most recent date for the template.
Removed the template_type param for the query as well.
Simplified the tests.
This commit is contained in:
Rebecca Law
2020-02-05 16:43:17 +00:00
parent 3a32c35dd2
commit dec42b06cc
4 changed files with 23 additions and 54 deletions

View File

@@ -72,7 +72,18 @@ def dao_get_last_template_usage(template_id, template_type, service_id):
@statsd(namespace="dao") @statsd(namespace="dao")
def dao_get_last_date_template_was_used(template_id, template_type, service_id): def dao_get_last_date_template_was_used(template_id, service_id):
last_date_from_notifications = db.session.query(
functions.max(Notification.created_at)
).filter(
Notification.service_id == service_id,
Notification.template_id == template_id,
Notification.key_type != KEY_TYPE_TEST
).scalar()
if last_date_from_notifications:
return last_date_from_notifications
last_date = db.session.query( last_date = db.session.query(
functions.max(FactNotificationStatus.bst_date) functions.max(FactNotificationStatus.bst_date)
).filter( ).filter(
@@ -80,26 +91,7 @@ def dao_get_last_date_template_was_used(template_id, template_type, service_id):
FactNotificationStatus.key_type != KEY_TYPE_TEST FactNotificationStatus.key_type != KEY_TYPE_TEST
).scalar() ).scalar()
last_date_from_notifications = db.session.query( return last_date
functions.max(Notification.created_at)
).filter(
Notification.service_id == service_id,
Notification.notification_type == template_type,
Notification.template_id == template_id,
Notification.key_type != KEY_TYPE_TEST
).scalar()
if last_date and last_date_from_notifications:
if datetime.combine(last_date, datetime.utcnow().min.time()) >= last_date_from_notifications:
return last_date
else:
return last_date_from_notifications
elif not last_date:
return last_date_from_notifications
elif not last_date_from_notifications:
return last_date
else:
return None
@statsd(namespace="dao") @statsd(namespace="dao")

View File

@@ -58,10 +58,10 @@ def get_template_statistics_for_template_id(service_id, template_id):
@template_statistics.route('/last-used/<uuid:template_id>') @template_statistics.route('/last-used/<uuid:template_id>')
def get_last_used_datetime_for_template(service_id, template_id): def get_last_used_datetime_for_template(service_id, template_id):
template = dao_get_template_by_id_and_service_id(template_id, service_id) # Check the template and service exist
dao_get_template_by_id_and_service_id(template_id, service_id)
last_date_used = dao_get_last_date_template_was_used(template_id=template_id, last_date_used = dao_get_last_date_template_was_used(template_id=template_id,
template_type=template.template_type,
service_id=service_id) service_id=service_id)
return jsonify(last_date_used=last_date_used.strftime(DATETIME_FORMAT)) return jsonify(last_date_used=last_date_used.strftime(DATETIME_FORMAT))

View File

@@ -56,54 +56,31 @@ def test_last_template_usage_should_be_able_to_get_no_template_usage_history_if_
assert not results assert not results
@pytest.mark.parametrize('last_status_date, last_notification_date',
[((datetime.utcnow() - timedelta(days=2)).date(), None),
((datetime.utcnow() - timedelta(days=2)).date(), datetime.utcnow() - timedelta(days=3))]
)
def test_dao_get_last_date_template_was_used_returns_bst_date_from_stats_table( def test_dao_get_last_date_template_was_used_returns_bst_date_from_stats_table(
sample_template, last_status_date, last_notification_date sample_template
): ):
last_status_date = (datetime.utcnow() - timedelta(days=2)).date()
create_ft_notification_status(bst_date=last_status_date, create_ft_notification_status(bst_date=last_status_date,
template=sample_template) template=sample_template)
if last_notification_date:
create_notification(template=sample_template, created_at=last_notification_date)
last_used_date = dao_get_last_date_template_was_used(template_id=sample_template.id, last_used_date = dao_get_last_date_template_was_used(template_id=sample_template.id,
template_type='sms',
service_id=sample_template.service_id) service_id=sample_template.service_id)
assert last_used_date == last_status_date assert last_used_date == last_status_date
@pytest.mark.parametrize('last_notification_date, last_status_date',
[(datetime.utcnow() - timedelta(hours=2), None),
(datetime.utcnow() - timedelta(hours=2), (datetime.utcnow() - timedelta(days=2)).date())]
)
def test_dao_get_last_date_template_was_used_returns_created_at_from_notifications( def test_dao_get_last_date_template_was_used_returns_created_at_from_notifications(
sample_template, last_status_date, last_notification_date sample_template
): ):
last_notification_date = datetime.utcnow() - timedelta(hours=2)
create_notification(template=sample_template, created_at=last_notification_date) create_notification(template=sample_template, created_at=last_notification_date)
if last_status_date: last_status_date = (datetime.utcnow() - timedelta(days=2)).date()
create_ft_notification_status(bst_date=last_status_date, template=sample_template) create_ft_notification_status(bst_date=last_status_date, template=sample_template)
last_used_date = dao_get_last_date_template_was_used(template_id=sample_template.id, last_used_date = dao_get_last_date_template_was_used(template_id=sample_template.id,
template_type='sms',
service_id=sample_template.service_id) service_id=sample_template.service_id)
assert last_used_date == last_notification_date assert last_used_date == last_notification_date
def test_dao_get_last_date_template_was_used_returns_none_if_never_used(sample_template): def test_dao_get_last_date_template_was_used_returns_none_if_never_used(sample_template):
assert not dao_get_last_date_template_was_used(template_id=sample_template.id, assert not dao_get_last_date_template_was_used(template_id=sample_template.id,
template_type='sms',
service_id=sample_template.service_id) service_id=sample_template.service_id)
def test_dao_get_last_date_template_was_used_correct_date(sample_template):
date_from_notification = datetime.utcnow() - timedelta(hours=2)
create_notification(template=sample_template, created_at=date_from_notification)
date_from_ft_status = (datetime.utcnow() - timedelta(days=2)).date()
create_ft_notification_status(bst_date=date_from_ft_status,
template=sample_template)
actual_result = dao_get_last_date_template_was_used(template_id=sample_template.id,
template_type='sms',
service_id=sample_template.service_id)
assert actual_result == date_from_notification

View File

@@ -221,7 +221,7 @@ def test_get_last_used_datetime_for_template_returns_400_if_service_does_not_exi
) )
def test_get_last_used_datetime_for_template_returns_400_if_template_does_not_exist( def test_get_last_used_datetime_for_template_returns_404_if_template_does_not_exist(
admin_request, sample_template admin_request, sample_template
): ):
admin_request.get( admin_request.get(