mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-29 19:08:22 -04:00
Add Template name to response and filter by year
The template name should be returned for the response and the user will pick a year, so ths adds those two features to the notifications/templates_usage/monthly endpoint and added some tests to test the functionality.
This commit is contained in:
@@ -1054,14 +1054,14 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_no_stats_tod
|
||||
template_two = create_sample_template(notify_db, notify_db_session, template_name='2')
|
||||
|
||||
n = notification_history(created_at=datetime(2017, 10, 1), sample_template=template_one)
|
||||
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
|
||||
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
|
||||
notification_history(created_at=datetime(2017, 4, 1), sample_template=template_two)
|
||||
notification_history(created_at=datetime(2017, 4, 1), sample_template=template_two)
|
||||
notification_history(created_at=datetime.now(), sample_template=template_two)
|
||||
|
||||
daily_stats_template_usage_by_month()
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id),
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
@@ -1069,7 +1069,7 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_no_stats_tod
|
||||
|
||||
assert result[0].template_id == template_two.id
|
||||
assert result[0].month == 4
|
||||
assert result[0].year == 2016
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 2
|
||||
|
||||
assert result[1].template_id == template_one.id
|
||||
@@ -1107,7 +1107,7 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_add_to_histo
|
||||
daily_stats_template_usage_by_month()
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id),
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
@@ -1139,7 +1139,7 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_add_to_histo
|
||||
)
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id),
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
@@ -1190,7 +1190,7 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_add_old
|
||||
daily_stats_template_usage_by_month()
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id),
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
@@ -1215,7 +1215,78 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_add_old
|
||||
)
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id),
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
assert len(result) == 2
|
||||
|
||||
|
||||
@freeze_time("2017-11-10 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_get_this_year_only(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service
|
||||
):
|
||||
notification_history = functools.partial(
|
||||
create_notification_history,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
status='delivered'
|
||||
)
|
||||
|
||||
template_one = create_sample_template(notify_db, notify_db_session, template_name='1')
|
||||
template_two = create_sample_template(notify_db, notify_db_session, template_name='2')
|
||||
template_three = create_sample_template(notify_db, notify_db_session, template_name='3')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
month = date.month
|
||||
year = date.year
|
||||
|
||||
n = notification_history(created_at=datetime(2016, 9, 1), sample_template=template_one)
|
||||
notification_history(created_at=datetime(year, month, day) - timedelta(days=1), sample_template=template_two)
|
||||
notification_history(created_at=datetime(year, month, day) - timedelta(days=1), sample_template=template_two)
|
||||
|
||||
daily_stats_template_usage_by_month()
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
assert result[0].template_id == template_two.id
|
||||
assert result[0].month == 11
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 2
|
||||
|
||||
create_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
template=template_three,
|
||||
created_at=datetime.utcnow() - timedelta(days=2)
|
||||
)
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
create_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
template=template_three,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
|
||||
key=lambda x: (x.month, x.year)
|
||||
)
|
||||
|
||||
|
||||
@@ -68,6 +68,37 @@ def test_dao_get_template_usage_stats_by_service(sample_service):
|
||||
db.session.add(stats1)
|
||||
db.session.add(stats2)
|
||||
|
||||
result = dao_get_template_usage_stats_by_service(sample_service.id)
|
||||
result = dao_get_template_usage_stats_by_service(sample_service.id, 2017)
|
||||
|
||||
assert len(result) == 2
|
||||
|
||||
|
||||
def test_dao_get_template_usage_stats_by_service_specific_year(sample_service):
|
||||
|
||||
email_template = create_template(service=sample_service, template_type="email")
|
||||
|
||||
stats1 = StatsTemplateUsageByMonth(
|
||||
template_id=email_template.id,
|
||||
month=1,
|
||||
year=2016,
|
||||
count=10
|
||||
)
|
||||
|
||||
stats2 = StatsTemplateUsageByMonth(
|
||||
template_id=email_template.id,
|
||||
month=2,
|
||||
year=2017,
|
||||
count=10
|
||||
)
|
||||
|
||||
db.session.add(stats1)
|
||||
db.session.add(stats2)
|
||||
|
||||
result = dao_get_template_usage_stats_by_service(sample_service.id, 2017)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].template_id == email_template.id
|
||||
assert result[0].name == email_template.name
|
||||
assert result[0].month == 2
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 10
|
||||
|
||||
@@ -1984,6 +1984,7 @@ def test_get_template_usage_by_month_returns_correct_data(
|
||||
assert len(resp_json) == 1
|
||||
|
||||
assert resp_json[0]["template_id"] == str(sample_template.id)
|
||||
assert resp_json[0]["name"] == sample_template.name
|
||||
assert resp_json[0]["month"] == 4
|
||||
assert resp_json[0]["year"] == 2016
|
||||
assert resp_json[0]["count"] == 5
|
||||
|
||||
Reference in New Issue
Block a user