diff --git a/app/models.py b/app/models.py index 59e6f7a91..0d7f7236d 100644 --- a/app/models.py +++ b/app/models.py @@ -670,7 +670,7 @@ class Notification(db.Model): international = db.Column(db.Boolean, nullable=False, default=False) phone_prefix = db.Column(db.String, nullable=True) - rate_multiplier = db.Column(db.Float(), nullable=True) + rate_multiplier = db.Column(db.Float(asdecimal=False), nullable=True) @property def personalisation(self): diff --git a/tests/app/conftest.py b/tests/app/conftest.py index de69e9240..fc9748870 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -444,7 +444,8 @@ def sample_notification( api_key_id=None, key_type=KEY_TYPE_NORMAL, sent_by=None, - client_reference=None + client_reference=None, + rate_multiplier=1.0 ): if created_at is None: created_at = datetime.utcnow() @@ -481,7 +482,8 @@ def sample_notification( 'key_type': key_type, 'sent_by': sent_by, 'updated_at': created_at if status in NOTIFICATION_STATUS_TYPES_COMPLETED else None, - 'client_reference': client_reference + 'client_reference': client_reference, + 'rate_multiplier': rate_multiplier } if job_row_number is not None: data['job_row_number'] = job_row_number diff --git a/tests/app/dao/test_notification_usage_dao.py b/tests/app/dao/test_notification_usage_dao.py index 72891d46e..0c3ddc952 100644 --- a/tests/app/dao/test_notification_usage_dao.py +++ b/tests/app/dao/test_notification_usage_dao.py @@ -160,6 +160,13 @@ def test_get_monthly_billing_data_with_multiple_rates(notify_db, notify_db_sessi assert results[3] == ('June', 4, 1, False, 'sms', 1.75) +def test_get_monthly_billing_data_with_no_notifications_for_year(notify_db, notify_db_session, sample_template, + sample_email_template): + set_up_rate(notify_db, datetime(2016, 4, 1), 1.40) + results = get_monthly_billing_data(sample_template.service_id, 2016) + assert len(results) == 0 + + def set_up_rate(notify_db, start_date, value): rate = Rate(id=uuid.uuid4(), valid_from=start_date, rate=value, notification_type='sms') notify_db.session.add(rate) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 89cfc27f1..f289960a9 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -1554,6 +1554,9 @@ def test_get_monthly_billing_usage(client, notify_db, notify_db_session): notification = create_sample_notification(notify_db, notify_db_session, created_at=datetime(2016, 6, 5), sent_at=datetime(2016, 6, 5), status='sending') + create_sample_notification(notify_db, notify_db_session, created_at=datetime(2016, 6, 5), + sent_at=datetime(2016, 6, 5), + status='sending', rate_multiplier=2) create_sample_notification(notify_db, notify_db_session, created_at=datetime(2016, 7, 5), sent_at=datetime(2016, 7, 5), status='sending') @@ -1563,14 +1566,19 @@ def test_get_monthly_billing_usage(client, notify_db, notify_db_session): ) assert response.status_code == 200 actual = json.loads(response.get_data(as_text=True)) - assert len(actual) == 2 - print(actual) + assert len(actual) == 3 assert actual == [{'month': 'June', 'international': False, 'rate_multiplier': 1, 'notification_type': 'sms', 'rate': 1.58, 'billing_units': 1}, + {'month': 'June', + 'international': False, + 'rate_multiplier': 2, + 'notification_type': 'sms', + 'rate': 1.58, + 'billing_units': 1}, {'month': 'July', 'international': False, 'rate_multiplier': 1, @@ -1588,3 +1596,14 @@ def test_get_monthly_billing_usage_returns_400_if_missing_year(client, sample_se assert json.loads(response.get_data(as_text=True)) == { 'message': 'No valid year provided', 'result': 'error' } + + +def test_get_monthly_billing_usage_returns_empty_list_if_no_notifications(client, notify_db, sample_service): + rate = Rate(id=uuid.uuid4(), valid_from=datetime(2016, 3, 31, 23, 00), rate=1.58, notification_type='sms') + notify_db.session.add(rate) + response = client.get( + '/service/{}/monthly-usage?year=2016'.format(sample_service.id), + headers=[create_authorization_header()] + ) + assert response.status_code == 200 + assert json.loads(response.get_data(as_text=True)) == []