Add notification_type to query

This commit is contained in:
Rebecca Law
2018-04-27 15:15:55 +01:00
parent 8e2e94ca12
commit 0fb9c1d318
3 changed files with 119 additions and 23 deletions

View File

@@ -5,7 +5,7 @@ from freezegun import freeze_time
from app import db
from app.dao.fact_billing_dao import (
fetch_montly_billing_for_year, fetch_billing_data_for_day, get_rates_for_billing,
fetch_monthly_billing_for_year, fetch_billing_data_for_day, get_rates_for_billing,
get_rate
)
from app.models import FactBilling
@@ -132,6 +132,25 @@ def test_fetch_billing_data_for_day_is_grouped_by_international(notify_db_sessio
assert results[1].notifications_sent == 1
def test_fetch_billing_data_for_day_is_grouped_by_notification_type(notify_db_session):
service = create_service()
sms_template = create_template(service=service, template_type='sms')
email_template = create_template(service=service, template_type='email')
letter_template = create_template(service=service, template_type='letter')
create_notification(template=sms_template, status='delivered')
create_notification(template=sms_template, status='delivered')
create_notification(template=sms_template, status='delivered')
create_notification(template=email_template, status='delivered')
create_notification(template=email_template, status='delivered')
create_notification(template=letter_template, status='delivered')
today = convert_utc_to_bst(datetime.utcnow())
results = fetch_billing_data_for_day(today)
assert len(results) == 3
notification_types = [x[2] for x in results if x[2] in ['email', 'sms', 'letter']]
assert len(notification_types) == 3
def test_fetch_billing_data_for_day_returns_empty_list(notify_db_session):
today = convert_utc_to_bst(datetime.utcnow())
results = fetch_billing_data_for_day(today)
@@ -181,7 +200,7 @@ def test_get_rate(notify_db_session):
assert letter_rate == Decimal('4.4')
def test_fetch_annual_billing_for_year(notify_db_session):
def test_fetch_monthly_billing_for_year(notify_db_session):
service = create_service()
template = create_template(service=service, template_type="sms")
for i in range(1, 31):
@@ -197,28 +216,30 @@ def test_fetch_annual_billing_for_year(notify_db_session):
notification_type='sms',
rate=0.158)
results = fetch_montly_billing_for_year(service_id=service.id, year=2018)
results = fetch_monthly_billing_for_year(service_id=service.id, year=2018)
assert len(results) == 2
assert results[0][0] == 6.0
assert results[0][1] == 30
assert results[0][2] == Decimal('30')
assert results[0][3] == service.id
assert results[0][4] == Decimal('0.162')
assert results[0][5] == Decimal('1')
assert results[0][6] is False
assert results[0].Month == 6.0
assert results[0].notifications_sent == 30
assert results[0].billable_units == Decimal('30')
assert results[0].service_id == service.id
assert results[0].rate == Decimal('0.162')
assert results[0].rate_multiplier == Decimal('1')
assert results[0].international is False
assert results[0].notification_type == 'sms'
assert results[1][0] == 7.0
assert results[1][1] == 31
assert results[1][2] == Decimal('31')
assert results[1][3] == service.id
assert results[1][4] == Decimal('0.158')
assert results[1][5] == Decimal('1')
assert results[1][6] is False
assert results[1].Month == 7.0
assert results[1].notifications_sent == 31
assert results[1].billable_units == Decimal('31')
assert results[1].service_id == service.id
assert results[1].rate == Decimal('0.158')
assert results[1].rate_multiplier == Decimal('1')
assert results[1].international is False
assert results[1].notification_type == 'sms'
@freeze_time('2018-08-01 13:30:00')
def test_fetch_annual_billing_for_year_adds_data_for_today(notify_db_session):
def test_fetch_monthly_billing_for_year_adds_data_for_today(notify_db_session):
service = create_service()
template = create_template(service=service, template_type="email")
for i in range(1, 32):
@@ -230,7 +251,47 @@ def test_fetch_annual_billing_for_year_adds_data_for_today(notify_db_session):
create_notification(template=template, status='delivered')
assert db.session.query(FactBilling.bst_date).count() == 31
results = fetch_montly_billing_for_year(service_id=service.id,
year=2018)
results = fetch_monthly_billing_for_year(service_id=service.id,
year=2018)
assert db.session.query(FactBilling.bst_date).count() == 32
assert len(results) == 2
def test_fetch_monthly_billing_for_year(notify_db_session):
service = create_service()
sms_template = create_template(service=service, template_type="email")
email_template = create_template(service=service, template_type="email")
letter_template = create_template(service=service, template_type="email")
for month in range(1, 13):
mon = str(month).zfill(2)
days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32}
for day in range(1, days_in_month[month]):
d = str(day).zfill(2)
create_ft_billing(bst_date='2016-{}-{}'.format(mon, d),
service=service,
template=sms_template,
notification_type='sms',
rate=0.162)
create_ft_billing(bst_date='2016-{}-{}'.format(mon, d),
service=service,
template=email_template,
notification_type='email',
rate=0)
create_ft_billing(bst_date='2016-{}-{}'.format(mon, d),
service=service,
template=letter_template,
notification_type='letter',
rate=0.33)
results = fetch_monthly_billing_for_year(service.id, 2016)
# returns 3 rows, per month, returns financial year april to december
# Orders by Month
assert len(results) == 27
assert results[0][0] == 4
assert results[1][0] == 4
assert results[2][0] == 4
assert results[3][0] == 5
assert results[26][0] == 12