From d01d875f7ee2811757f797be483414c7bfb7d7a8 Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Mon, 31 Jul 2017 17:47:53 +0100 Subject: [PATCH] Fix issue where monthly billing data was not being updated: --- app/dao/monthly_billing_dao.py | 27 ++++++++---- tests/app/dao/test_monthly_billing.py | 60 +++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 10 deletions(-) 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..e622be569 100644 --- a/tests/app/dao/test_monthly_billing.py +++ b/tests/app/dao/test_monthly_billing.py @@ -1,17 +1,40 @@ -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( + notify_db, + notify_db_session, + service_id, + notification_type, + monthly_totals, + start_date, + end_date +): + 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) @@ -136,3 +159,34 @@ 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( + notify_db, + notify_db_session, + service_1.id, + 'sms', + [], + now, + now + timedelta(days=30) + ) + + create_sample_monthly_billing_entry( + notify_db, + notify_db_session, + service_2.id, + 'sms', + [], + now, + 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