Add .cost() to notification model

In the V2 API, the GET response for an individual notification
returns a 'cost' value, which we can get by multiplying the
billable units by the per-message rate of the supplier who
sent the message.
Any notifications with billable units > 0 but without a
corresponding `ProviderRates` entry will blow up the application,
so make sure you've got one.
This commit is contained in:
Paul Craig
2016-11-18 15:09:15 +00:00
parent 1345c94f4b
commit fddb1653ac
4 changed files with 58 additions and 8 deletions

View File

@@ -1,9 +1,7 @@
import uuid
import pytest
from datetime import datetime
import pytest
from app import DATETIME_FORMAT
from tests.app.conftest import sample_notification, sample_provider_rate
from app.models import (
Notification,
ServiceWhitelist,
@@ -37,3 +35,29 @@ def test_should_build_service_whitelist_from_email_address(email_address):
def test_should_not_build_service_whitelist_from_invalid_contact(recipient_type, contact):
with pytest.raises(ValueError):
ServiceWhitelist.from_string('service_id', recipient_type, contact)
@pytest.mark.parametrize('provider, billable_units, expected_cost', [
('mmg', 1, 3.5),
('firetext', 2, 5),
('ses', 0, 0)
])
def test_calculate_cost_from_notification_billable_units(
notify_db, notify_db_session, provider, billable_units, expected_cost
):
provider_rates = [
('mmg', datetime(2016, 7, 1), 1.5),
('firetext', datetime(2016, 7, 1), 2.5),
('mmg', datetime.utcnow(), 3.5),
]
for provider_identifier, valid_from, rate in provider_rates:
sample_provider_rate(
notify_db,
notify_db_session,
provider_identifier=provider_identifier,
valid_from=valid_from,
rate=rate
)
notification = sample_notification(notify_db, notify_db_session, billable_units=billable_units, sent_by=provider)
assert notification.cost() == expected_cost