mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
use db models instead of tuples when referring to rate objects
makes it less confusing
This commit is contained in:
@@ -209,10 +209,8 @@ def fetch_billing_data_for_day(process_day, service_id=None):
|
||||
|
||||
|
||||
def get_rates_for_billing():
|
||||
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 = [(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()]
|
||||
non_letter_rates = Rate.query.order_by(desc(Rate.valid_from)).all()
|
||||
letter_rates = LetterRate.query.order_by(desc(LetterRate.start_date)).all()
|
||||
return non_letter_rates, letter_rates
|
||||
|
||||
|
||||
@@ -234,11 +232,22 @@ def get_rate(
|
||||
if letter_page_count == 0:
|
||||
return 0
|
||||
return next(
|
||||
r[3] for r in letter_rates if date >= r[0] and crown == r[1]
|
||||
and letter_page_count == r[2] and post_class == r[4]
|
||||
r.rate
|
||||
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:
|
||||
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:
|
||||
return 0
|
||||
|
||||
|
||||
@@ -14,10 +14,8 @@ from app.models import (
|
||||
EMAIL_TYPE,
|
||||
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(
|
||||
@@ -322,38 +320,26 @@ def test_create_nightly_billing_consolidate_from_3_days_delta(
|
||||
|
||||
|
||||
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,
|
||||
# rate and post_class
|
||||
new_letter_rate = (datetime(2017, 12, 1), True, 1, Decimal(0.33), 'second')
|
||||
old_letter_rate = (datetime(2016, 12, 1), True, 1, Decimal(0.30), 'second')
|
||||
letter_rates = [new_letter_rate, old_letter_rate]
|
||||
new = create_letter_rate(datetime(2017, 12, 1), crown=True, sheet_count=1, rate=0.33, post_class='second')
|
||||
old = create_letter_rate(datetime(2016, 12, 1), crown=True, sheet_count=1, rate=0.30, post_class='second')
|
||||
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)
|
||||
|
||||
|
||||
def test_get_rate_for_sms_and_email(notify_db_session):
|
||||
sms_rate = Rate(valid_from=datetime(2017, 12, 1),
|
||||
rate=Decimal(0.15),
|
||||
notification_type=SMS_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 = [
|
||||
create_rate(datetime(2017, 12, 1), 0.15, SMS_TYPE)
|
||||
create_rate(datetime(2017, 12, 1), 0, EMAIL_TYPE)
|
||||
]
|
||||
|
||||
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 = [(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))
|
||||
rate = get_rate(non_letter_rates, [], SMS_TYPE, datetime(2018, 1, 1))
|
||||
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)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import random
|
||||
import uuid
|
||||
from datetime import datetime, date
|
||||
from decimal import Decimal
|
||||
|
||||
from app import db
|
||||
from app.dao.email_branding_dao import dao_create_email_branding
|
||||
@@ -29,6 +30,7 @@ from app.models import (
|
||||
Job,
|
||||
Notification,
|
||||
EmailBranding,
|
||||
LetterRate,
|
||||
Organisation,
|
||||
Rate,
|
||||
Service,
|
||||
@@ -403,6 +405,23 @@ def create_rate(start_date, value, notification_type):
|
||||
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):
|
||||
id_ = uuid.uuid4()
|
||||
api_key = ApiKey(
|
||||
|
||||
Reference in New Issue
Block a user