use db models instead of tuples when referring to rate objects

makes it less confusing
This commit is contained in:
Leo Hemsted
2019-04-03 13:07:49 +01:00
parent fcc25abf18
commit 6f41f6c7d7
3 changed files with 46 additions and 32 deletions

View File

@@ -209,10 +209,8 @@ def fetch_billing_data_for_day(process_day, service_id=None):
def get_rates_for_billing(): def get_rates_for_billing():
non_letter_rates = [(r.notification_type, r.valid_from, r.rate) for r in non_letter_rates = Rate.query.order_by(desc(Rate.valid_from)).all()
Rate.query.order_by(desc(Rate.valid_from)).all()] letter_rates = LetterRate.query.order_by(desc(LetterRate.start_date)).all()
letter_rates = [(r.start_date, r.crown, r.sheet_count, r.rate, r.post_class) for r in
LetterRate.query.order_by(desc(LetterRate.start_date)).all()]
return non_letter_rates, letter_rates return non_letter_rates, letter_rates
@@ -234,11 +232,22 @@ def get_rate(
if letter_page_count == 0: if letter_page_count == 0:
return 0 return 0
return next( return next(
r[3] for r in letter_rates if date >= r[0] and crown == r[1] r.rate
and letter_page_count == r[2] and post_class == r[4] for r in letter_rates if (
date >= r.start_date and
crown == r.crown and
letter_page_count == r.sheet_count and
post_class == r.post_class
)
) )
elif notification_type == SMS_TYPE: elif notification_type == SMS_TYPE:
return next(r[2] for r in non_letter_rates if notification_type == r[0] and date >= r[1]) return next(
r.rate
for r in non_letter_rates if (
notification_type == r.notification_type and
date >= r.valid_from
)
)
else: else:
return 0 return 0

View File

@@ -14,10 +14,8 @@ from app.models import (
EMAIL_TYPE, EMAIL_TYPE,
SMS_TYPE, FactNotificationStatus SMS_TYPE, FactNotificationStatus
) )
from app.models import LetterRate, Rate
from app import db
from tests.app.db import create_service, create_template, create_notification from tests.app.db import create_service, create_template, create_notification, create_rate, create_letter_rate
def mocker_get_rate( def mocker_get_rate(
@@ -322,38 +320,26 @@ def test_create_nightly_billing_consolidate_from_3_days_delta(
def test_get_rate_for_letter_latest(notify_db_session): def test_get_rate_for_letter_latest(notify_db_session):
non_letter_rates = [(r.notification_type, r.valid_from, r.rate) for r in
Rate.query.order_by(desc(Rate.valid_from)).all()]
# letter rates should be passed into the get_rate function as a tuple of start_date, crown, sheet_count, # letter rates should be passed into the get_rate function as a tuple of start_date, crown, sheet_count,
# rate and post_class # rate and post_class
new_letter_rate = (datetime(2017, 12, 1), True, 1, Decimal(0.33), 'second') new = create_letter_rate(datetime(2017, 12, 1), crown=True, sheet_count=1, rate=0.33, post_class='second')
old_letter_rate = (datetime(2016, 12, 1), True, 1, Decimal(0.30), 'second') old = create_letter_rate(datetime(2016, 12, 1), crown=True, sheet_count=1, rate=0.30, post_class='second')
letter_rates = [new_letter_rate, old_letter_rate] letter_rates = [new, old]
rate = get_rate(non_letter_rates, letter_rates, LETTER_TYPE, datetime(2018, 1, 1), True, 1) rate = get_rate([], letter_rates, LETTER_TYPE, datetime(2018, 1, 1), True, 1)
assert rate == Decimal(0.33) assert rate == Decimal(0.33)
def test_get_rate_for_sms_and_email(notify_db_session): def test_get_rate_for_sms_and_email(notify_db_session):
sms_rate = Rate(valid_from=datetime(2017, 12, 1), non_letter_rates = [
rate=Decimal(0.15), create_rate(datetime(2017, 12, 1), 0.15, SMS_TYPE)
notification_type=SMS_TYPE) create_rate(datetime(2017, 12, 1), 0, EMAIL_TYPE)
db.session.add(sms_rate) ]
email_rate = Rate(valid_from=datetime(2017, 12, 1),
rate=Decimal(0),
notification_type=EMAIL_TYPE)
db.session.add(email_rate)
non_letter_rates = [(r.notification_type, r.valid_from, r.rate) for r in rate = get_rate(non_letter_rates, [], SMS_TYPE, datetime(2018, 1, 1))
Rate.query.order_by(desc(Rate.valid_from)).all()]
letter_rates = [(r.start_date, r.crown, r.sheet_count, r.rate) for r in
LetterRate.query.order_by(desc(LetterRate.start_date)).all()]
rate = get_rate(non_letter_rates, letter_rates, SMS_TYPE, datetime(2018, 1, 1))
assert rate == Decimal(0.15) assert rate == Decimal(0.15)
rate = get_rate(non_letter_rates, letter_rates, EMAIL_TYPE, datetime(2018, 1, 1)) rate = get_rate(non_letter_rates, [], EMAIL_TYPE, datetime(2018, 1, 1))
assert rate == Decimal(0) assert rate == Decimal(0)

View File

@@ -1,6 +1,7 @@
import random import random
import uuid import uuid
from datetime import datetime, date from datetime import datetime, date
from decimal import Decimal
from app import db from app import db
from app.dao.email_branding_dao import dao_create_email_branding from app.dao.email_branding_dao import dao_create_email_branding
@@ -29,6 +30,7 @@ from app.models import (
Job, Job,
Notification, Notification,
EmailBranding, EmailBranding,
LetterRate,
Organisation, Organisation,
Rate, Rate,
Service, Service,
@@ -403,6 +405,23 @@ def create_rate(start_date, value, notification_type):
return rate return rate
def create_letter_rate(start_date=None, end_date=None, crown=True, sheet_count=1, rate=0.33, post_class='second'):
if start_date is None:
start_date = datetime(2016, 1, 1)
rate = LetterRate(
id=uuid.uuid4(),
start_date=start_date,
end_date=end_date,
crown=crown,
sheet_count=sheet_count,
rate=Decimal(rate),
post_class=post_class
)
db.session.add(rate)
db.session.commit()
return rate
def create_api_key(service, key_type=KEY_TYPE_NORMAL): def create_api_key(service, key_type=KEY_TYPE_NORMAL):
id_ = uuid.uuid4() id_ = uuid.uuid4()
api_key = ApiKey( api_key = ApiKey(