make sure test keys are filtered out of stats

This commit is contained in:
Leo Hemsted
2018-07-03 10:39:39 +01:00
parent 22496d9161
commit 1589bb58bb
3 changed files with 15 additions and 7 deletions

View File

@@ -7,7 +7,7 @@ from sqlalchemy.sql.expression import literal
from sqlalchemy.types import DateTime from sqlalchemy.types import DateTime
from app import db from app import db
from app.models import Notification, NotificationHistory, FactNotificationStatus from app.models import Notification, NotificationHistory, FactNotificationStatus, KEY_TYPE_TEST
from app.utils import convert_bst_to_utc, get_london_midnight_in_utc from app.utils import convert_bst_to_utc, get_london_midnight_in_utc
@@ -87,6 +87,7 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service
FactNotificationStatus.service_id == service_id, FactNotificationStatus.service_id == service_id,
FactNotificationStatus.bst_date >= start_date, FactNotificationStatus.bst_date >= start_date,
FactNotificationStatus.bst_date < end_date, FactNotificationStatus.bst_date < end_date,
FactNotificationStatus.key_type != KEY_TYPE_TEST
).group_by( ).group_by(
func.date_trunc('month', FactNotificationStatus.bst_date).label('month'), func.date_trunc('month', FactNotificationStatus.bst_date).label('month'),
FactNotificationStatus.notification_type, FactNotificationStatus.notification_type,
@@ -104,7 +105,8 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
).filter( ).filter(
Notification.created_at >= get_london_midnight_in_utc(bst_day), Notification.created_at >= get_london_midnight_in_utc(bst_day),
Notification.created_at < get_london_midnight_in_utc(bst_day + timedelta(days=1)), Notification.created_at < get_london_midnight_in_utc(bst_day + timedelta(days=1)),
Notification.service_id == service_id Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST
).group_by( ).group_by(
Notification.notification_type, Notification.notification_type,
Notification.status Notification.status

View File

@@ -92,7 +92,7 @@ def create_empty_monthly_notification_status_stats_dict(year):
# nested dicts - data[month][template type][status] = count # nested dicts - data[month][template type][status] = count
return { return {
convert_utc_to_bst(start).strftime('%Y-%m'): { convert_utc_to_bst(start).strftime('%Y-%m'): {
template_type: defaultdict(int) template_type: {}
for template_type in TEMPLATE_TYPES for template_type in TEMPLATE_TYPES
} }
for start in utc_month_starts for start in utc_month_starts

View File

@@ -7,7 +7,7 @@ from app.dao.fact_notification_status_dao import (
fetch_notification_status_for_service_by_month, fetch_notification_status_for_service_by_month,
fetch_notification_status_for_service_for_day, fetch_notification_status_for_service_for_day,
) )
from app.models import FactNotificationStatus from app.models import FactNotificationStatus, KEY_TYPE_TEST, KEY_TYPE_TEAM
from tests.app.db import create_notification, create_service, create_template, create_ft_notification_status from tests.app.db import create_notification, create_service, create_template, create_ft_notification_status
@@ -104,6 +104,8 @@ def test_fetch_notification_status_for_service_by_month(notify_db_session):
create_ft_notification_status(date(2017, 3, 1), 'sms', service_1) create_ft_notification_status(date(2017, 3, 1), 'sms', service_1)
# not included - wrong service # not included - wrong service
create_ft_notification_status(date(2018, 1, 3), 'sms', service_2) create_ft_notification_status(date(2018, 1, 3), 'sms', service_2)
# not included - test keys
create_ft_notification_status(date(2018, 1, 3), 'sms', service_1, key_type=KEY_TYPE_TEST)
results = sorted( results = sorted(
fetch_notification_status_for_service_by_month(date(2018, 1, 1), date(2018, 2, 28), service_1.id), fetch_notification_status_for_service_by_month(date(2018, 1, 1), date(2018, 2, 28), service_1.id),
@@ -146,10 +148,14 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session):
# included # included
create_notification(service_1.templates[0], created_at=datetime(2018, 5, 31, 23, 0, 0)) create_notification(service_1.templates[0], created_at=datetime(2018, 5, 31, 23, 0, 0))
create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 22, 59, 0)) create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 22, 59, 0))
create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 22, 59, 0), status='delivered') create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 12, 0, 0), key_type=KEY_TYPE_TEAM)
create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 12, 0, 0), status='delivered')
# test key
create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 12, 0, 0), key_type=KEY_TYPE_TEST)
# wrong service # wrong service
create_notification(service_2.templates[0], created_at=datetime(2018, 5, 31, 23, 0, 0)) create_notification(service_2.templates[0], created_at=datetime(2018, 6, 1, 12, 0, 0))
# tomorrow (somehow) # tomorrow (somehow)
create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 23, 0, 0)) create_notification(service_1.templates[0], created_at=datetime(2018, 6, 1, 23, 0, 0))
@@ -163,7 +169,7 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session):
assert results[0].month == datetime(2018, 6, 1, 0, 0) assert results[0].month == datetime(2018, 6, 1, 0, 0)
assert results[0].notification_type == 'sms' assert results[0].notification_type == 'sms'
assert results[0].notification_status == 'created' assert results[0].notification_status == 'created'
assert results[0].count == 2 assert results[0].count == 3
assert results[1].month == datetime(2018, 6, 1, 0, 0) assert results[1].month == datetime(2018, 6, 1, 0, 0)
assert results[1].notification_type == 'sms' assert results[1].notification_type == 'sms'