From eaf5cbb86876c417e6dec4d6b76e24f94cbe71bd Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 25 Jul 2017 11:43:41 +0100 Subject: [PATCH] Add labels to query so that the named tuples can be referenced later. Remove unnecessary function --- app/dao/monthly_billing_dao.py | 10 +++++----- app/dao/notification_usage_dao.py | 8 ++++---- app/dao/provider_rates_dao.py | 5 ----- tests/app/dao/test_monthly_billing.py | 2 +- tests/app/dao/test_provider_rates_dao.py | 12 ++---------- tests/app/db.py | 5 +++-- 6 files changed, 15 insertions(+), 27 deletions(-) diff --git a/app/dao/monthly_billing_dao.py b/app/dao/monthly_billing_dao.py index 4dede3f2d..6cc499001 100644 --- a/app/dao/monthly_billing_dao.py +++ b/app/dao/monthly_billing_dao.py @@ -50,8 +50,8 @@ def _monthly_billing_data_to_json(monthly): # (month, billing_units, rate_multiplier, international, notification_type, rate) # total cost must take into account the free allowance. # might be a good idea to capture free allowance in this table - return [{"billing_units": x[1], - "rate_multiplier": x[2], - "international": x[3], - "rate": x[5], - "total_cost": (x[1] * x[2]) * x[5]} for x in monthly] + return [{"billing_units": x.billing_units, + "rate_multiplier": x.rate_multiplier, + "international": x.international, + "rate": x.rate, + "total_cost": (x.billing_units * x.rate_multiplier) * x.rate} for x in monthly] diff --git a/app/dao/notification_usage_dao.py b/app/dao/notification_usage_dao.py index 13b8944c6..43fb6a6cd 100644 --- a/app/dao/notification_usage_dao.py +++ b/app/dao/notification_usage_dao.py @@ -142,12 +142,12 @@ def is_between(date, start_date, end_date): def sms_billing_data_per_month_query(rate, service_id, start_date, end_date): month = get_london_month_from_utc_column(NotificationHistory.created_at) result = db.session.query( - month, - func.sum(NotificationHistory.billable_units), - rate_multiplier(), + month.label('month'), + func.sum(NotificationHistory.billable_units).label('billing_units'), + rate_multiplier().label('rate_multiplier'), NotificationHistory.international, NotificationHistory.notification_type, - cast(rate, Float()) + cast(rate, Float()).label('rate') ).filter( *billing_data_filter(SMS_TYPE, start_date, end_date, service_id) ).group_by( diff --git a/app/dao/provider_rates_dao.py b/app/dao/provider_rates_dao.py index 443543f86..145bd431e 100644 --- a/app/dao/provider_rates_dao.py +++ b/app/dao/provider_rates_dao.py @@ -9,8 +9,3 @@ def create_provider_rates(provider_identifier, valid_from, rate): provider_rates = ProviderRates(provider_id=provider.id, valid_from=valid_from, rate=rate) db.session.add(provider_rates) - - -@transactional -def create_sms_rate(rate): - db.session.add(rate) diff --git a/tests/app/dao/test_monthly_billing.py b/tests/app/dao/test_monthly_billing.py index 2ed13aaa2..9027ff870 100644 --- a/tests/app/dao/test_monthly_billing.py +++ b/tests/app/dao/test_monthly_billing.py @@ -111,7 +111,7 @@ def assert_monthly_billing(monthly_billing, year, month, service_id, expected_le assert sorted(monthly_billing.monthly_totals[0]) == sorted(first_row) -def test_get_service_id(): +def test_get_service_id(notify_db_session): service_1 = create_service(service_name="Service One") template_1 = create_template(service=service_1) service_2 = create_service(service_name="Service Two") diff --git a/tests/app/dao/test_provider_rates_dao.py b/tests/app/dao/test_provider_rates_dao.py index 7edb7de43..c78290a90 100644 --- a/tests/app/dao/test_provider_rates_dao.py +++ b/tests/app/dao/test_provider_rates_dao.py @@ -1,8 +1,8 @@ import uuid from datetime import datetime from decimal import Decimal -from app.dao.provider_rates_dao import create_provider_rates, create_sms_rate -from app.models import ProviderRates, ProviderDetails, Rate +from app.dao.provider_rates_dao import create_provider_rates +from app.models import ProviderRates, ProviderDetails def test_create_provider_rates(notify_db, notify_db_session, mmg_provider): @@ -16,11 +16,3 @@ def test_create_provider_rates(notify_db, notify_db_session, mmg_provider): assert ProviderRates.query.first().rate == rate assert ProviderRates.query.first().valid_from == now assert ProviderRates.query.first().provider_id == provider.id - - -def test_create_sms_rate(): - rate = Rate(id=uuid.uuid4(), valid_from=datetime.now(), rate=0.014, notification_type='sms') - create_sms_rate(rate) - rates = Rate.query.all() - assert len(rates) == 1 - assert rates[0] == rate diff --git a/tests/app/db.py b/tests/app/db.py index 669803d92..ace7b7c1c 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -1,8 +1,8 @@ from datetime import datetime import uuid +from app import db from app.dao.jobs_dao import dao_create_job -from app.dao.provider_rates_dao import create_sms_rate from app.dao.service_inbound_api_dao import save_service_inbound_api from app.models import ( Service, @@ -244,5 +244,6 @@ def create_organisation(colour='blue', logo='test_x2.png', name='test_org_1'): def create_rate(start_date, value, notification_type): rate = Rate(id=uuid.uuid4(), valid_from=start_date, rate=value, notification_type=notification_type) - create_sms_rate(rate) + db.session.add(rate) + db.session.commit() return rate