calculate billable units when sending an sms

don't calculate it if we're in research mode
* added tests to prove this
* removed last code referring to content_char_count
This commit is contained in:
Leo Hemsted
2016-08-03 16:26:11 +01:00
parent 2793541b9c
commit 527a5c4eaa
9 changed files with 47 additions and 37 deletions

View File

@@ -111,7 +111,7 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
assert notification.status == 'sending'
assert notification.sent_at <= datetime.utcnow()
assert notification.sent_by == 'mmg'
assert notification.content_char_count == len("Sample service: Hello Jo\nYour thing is due soon")
assert notification.billable_units == 1
assert notification.personalisation == {"name": "Jo"}
@@ -194,7 +194,6 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
assert persisted_notification.template_id == sample_template.id
assert persisted_notification.template_version == version_on_notification
assert persisted_notification.template_version != sample_template.version
assert persisted_notification.content_char_count == len("Sample service: This is a template:\nwith a newline")
assert persisted_notification.status == 'sending'
assert not persisted_notification.personalisation
@@ -546,3 +545,30 @@ def test_send_email_should_use_service_reply_to_email(
html_body=ANY,
reply_to_address=sample_service.reply_to_email_address
)
def test_should_not_set_billable_units_if_research_mode(notify_db, sample_service, sample_notification, mocker):
mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.mmg_client.get_name', return_value="mmg")
mocker.patch('app.celery.research_mode_tasks.send_sms_response.apply_async')
sample_service.research_mode = True
notify_db.session.add(sample_service)
notify_db.session.commit()
send_sms_to_provider(
sample_notification.service_id,
sample_notification.id
)
persisted_notification = notifications_dao.get_notification(sample_service.id, sample_notification.id)
assert persisted_notification.billable_units == 0
def _get_provider_statistics(service, **kwargs):
query = ProviderStatistics.query.filter_by(service=service)
if 'providers' in kwargs:
providers = ProviderDetails.query.filter(ProviderDetails.identifier.in_(kwargs['providers'])).all()
provider_ids = [provider.id for provider in providers]
query = query.filter(ProviderStatistics.provider_id.in_(provider_ids))
return query

View File

@@ -323,7 +323,7 @@ def sample_notification(notify_db,
status='created',
reference=None,
created_at=None,
content_char_count=160,
billable_units=1,
create=True,
personalisation=None,
api_key_id=None,
@@ -356,7 +356,7 @@ def sample_notification(notify_db,
'status': status,
'reference': reference,
'created_at': created_at,
'content_char_count': content_char_count,
'billable_units': billable_units,
'personalisation': personalisation,
'notification_type': template.template_type,
'api_key_id': api_key_id,

View File

@@ -1028,7 +1028,7 @@ def _notification_json(sample_template, job_id=None, id=None, status=None):
'template_id': sample_template.id,
'template_version': sample_template.version,
'created_at': datetime.utcnow(),
'content_char_count': 160,
'billable_units': 1,
'notification_type': sample_template.template_type,
'key_type': KEY_TYPE_NORMAL
}

View File

@@ -119,11 +119,10 @@ def test_get_fragment_count_filters_on_service_id(notify_db, sample_template, se
assert get_fragment_count(service_2.id)['sms_count'] == 0
def test_get_fragment_count_sums_char_count_for_sms(notify_db, sample_template):
noti_hist(notify_db, sample_template, content_char_count=1) # 1
noti_hist(notify_db, sample_template, content_char_count=159) # 1
noti_hist(notify_db, sample_template, content_char_count=310) # 2
assert get_fragment_count(sample_template.service_id)['sms_count'] == 4
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
@pytest.mark.parametrize('key_type,sms_count', [
@@ -136,9 +135,9 @@ def test_get_fragment_count_ignores_test_api_keys(notify_db, sample_template, ke
assert get_fragment_count(sample_template.service_id)['sms_count'] == sms_count
def noti_hist(notify_db, template, status='delivered', content_char_count=None, key_type=KEY_TYPE_NORMAL):
if not content_char_count and template.template_type == 'sms':
content_char_count = 1
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
notification_history = NotificationHistory(
id=uuid.uuid4(),
@@ -147,9 +146,9 @@ def noti_hist(notify_db, template, status='delivered', content_char_count=None,
template_version=template.version,
status=status,
created_at=datetime.utcnow(),
content_char_count=content_char_count,
billable_units=billable_units,
notification_type=template.template_type,
key_type=KEY_TYPE_NORMAL
key_type=key_type
)
notify_db.session.add(notification_history)
notify_db.session.commit()