Add labels to query so that the named tuples can be referenced later.

Remove unnecessary function
This commit is contained in:
Rebecca Law
2017-07-25 11:43:41 +01:00
parent 91f29517eb
commit eaf5cbb868
6 changed files with 15 additions and 27 deletions

View File

@@ -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]

View File

@@ -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(

View File

@@ -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)

View File

@@ -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")

View File

@@ -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

View File

@@ -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