don't pass running totals in to functions

or you can easily end up double-counting things. (the test written
previously returned 6)
This commit is contained in:
Leo Hemsted
2019-06-03 17:47:42 +01:00
parent 67f171f2f8
commit 2ac2cbbd37
2 changed files with 17 additions and 8 deletions

View File

@@ -310,9 +310,7 @@ def delete_notifications_older_than_retention_by_type(notification_type, qry_lim
current_app.logger.info(
"Deleting {} notifications for service id: {}".format(notification_type, f.service_id))
deleted += _delete_notifications(
deleted, notification_type, days_of_retention, f.service_id, qry_limit
)
deleted += _delete_notifications(notification_type, days_of_retention, f.service_id, qry_limit)
current_app.logger.info(
'Deleting {} notifications for services without flexible data retention'.format(notification_type))
@@ -327,16 +325,14 @@ def delete_notifications_older_than_retention_by_type(notification_type, qry_lim
notification_type, service_id, seven_days_ago, qry_limit
)
insert_update_notification_history(notification_type, seven_days_ago, service_id)
deleted += _delete_notifications(
deleted, notification_type, seven_days_ago, service_id, qry_limit
)
deleted += _delete_notifications(notification_type, seven_days_ago, service_id, qry_limit)
current_app.logger.info('Finished deleting {} notifications'.format(notification_type))
return deleted
def _delete_notifications(deleted, notification_type, date_to_delete_from, service_id, query_limit):
def _delete_notifications(notification_type, date_to_delete_from, service_id, query_limit):
subquery = db.session.query(
Notification.id
).join(NotificationHistory, NotificationHistory.id == Notification.id).filter(
@@ -345,7 +341,7 @@ def _delete_notifications(deleted, notification_type, date_to_delete_from, servi
Notification.created_at < date_to_delete_from,
).limit(query_limit).subquery()
deleted += _delete_for_query(subquery)
deleted = _delete_for_query(subquery)
subquery_for_test_keys = db.session.query(
Notification.id

View File

@@ -252,6 +252,19 @@ def test_delete_notifications_calls_subquery_multiple_times(sample_template):
assert Notification.query.count() == 0
def test_delete_notifications_returns_sum_correctly(sample_template, notify_db_session):
create_notification(template=sample_template, created_at=datetime.now() - timedelta(days=8))
create_notification(template=sample_template, created_at=datetime.now() - timedelta(days=8))
s2 = create_service(service_name='s2')
t2 = create_template(s2, template_type='sms')
create_notification(template=t2, created_at=datetime.now() - timedelta(days=8))
create_notification(template=t2, created_at=datetime.now() - timedelta(days=8))
ret = delete_notifications_older_than_retention_by_type('sms', qry_limit=1)
assert ret == 4
@pytest.mark.parametrize('notification_type', ['sms'])
def test_insert_update_notification_history(sample_service, notification_type):
template = create_template(sample_service, template_type=notification_type)