mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 21:48:49 -04:00
Merge pull request #1399 from alphagov/rc-monthly-template-usage-endpoint
Added a template_type to the rest call and Aggregation Bug Fix
This commit is contained in:
@@ -1069,11 +1069,15 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_no_stats_tod
|
||||
assert len(result) == 2
|
||||
|
||||
assert result[0].template_id == template_two.id
|
||||
assert result[0].name == template_two.name
|
||||
assert result[0].template_type == template_two.template_type
|
||||
assert result[0].month == 4
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 2
|
||||
|
||||
assert result[1].template_id == template_one.id
|
||||
assert result[1].name == template_one.name
|
||||
assert result[1].template_type == template_two.template_type
|
||||
assert result[1].month == 10
|
||||
assert result[1].year == 2017
|
||||
assert result[1].count == 1
|
||||
@@ -1115,11 +1119,15 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_add_to_histo
|
||||
assert len(result) == 2
|
||||
|
||||
assert result[0].template_id == template_one.id
|
||||
assert result[0].name == template_one.name
|
||||
assert result[0].template_type == template_one.template_type
|
||||
assert result[0].month == 9
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 1
|
||||
|
||||
assert result[1].template_id == template_two.id
|
||||
assert result[1].name == template_two.name
|
||||
assert result[1].template_type == template_two.template_type
|
||||
assert result[1].month == 11
|
||||
assert result[1].year == 2017
|
||||
assert result[1].count == 2
|
||||
@@ -1147,16 +1155,22 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_add_to_histo
|
||||
assert len(result) == 3
|
||||
|
||||
assert result[0].template_id == template_one.id
|
||||
assert result[0].name == template_one.name
|
||||
assert result[0].template_type == template_one.template_type
|
||||
assert result[0].month == 9
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 1
|
||||
|
||||
assert result[1].template_id == template_two.id
|
||||
assert result[1].name == template_two.name
|
||||
assert result[1].template_type == template_two.template_type
|
||||
assert result[1].month == month
|
||||
assert result[1].year == year
|
||||
assert result[1].count == 3
|
||||
|
||||
assert result[2].template_id == template_three.id
|
||||
assert result[2].name == template_three.name
|
||||
assert result[2].template_type == template_three.template_type
|
||||
assert result[2].month == 11
|
||||
assert result[2].year == 2017
|
||||
assert result[2].count == 1
|
||||
@@ -1175,9 +1189,9 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_add_old
|
||||
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')
|
||||
template_one = create_sample_template(notify_db, notify_db_session, template_name='1', template_type='email')
|
||||
template_two = create_sample_template(notify_db, notify_db_session, template_name='2', template_type='sms')
|
||||
template_three = create_sample_template(notify_db, notify_db_session, template_name='3', template_type='letter')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
@@ -1198,11 +1212,15 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_add_old
|
||||
assert len(result) == 2
|
||||
|
||||
assert result[0].template_id == template_one.id
|
||||
assert result[0].name == template_one.name
|
||||
assert result[0].template_type == template_one.template_type
|
||||
assert result[0].month == 9
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 1
|
||||
|
||||
assert result[1].template_id == template_two.id
|
||||
assert result[1].name == template_two.name
|
||||
assert result[1].template_type == template_two.template_type
|
||||
assert result[1].month == 11
|
||||
assert result[1].year == 2017
|
||||
assert result[1].count == 2
|
||||
@@ -1236,9 +1254,9 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_get_this_yea
|
||||
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')
|
||||
template_one = create_sample_template(notify_db, notify_db_session, template_name='1', template_type='email')
|
||||
template_two = create_sample_template(notify_db, notify_db_session, template_name='2', template_type='sms')
|
||||
template_three = create_sample_template(notify_db, notify_db_session, template_name='3', template_type='letter')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
@@ -1259,6 +1277,8 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_get_this_yea
|
||||
assert len(result) == 1
|
||||
|
||||
assert result[0].template_id == template_two.id
|
||||
assert result[0].name == template_two.name
|
||||
assert result[0].template_type == template_two.template_type
|
||||
assert result[0].month == 11
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 2
|
||||
@@ -1292,3 +1312,71 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_get_this_yea
|
||||
)
|
||||
|
||||
assert len(result) == 2
|
||||
|
||||
|
||||
@freeze_time("2017-11-10 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_combined_historical_current(
|
||||
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')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
month = date.month
|
||||
year = date.year
|
||||
|
||||
n = notification_history(created_at=datetime(year, month, day) - timedelta(days=30), sample_template=template_one)
|
||||
|
||||
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_one.id
|
||||
assert result[0].name == template_one.name
|
||||
assert result[0].template_type == template_one.template_type
|
||||
assert result[0].month == 10
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 1
|
||||
|
||||
create_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
template=template_one,
|
||||
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)
|
||||
)
|
||||
|
||||
assert len(result) == 2
|
||||
|
||||
assert result[0].template_id == template_one.id
|
||||
assert result[0].name == template_one.name
|
||||
assert result[0].template_type == template_one.template_type
|
||||
assert result[0].month == 10
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 1
|
||||
|
||||
assert result[1].template_id == template_one.id
|
||||
assert result[1].name == template_one.name
|
||||
assert result[1].template_type == template_one.template_type
|
||||
assert result[1].month == 11
|
||||
assert result[1].year == 2017
|
||||
assert result[1].count == 1
|
||||
|
||||
@@ -99,6 +99,7 @@ def test_dao_get_template_usage_stats_by_service_specific_year(sample_service):
|
||||
assert len(result) == 1
|
||||
assert result[0].template_id == email_template.id
|
||||
assert result[0].name == email_template.name
|
||||
assert result[0].template_type == email_template.template_type
|
||||
assert result[0].month == 2
|
||||
assert result[0].year == 2017
|
||||
assert result[0].count == 10
|
||||
|
||||
@@ -1865,6 +1865,7 @@ def test_get_template_stats_by_month_returns_correct_data(notify_db, notify_db_s
|
||||
assert resp_json["2016-05"][str(sample_template.id)]["counts"]["permanent-failure"] == 1
|
||||
|
||||
|
||||
@freeze_time('2017-11-11 02:00')
|
||||
def test_get_template_usage_by_month_returns_correct_data(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -1918,15 +1919,24 @@ def test_get_template_usage_by_month_returns_correct_data(
|
||||
resp_json = json.loads(resp.get_data(as_text=True)).get('stats')
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert len(resp_json) == 1
|
||||
assert len(resp_json) == 2
|
||||
|
||||
assert resp_json[0]["template_id"] == str(sample_template.id)
|
||||
assert resp_json[0]["name"] == sample_template.name
|
||||
assert resp_json[0]["type"] == sample_template.template_type
|
||||
assert resp_json[0]["month"] == 4
|
||||
assert resp_json[0]["year"] == 2016
|
||||
assert resp_json[0]["count"] == 5
|
||||
assert resp_json[0]["count"] == 4
|
||||
|
||||
assert resp_json[1]["template_id"] == str(sample_template.id)
|
||||
assert resp_json[1]["name"] == sample_template.name
|
||||
assert resp_json[1]["type"] == sample_template.template_type
|
||||
assert resp_json[1]["month"] == 11
|
||||
assert resp_json[1]["year"] == 2017
|
||||
assert resp_json[1]["count"] == 1
|
||||
|
||||
|
||||
@freeze_time('2017-11-11 02:00')
|
||||
def test_get_template_usage_by_month_returns_two_templates(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -1983,19 +1993,30 @@ def test_get_template_usage_by_month_returns_two_templates(
|
||||
resp_json = json.loads(resp.get_data(as_text=True)).get('stats')
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert len(resp_json) == 2
|
||||
assert len(resp_json) == 3
|
||||
|
||||
resp_json = sorted(resp_json, key=lambda k: k.get('count', 0))
|
||||
resp_json = sorted(resp_json, key=lambda k: (k.get('year', 0), k.get('month', 0), k.get('count', 0)))
|
||||
|
||||
assert resp_json[0]["template_id"] == str(template_one.id)
|
||||
assert resp_json[0]["name"] == template_one.name
|
||||
assert resp_json[0]["type"] == template_one.template_type
|
||||
assert resp_json[0]["month"] == 4
|
||||
assert resp_json[0]["year"] == 2016
|
||||
assert resp_json[0]["count"] == 1
|
||||
|
||||
assert resp_json[1]["template_id"] == str(sample_template.id)
|
||||
assert resp_json[1]["name"] == sample_template.name
|
||||
assert resp_json[1]["type"] == sample_template.template_type
|
||||
assert resp_json[1]["month"] == 4
|
||||
assert resp_json[1]["year"] == 2016
|
||||
assert resp_json[1]["count"] == 4
|
||||
assert resp_json[1]["count"] == 3
|
||||
|
||||
assert resp_json[2]["template_id"] == str(sample_template.id)
|
||||
assert resp_json[2]["name"] == sample_template.name
|
||||
assert resp_json[2]["type"] == sample_template.template_type
|
||||
assert resp_json[2]["month"] == 11
|
||||
assert resp_json[2]["year"] == 2017
|
||||
assert resp_json[2]["count"] == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize('query_string, expected_status, expected_json', [
|
||||
|
||||
Reference in New Issue
Block a user