mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
There is no need to have a separate table to store template monthly statistics. It's easy enough to aggregate the stats from ft_notification_status.
This removes the nightly task, and all the dao methods. The next PR will remove the table.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
@@ -7,7 +7,6 @@ from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
from sqlalchemy.orm.exc import FlushError, NoResultFound
|
||||
|
||||
from app import db
|
||||
from app.celery.scheduled_tasks import daily_stats_template_usage_by_month
|
||||
from app.dao.inbound_numbers_dao import (
|
||||
dao_set_inbound_number_to_service,
|
||||
dao_get_available_inbound_numbers,
|
||||
@@ -32,7 +31,6 @@ from app.dao.services_dao import (
|
||||
dao_fetch_active_users_for_service,
|
||||
dao_fetch_service_by_inbound_number,
|
||||
dao_fetch_monthly_historical_stats_by_template,
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service
|
||||
)
|
||||
from app.dao.users_dao import save_model_user, create_user_code
|
||||
from app.models import (
|
||||
@@ -901,441 +899,9 @@ def test_dao_fetch_monthly_historical_stats_by_template(notify_db_session):
|
||||
assert result[1].count == 1
|
||||
|
||||
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_no_stats_today(
|
||||
notify_db_session,
|
||||
):
|
||||
service = create_service()
|
||||
template_one = create_template(service=service, template_name='1')
|
||||
template_two = create_template(service=service, template_name='2')
|
||||
|
||||
n = create_notification(created_at=datetime(2017, 10, 1), template=template_one, status='delivered')
|
||||
create_notification(created_at=datetime(2017, 4, 1), template=template_two, status='delivered')
|
||||
create_notification(created_at=datetime(2017, 4, 1), template=template_two, status='delivered')
|
||||
create_notification(created_at=datetime.now(), template=template_two, status='delivered')
|
||||
|
||||
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) == 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
|
||||
|
||||
|
||||
@freeze_time("2017-11-10 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_add_to_historical(
|
||||
notify_db_session,
|
||||
):
|
||||
service = create_service()
|
||||
template_one = create_template(service=service, template_name='1')
|
||||
template_two = create_template(service=service, template_name='2')
|
||||
template_three = create_template(service=service, template_name='3')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
month = date.month
|
||||
year = date.year
|
||||
|
||||
n = create_notification(created_at=datetime(2017, 9, 1), template=template_one, status='delivered')
|
||||
create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two,
|
||||
status='delivered')
|
||||
create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two,
|
||||
status='delivered')
|
||||
|
||||
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) == 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
|
||||
|
||||
create_notification(
|
||||
template=template_three,
|
||||
created_at=datetime.now(),
|
||||
status='delivered'
|
||||
)
|
||||
create_notification(
|
||||
template=template_two,
|
||||
created_at=datetime.now(),
|
||||
status='delivered'
|
||||
)
|
||||
|
||||
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) == 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
|
||||
|
||||
|
||||
@freeze_time("2017-11-10 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_add_old_notification(
|
||||
notify_db_session,
|
||||
):
|
||||
template_one, template_three, template_two = create_email_sms_letter_template()
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
month = date.month
|
||||
year = date.year
|
||||
|
||||
n = create_notification(created_at=datetime(2017, 9, 1), template=template_one, status='delivered')
|
||||
create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two,
|
||||
status='delivered')
|
||||
create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two,
|
||||
status='delivered')
|
||||
|
||||
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) == 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
|
||||
|
||||
create_notification(
|
||||
template=template_three,
|
||||
created_at=datetime.utcnow() - timedelta(days=2),
|
||||
status='delivered'
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
|
||||
@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_session,
|
||||
):
|
||||
template_one, template_three, template_two = create_email_sms_letter_template()
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
month = date.month
|
||||
year = date.year
|
||||
|
||||
n = create_notification(created_at=datetime(2016, 9, 1), template=template_one, status='delivered')
|
||||
create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two,
|
||||
status='delivered')
|
||||
create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two,
|
||||
status='delivered')
|
||||
|
||||
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].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
|
||||
|
||||
create_notification(
|
||||
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(
|
||||
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)
|
||||
)
|
||||
|
||||
assert len(result) == 2
|
||||
|
||||
|
||||
def create_email_sms_letter_template():
|
||||
service = create_service()
|
||||
template_one = create_template(service=service, template_name='1', template_type='email')
|
||||
template_two = create_template(service=service, template_name='2', template_type='sms')
|
||||
template_three = create_template(service=service, template_name='3', template_type='letter')
|
||||
return template_one, template_three, template_two
|
||||
|
||||
|
||||
@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_session,
|
||||
):
|
||||
template_one = create_template(service=create_service(), template_name='1')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
month = date.month
|
||||
year = date.year
|
||||
|
||||
n = create_notification(status='delivered', created_at=datetime(year, month, day) - timedelta(days=30),
|
||||
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(
|
||||
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
|
||||
|
||||
|
||||
@freeze_time("2017-11-10 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_not_return_double_precision_values(
|
||||
notify_db_session,
|
||||
):
|
||||
template_one = create_template(service=create_service())
|
||||
|
||||
n = create_notification(
|
||||
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) == 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 == 11
|
||||
assert len(str(result[0].month)) == 2
|
||||
assert result[0].year == 2017
|
||||
assert len(str(result[0].year)) == 4
|
||||
assert result[0].count == 1
|
||||
|
||||
|
||||
@freeze_time("2018-03-10 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_returns_financial_year(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
):
|
||||
service = create_service()
|
||||
template_one = create_template(service=service, template_name='1', template_type='email')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
year = date.year
|
||||
|
||||
create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 1, day))
|
||||
create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 3, day))
|
||||
create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 4, day))
|
||||
create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 5, day))
|
||||
create_notification(template=template_one, status='delivered', created_at=datetime(year, 1, day))
|
||||
create_notification(template=template_one, status='delivered', created_at=datetime(year, 2, day))
|
||||
|
||||
daily_stats_template_usage_by_month()
|
||||
|
||||
n = create_notification(
|
||||
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.year, x.month)
|
||||
)
|
||||
|
||||
assert len(result) == 5
|
||||
|
||||
assert result[0].month == 4
|
||||
assert result[0].year == 2017
|
||||
assert result[1].month == 5
|
||||
assert result[1].year == 2017
|
||||
assert result[2].month == 1
|
||||
assert result[2].year == 2018
|
||||
assert result[3].month == 2
|
||||
assert result[3].year == 2018
|
||||
assert result[4].month == 3
|
||||
assert result[4].year == 2018
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2014),
|
||||
key=lambda x: (x.year, x.month)
|
||||
)
|
||||
|
||||
assert len(result) == 0
|
||||
|
||||
|
||||
@freeze_time("2018-03-10 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_only_returns_for_service(
|
||||
notify_db_session
|
||||
):
|
||||
template_one = create_template(service=create_service(), template_name='1', template_type='email')
|
||||
|
||||
date = datetime.now()
|
||||
day = date.day
|
||||
year = date.year
|
||||
|
||||
create_notification(template=template_one, created_at=datetime(year, 1, day))
|
||||
create_notification(template=template_one, created_at=datetime(year, 2, day))
|
||||
create_notification(template=template_one, created_at=datetime(year, 3, day))
|
||||
|
||||
service_two = create_service(service_name='other_service', user=create_user())
|
||||
template_two = create_template(service=service_two, template_name='1', template_type='email')
|
||||
|
||||
create_notification(template=template_two)
|
||||
create_notification(template=template_two)
|
||||
|
||||
daily_stats_template_usage_by_month()
|
||||
|
||||
x = dao_fetch_monthly_historical_usage_by_template_for_service(template_one.service_id, 2017)
|
||||
|
||||
result = sorted(
|
||||
x,
|
||||
key=lambda x: (x.year, x.month)
|
||||
)
|
||||
|
||||
assert len(result) == 3
|
||||
|
||||
result = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(service_two.id, 2017),
|
||||
key=lambda x: (x.year, x.month)
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
@freeze_time("2018-01-01 11:09:00.000000")
|
||||
def test_dao_fetch_monthly_historical_usage_by_template_for_service_ignores_test_api_keys(notify_db_session):
|
||||
service = create_service()
|
||||
template_1 = create_template(service, template_name='1')
|
||||
template_2 = create_template(service, template_name='2')
|
||||
template_3 = create_template(service, template_name='3')
|
||||
|
||||
create_notification(template_1, key_type=KEY_TYPE_TEST)
|
||||
create_notification(template_2, key_type=KEY_TYPE_TEAM)
|
||||
create_notification(template_3, key_type=KEY_TYPE_NORMAL)
|
||||
|
||||
results = sorted(
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service(service.id, 2017),
|
||||
key=lambda x: x.name
|
||||
)
|
||||
|
||||
assert len(results) == 2
|
||||
# template_1 only used with test keys
|
||||
assert results[0].template_id == template_2.id
|
||||
assert results[0].count == 1
|
||||
|
||||
assert results[1].template_id == template_3.id
|
||||
assert results[1].count == 1
|
||||
|
||||
Reference in New Issue
Block a user