2016-07-29 16:39:51 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
import uuid
|
|
|
|
|
|
2016-08-03 17:22:20 +01:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from app.models import NotificationHistory, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, NOTIFICATION_STATUS_TYPES
|
2016-08-25 10:33:12 +01:00
|
|
|
from app.dao.provider_statistics_dao import get_fragment_count
|
2016-04-28 12:01:27 +01:00
|
|
|
|
|
|
|
|
|
2016-07-29 16:39:51 +01:00
|
|
|
def test_get_fragment_count_with_no_data(sample_template):
|
|
|
|
|
assert get_fragment_count(sample_template.service_id)['sms_count'] == 0
|
|
|
|
|
assert get_fragment_count(sample_template.service_id)['email_count'] == 0
|
2016-05-06 09:09:47 +01:00
|
|
|
|
|
|
|
|
|
2016-07-29 16:39:51 +01:00
|
|
|
def test_get_fragment_count_separates_sms_and_email(notify_db, sample_template, sample_email_template):
|
|
|
|
|
noti_hist(notify_db, sample_template)
|
|
|
|
|
noti_hist(notify_db, sample_template)
|
|
|
|
|
noti_hist(notify_db, sample_email_template)
|
|
|
|
|
assert get_fragment_count(sample_template.service_id) == {
|
|
|
|
|
'sms_count': 2,
|
|
|
|
|
'email_count': 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_fragment_count_filters_on_status(notify_db, sample_template):
|
|
|
|
|
for status in NOTIFICATION_STATUS_TYPES:
|
|
|
|
|
noti_hist(notify_db, sample_template, status=status)
|
|
|
|
|
# sending, delivered, failed, technical-failure, temporary-failure, permanent-failure
|
|
|
|
|
assert get_fragment_count(sample_template.service_id)['sms_count'] == 6
|
|
|
|
|
|
|
|
|
|
|
2016-08-01 10:22:22 +01:00
|
|
|
def test_get_fragment_count_filters_on_service_id(notify_db, sample_template, service_factory):
|
|
|
|
|
service_2 = service_factory.get('service 2', email_from='service.2')
|
|
|
|
|
noti_hist(notify_db, sample_template)
|
|
|
|
|
assert get_fragment_count(service_2.id)['sms_count'] == 0
|
|
|
|
|
|
|
|
|
|
|
2016-08-03 16:26:11 +01:00
|
|
|
def test_get_fragment_count_sums_billable_units_for_sms(notify_db, sample_template):
|
|
|
|
|
noti_hist(notify_db, sample_template, billable_units=1)
|
|
|
|
|
noti_hist(notify_db, sample_template, billable_units=2)
|
|
|
|
|
assert get_fragment_count(sample_template.service_id)['sms_count'] == 3
|
2016-07-29 16:39:51 +01:00
|
|
|
|
|
|
|
|
|
2016-08-01 10:22:22 +01:00
|
|
|
@pytest.mark.parametrize('key_type,sms_count', [
|
|
|
|
|
(KEY_TYPE_NORMAL, 1),
|
|
|
|
|
(KEY_TYPE_TEAM, 1),
|
|
|
|
|
(KEY_TYPE_TEST, 0),
|
|
|
|
|
])
|
|
|
|
|
def test_get_fragment_count_ignores_test_api_keys(notify_db, sample_template, key_type, sms_count):
|
|
|
|
|
noti_hist(notify_db, sample_template, key_type=key_type)
|
|
|
|
|
assert get_fragment_count(sample_template.service_id)['sms_count'] == sms_count
|
|
|
|
|
|
|
|
|
|
|
2016-08-03 16:26:11 +01:00
|
|
|
def noti_hist(notify_db, template, status='delivered', billable_units=None, key_type=KEY_TYPE_NORMAL):
|
|
|
|
|
if not billable_units and template.template_type == 'sms':
|
|
|
|
|
billable_units = 1
|
2016-07-29 16:39:51 +01:00
|
|
|
|
|
|
|
|
notification_history = NotificationHistory(
|
|
|
|
|
id=uuid.uuid4(),
|
|
|
|
|
service=template.service,
|
|
|
|
|
template=template,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
status=status,
|
|
|
|
|
created_at=datetime.utcnow(),
|
2016-08-03 16:26:11 +01:00
|
|
|
billable_units=billable_units,
|
2016-07-29 16:39:51 +01:00
|
|
|
notification_type=template.template_type,
|
2016-08-03 16:26:11 +01:00
|
|
|
key_type=key_type
|
2016-04-28 12:01:27 +01:00
|
|
|
)
|
2016-07-29 16:39:51 +01:00
|
|
|
notify_db.session.add(notification_history)
|
2016-04-28 12:01:27 +01:00
|
|
|
notify_db.session.commit()
|
2016-07-29 16:39:51 +01:00
|
|
|
|
|
|
|
|
return notification_history
|