2016-04-28 12:01:27 +01:00
|
|
|
from datetime import (date, timedelta)
|
2016-04-21 11:37:38 +01:00
|
|
|
from app.models import ProviderStatistics
|
2016-06-02 09:52:47 +01:00
|
|
|
from app.dao.notifications_dao import update_provider_stats
|
2016-04-28 12:01:27 +01:00
|
|
|
from app.dao.provider_statistics_dao import (
|
|
|
|
|
get_provider_statistics, get_fragment_count)
|
2016-04-21 11:37:38 +01:00
|
|
|
from tests.app.conftest import sample_notification as create_sample_notification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_update_provider_statistics_sms(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_template,
|
2016-05-06 09:09:47 +01:00
|
|
|
mmg_provider):
|
2016-06-01 12:42:19 +01:00
|
|
|
n1 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template=sample_template)
|
2016-06-02 09:52:47 +01:00
|
|
|
update_provider_stats(n1.id, 'sms', mmg_provider.identifier)
|
2016-04-28 12:01:27 +01:00
|
|
|
provider_stats = get_provider_statistics(
|
|
|
|
|
sample_template.service,
|
2016-05-06 09:09:47 +01:00
|
|
|
providers=[mmg_provider.identifier]).one()
|
2016-04-21 11:37:38 +01:00
|
|
|
assert provider_stats.unit_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_update_provider_statistics_email(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_email_template,
|
2016-05-06 09:09:47 +01:00
|
|
|
ses_provider):
|
2016-06-01 12:42:19 +01:00
|
|
|
n1 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template=sample_email_template)
|
2016-06-02 10:00:27 +01:00
|
|
|
update_provider_stats(n1.id, 'email', ses_provider.identifier)
|
2016-04-28 12:01:27 +01:00
|
|
|
provider_stats = get_provider_statistics(
|
|
|
|
|
sample_email_template.service,
|
2016-05-06 09:09:47 +01:00
|
|
|
providers=[ses_provider.identifier]).one()
|
2016-04-21 11:37:38 +01:00
|
|
|
assert provider_stats.unit_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_update_provider_statistics_sms_multi(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_template,
|
2016-05-06 09:09:47 +01:00
|
|
|
mmg_provider):
|
2016-06-01 12:42:19 +01:00
|
|
|
n1 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template=sample_template,
|
|
|
|
|
content_char_count=160)
|
2016-06-02 09:52:47 +01:00
|
|
|
update_provider_stats(n1.id, 'sms', mmg_provider.identifier)
|
2016-06-01 12:42:19 +01:00
|
|
|
n2 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template=sample_template,
|
|
|
|
|
content_char_count=161)
|
2016-06-02 09:52:47 +01:00
|
|
|
update_provider_stats(n2.id, 'sms', mmg_provider.identifier)
|
2016-06-01 12:42:19 +01:00
|
|
|
n3 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
template=sample_template,
|
|
|
|
|
content_char_count=307)
|
2016-06-02 09:52:47 +01:00
|
|
|
update_provider_stats(n3.id, 'sms', mmg_provider.identifier)
|
2016-04-28 12:01:27 +01:00
|
|
|
provider_stats = get_provider_statistics(
|
|
|
|
|
sample_template.service,
|
2016-05-06 09:09:47 +01:00
|
|
|
providers=[mmg_provider.identifier]).one()
|
2016-04-21 11:37:38 +01:00
|
|
|
assert provider_stats.unit_count == 6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_update_provider_statistics_email_multi(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_email_template,
|
2016-05-06 09:09:47 +01:00
|
|
|
ses_provider):
|
2016-06-01 12:42:19 +01:00
|
|
|
n1 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
2016-06-13 11:38:25 +01:00
|
|
|
template=sample_email_template)
|
2016-06-02 10:00:27 +01:00
|
|
|
update_provider_stats(n1.id, 'email', ses_provider.identifier)
|
2016-06-01 12:42:19 +01:00
|
|
|
n2 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
2016-06-13 11:38:25 +01:00
|
|
|
template=sample_email_template)
|
2016-06-02 10:00:27 +01:00
|
|
|
update_provider_stats(n2.id, 'email', ses_provider.identifier)
|
2016-06-01 12:42:19 +01:00
|
|
|
n3 = create_sample_notification(
|
2016-04-21 11:37:38 +01:00
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
2016-06-13 11:38:25 +01:00
|
|
|
template=sample_email_template)
|
2016-06-02 10:00:27 +01:00
|
|
|
update_provider_stats(n3.id, 'email', ses_provider.identifier)
|
2016-04-28 12:01:27 +01:00
|
|
|
provider_stats = get_provider_statistics(
|
|
|
|
|
sample_email_template.service,
|
2016-05-06 09:09:47 +01:00
|
|
|
providers=[ses_provider.identifier]).one()
|
2016-04-21 11:37:38 +01:00
|
|
|
assert provider_stats.unit_count == 3
|
2016-04-28 12:01:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_aggregate_fragment_count(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_service,
|
2016-05-06 09:09:47 +01:00
|
|
|
mmg_provider,
|
|
|
|
|
firetext_provider,
|
|
|
|
|
ses_provider):
|
2016-04-28 12:01:27 +01:00
|
|
|
day = date.today()
|
|
|
|
|
stats_mmg = ProviderStatistics(
|
|
|
|
|
service=sample_service,
|
|
|
|
|
day=day,
|
2016-05-06 09:09:47 +01:00
|
|
|
provider_id=mmg_provider.id,
|
2016-04-28 12:01:27 +01:00
|
|
|
unit_count=2
|
|
|
|
|
)
|
2016-05-06 09:09:47 +01:00
|
|
|
|
|
|
|
|
stats_firetext = ProviderStatistics(
|
2016-04-28 12:01:27 +01:00
|
|
|
service=sample_service,
|
|
|
|
|
day=day,
|
2016-05-06 09:09:47 +01:00
|
|
|
provider_id=firetext_provider.id,
|
2016-04-28 12:01:27 +01:00
|
|
|
unit_count=3
|
|
|
|
|
)
|
2016-05-06 09:09:47 +01:00
|
|
|
|
|
|
|
|
stats_ses = ProviderStatistics(
|
2016-04-28 12:01:27 +01:00
|
|
|
service=sample_service,
|
|
|
|
|
day=day,
|
2016-05-06 09:09:47 +01:00
|
|
|
provider_id=ses_provider.id,
|
2016-04-28 12:01:27 +01:00
|
|
|
unit_count=1
|
|
|
|
|
)
|
|
|
|
|
notify_db.session.add(stats_mmg)
|
2016-05-06 09:09:47 +01:00
|
|
|
notify_db.session.add(stats_firetext)
|
|
|
|
|
notify_db.session.add(stats_ses)
|
2016-04-28 12:01:27 +01:00
|
|
|
notify_db.session.commit()
|
|
|
|
|
results = get_fragment_count(sample_service, day, day)
|
|
|
|
|
assert results['sms_count'] == 5
|
|
|
|
|
assert results['email_count'] == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_aggregate_fragment_count_over_days(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_service,
|
2016-05-06 09:09:47 +01:00
|
|
|
mmg_provider):
|
2016-04-28 12:01:27 +01:00
|
|
|
today = date.today()
|
|
|
|
|
yesterday = today - timedelta(days=1)
|
|
|
|
|
stats_today = ProviderStatistics(
|
|
|
|
|
service=sample_service,
|
|
|
|
|
day=today,
|
2016-05-06 09:09:47 +01:00
|
|
|
provider_id=mmg_provider.id,
|
2016-04-28 12:01:27 +01:00
|
|
|
unit_count=2
|
|
|
|
|
)
|
|
|
|
|
stats_yesterday = ProviderStatistics(
|
|
|
|
|
service=sample_service,
|
|
|
|
|
day=yesterday,
|
2016-05-06 09:09:47 +01:00
|
|
|
provider_id=mmg_provider.id,
|
2016-04-28 12:01:27 +01:00
|
|
|
unit_count=3
|
|
|
|
|
)
|
|
|
|
|
notify_db.session.add(stats_today)
|
|
|
|
|
notify_db.session.add(stats_yesterday)
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
results = get_fragment_count(sample_service, yesterday, today)
|
|
|
|
|
assert results['sms_count'] == 5
|
|
|
|
|
assert results['email_count'] == 0
|