diff --git a/app/dao/monthly_billing_dao.py b/app/dao/monthly_billing_dao.py index 4f4e254c8..4ddacddfa 100644 --- a/app/dao/monthly_billing_dao.py +++ b/app/dao/monthly_billing_dao.py @@ -25,20 +25,33 @@ def create_or_update_monthly_billing_sms(service_id, billing_month): monthly = get_billing_data_for_month(service_id=service_id, start_date=start_date, end_date=end_date) # update monthly monthly_totals = _monthly_billing_data_to_json(monthly) - row = MonthlyBilling.query.filter_by(start_date=start_date, - notification_type='sms').first() + row = get_monthly_billing_entry(service_id, start_date, SMS_TYPE) + if row: row.monthly_totals = monthly_totals row.updated_at = datetime.utcnow() else: - row = MonthlyBilling(service_id=service_id, - notification_type=SMS_TYPE, - monthly_totals=monthly_totals, - start_date=start_date, - end_date=end_date) + row = MonthlyBilling( + service_id=service_id, + notification_type=SMS_TYPE, + monthly_totals=monthly_totals, + start_date=start_date, + end_date=end_date + ) + db.session.add(row) +def get_monthly_billing_entry(service_id, start_date, notification_type): + entry = MonthlyBilling.query.filter_by( + service_id=service_id, + start_date=start_date, + notification_type=notification_type + ).first() + + return entry + + @statsd(namespace="dao") def get_monthly_billing_sms(service_id, billing_month): start_date, end_date = get_month_start_end_date(billing_month) diff --git a/tests/app/dao/test_monthly_billing.py b/tests/app/dao/test_monthly_billing.py index e07588374..bb40b7f9f 100644 --- a/tests/app/dao/test_monthly_billing.py +++ b/tests/app/dao/test_monthly_billing.py @@ -1,22 +1,43 @@ -from datetime import datetime - +from datetime import datetime, timedelta from freezegun import freeze_time from freezegun.api import FakeDatetime +from app import db from app.dao.monthly_billing_dao import ( create_or_update_monthly_billing_sms, + get_monthly_billing_entry, get_monthly_billing_sms, get_service_ids_that_need_sms_billing_populated ) -from app.models import MonthlyBilling +from app.models import MonthlyBilling, SMS_TYPE from tests.app.db import create_notification, create_rate, create_service, create_template +def create_sample_monthly_billing_entry( + service_id, + monthly_totals, + start_date, + end_date, + notification_type=SMS_TYPE +): + entry = MonthlyBilling( + service_id=service_id, + notification_type=notification_type, + monthly_totals=monthly_totals, + start_date=start_date, + end_date=end_date + ) + db.session.add(entry) + db.session.commit() + + return entry + + def test_add_monthly_billing(sample_template): jan = datetime(2017, 1, 1) feb = datetime(2017, 2, 15) - create_rate(start_date=jan, value=0.0158, notification_type='sms') - create_rate(start_date=datetime(2017, 3, 31, 23, 00, 00), value=0.123, notification_type='sms') + create_rate(start_date=jan, value=0.0158, notification_type=SMS_TYPE) + create_rate(start_date=datetime(2017, 3, 31, 23, 00, 00), value=0.123, notification_type=SMS_TYPE) create_notification(template=sample_template, created_at=jan, billable_units=1, status='delivered') create_notification(template=sample_template, created_at=feb, billable_units=2, status='delivered') @@ -51,8 +72,8 @@ def test_add_monthly_billing(sample_template): def test_add_monthly_billing_multiple_rates_in_a_month(sample_template): rate_1 = datetime(2016, 12, 1) rate_2 = datetime(2017, 1, 15) - create_rate(start_date=rate_1, value=0.0158, notification_type='sms') - create_rate(start_date=rate_2, value=0.0124, notification_type='sms') + create_rate(start_date=rate_1, value=0.0158, notification_type=SMS_TYPE) + create_rate(start_date=rate_2, value=0.0124, notification_type=SMS_TYPE) create_notification(template=sample_template, created_at=datetime(2017, 1, 1), billable_units=1, status='delivered') create_notification(template=sample_template, created_at=datetime(2017, 1, 14, 23, 59), billable_units=1, @@ -87,7 +108,7 @@ def test_add_monthly_billing_multiple_rates_in_a_month(sample_template): def test_update_monthly_billing_overwrites_old_totals(sample_template): july = datetime(2017, 7, 1) - create_rate(july, 0.123, 'sms') + create_rate(july, 0.123, SMS_TYPE) create_notification(template=sample_template, created_at=datetime(2017, 7, 2), billable_units=1, status='delivered') with freeze_time('2017-07-20 02:30:00'): create_or_update_monthly_billing_sms(sample_template.service_id, july) @@ -136,3 +157,28 @@ def test_get_service_id(notify_db_session): end_date=datetime(2017, 7, 16)) expected_services = [service_1.id, service_2.id] assert sorted([x.service_id for x in services]) == sorted(expected_services) + + +def test_get_monthly_billing_entry_filters_by_service(notify_db, notify_db_session): + service_1 = create_service(service_name="Service One") + service_2 = create_service(service_name="Service Two") + now = datetime.utcnow() + + create_sample_monthly_billing_entry( + service_id=service_1.id, + monthly_totals=[], + start_date=now, + end_date=now + timedelta(days=30) + ) + + create_sample_monthly_billing_entry( + service_id=service_2.id, + monthly_totals=[], + start_date=now, + end_date=now + timedelta(days=30) + ) + + entry = get_monthly_billing_entry(service_2.id, now, SMS_TYPE) + + assert entry.start_date == now + assert entry.service_id == service_2.id