mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 06:21:50 -05:00
Update queries for data model change (removal of organisation_to_service)
New query to get letter breakdown.
This commit is contained in:
@@ -16,7 +16,10 @@ from app.dao.fact_billing_dao import (
|
||||
fetch_monthly_billing_for_year,
|
||||
get_rate,
|
||||
get_rates_for_billing,
|
||||
)
|
||||
fetch_sms_free_allowance_remainder,
|
||||
fetch_sms_billing_for_all_services,
|
||||
fetch_letter_costs_for_all_services, fetch_letter_line_items_for_all_services)
|
||||
from app.dao.organisation_dao import dao_add_service_to_organisation
|
||||
from app.models import (
|
||||
FactBilling,
|
||||
Notification,
|
||||
@@ -29,7 +32,9 @@ from tests.app.db import (
|
||||
create_notification,
|
||||
create_rate,
|
||||
create_letter_rate,
|
||||
create_notification_history
|
||||
create_notification_history,
|
||||
create_annual_billing,
|
||||
create_organisation
|
||||
)
|
||||
|
||||
|
||||
@@ -478,3 +483,193 @@ def test_delete_billing_data(notify_db_session):
|
||||
assert sorted(x.billable_units for x in current_rows) == sorted(
|
||||
[other_day.billable_units, other_service.billable_units]
|
||||
)
|
||||
|
||||
|
||||
def test_fetch_sms_free_allowance_remainder_with_two_services(notify_db_session):
|
||||
service = create_service(service_name='has free allowance')
|
||||
template = create_template(service=service)
|
||||
org = create_organisation(name="Org for {}".format(service.name))
|
||||
dao_add_service_to_organisation(service=service, organisation_id=org.id)
|
||||
create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2016)
|
||||
create_ft_billing(service=service, template=template,
|
||||
bst_date=datetime(2016, 4, 20), notification_type='sms', billable_unit=2, rate=0.11)
|
||||
create_ft_billing(service=service, template=template, bst_date=datetime(2016, 5, 20), notification_type='sms',
|
||||
billable_unit=3, rate=0.11)
|
||||
|
||||
service_2 = create_service(service_name='used free allowance')
|
||||
template_2 = create_template(service=service_2)
|
||||
org_2 = create_organisation(name="Org for {}".format(service_2.name))
|
||||
dao_add_service_to_organisation(service=service_2, organisation_id=org_2.id)
|
||||
create_annual_billing(service_id=service_2.id, free_sms_fragment_limit=20, financial_year_start=2016)
|
||||
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2016, 4, 20), notification_type='sms',
|
||||
billable_unit=12, rate=0.11)
|
||||
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2016, 4, 22), notification_type='sms',
|
||||
billable_unit=10, rate=0.11)
|
||||
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2016, 5, 20), notification_type='sms',
|
||||
billable_unit=3, rate=0.11)
|
||||
results = fetch_sms_free_allowance_remainder(datetime(2016, 5, 1)).all()
|
||||
assert len(results) == 2
|
||||
service_result = [row for row in results if row[0] == service.id]
|
||||
assert service_result[0] == (service.id, 10, 2, 8)
|
||||
service_2_result = [row for row in results if row[0] == service_2.id]
|
||||
assert service_2_result[0] == (service_2.id, 20, 22, 0)
|
||||
|
||||
|
||||
def test_fetch_sms_billing_for_all_services_with_remainder(notify_db_session):
|
||||
service = create_service(service_name='a - has free allowance')
|
||||
template = create_template(service=service)
|
||||
org = create_organisation(name="Org for {}".format(service.name))
|
||||
dao_add_service_to_organisation(service=service, organisation_id=org.id)
|
||||
create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
||||
create_ft_billing(service=service, template=template,
|
||||
bst_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=2, rate=0.11)
|
||||
create_ft_billing(service=service, template=template, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
||||
billable_unit=2, rate=0.11)
|
||||
create_ft_billing(service=service, template=template, bst_date=datetime(2019, 5, 22), notification_type='sms',
|
||||
billable_unit=1, rate=0.11)
|
||||
|
||||
service_2 = create_service(service_name='b - used free allowance')
|
||||
template_2 = create_template(service=service_2)
|
||||
org_2 = create_organisation(name="Org for {}".format(service_2.name))
|
||||
dao_add_service_to_organisation(service=service_2, organisation_id=org_2.id)
|
||||
create_annual_billing(service_id=service_2.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
||||
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2019, 4, 20), notification_type='sms',
|
||||
billable_unit=12, rate=0.11)
|
||||
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
||||
billable_unit=3, rate=0.11)
|
||||
|
||||
service_3 = create_service(service_name='c - partial allowance')
|
||||
template_3 = create_template(service=service_3)
|
||||
org_3 = create_organisation(name="Org for {}".format(service_3.name))
|
||||
dao_add_service_to_organisation(service=service_3, organisation_id=org_3.id)
|
||||
create_annual_billing(service_id=service_3.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
||||
create_ft_billing(service=service_3, template=template_3, bst_date=datetime(2019, 4, 20), notification_type='sms',
|
||||
billable_unit=5, rate=0.11)
|
||||
create_ft_billing(service=service_3, template=template_3, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
||||
billable_unit=7, rate=0.11)
|
||||
|
||||
service_4 = create_service(service_name='d - email only')
|
||||
email_template = create_template(service=service_4, template_type='email')
|
||||
org_4 = create_organisation(name="Org for {}".format(service_4.name))
|
||||
dao_add_service_to_organisation(service=service_4, organisation_id=org_4.id)
|
||||
create_annual_billing(service_id=service_4.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
||||
create_ft_billing(service=service_4, template=email_template, bst_date=datetime(2019, 5, 22), notifications_sent=5,
|
||||
notification_type='email', billable_unit=0, rate=0)
|
||||
|
||||
results = fetch_sms_billing_for_all_services(datetime(2019, 5, 1), datetime(2019, 5, 31))
|
||||
assert len(results) == 3
|
||||
|
||||
assert results[0].organisation_id == org.id
|
||||
assert results[0].service_id == service.id
|
||||
assert results[0].sms_billable_units == 3
|
||||
assert results[0].sms_remainder == 8
|
||||
assert results[0].chargeable_billable_sms == 0
|
||||
assert results[0].sms_cost == Decimal('0')
|
||||
|
||||
assert results[1].organisation_id == org_2.id
|
||||
assert results[1].service_id == service_2.id
|
||||
assert results[1].sms_billable_units == 3
|
||||
assert results[1].sms_remainder == 0
|
||||
assert results[1].chargeable_billable_sms == 3
|
||||
assert results[1].sms_cost == Decimal('0.33')
|
||||
|
||||
assert results[2].organisation_id == org_3.id
|
||||
assert results[2].service_id == service_3.id
|
||||
assert results[2].sms_billable_units == 7
|
||||
assert results[2].sms_remainder == 5
|
||||
assert results[2].chargeable_billable_sms == 2
|
||||
assert results[2].sms_cost == Decimal('0.22')
|
||||
|
||||
|
||||
def test_fetch_sms_billing_for_all_services_without_an_organisation_appears(notify_db_session):
|
||||
service = create_service(service_name='a - has free allowance')
|
||||
template = create_template(service=service)
|
||||
org = create_organisation(name="Org for {}".format(service.name))
|
||||
service.organisation = org
|
||||
create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
||||
create_ft_billing(service=service, template=template,
|
||||
bst_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=2, rate=0.11)
|
||||
create_ft_billing(service=service, template=template, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
||||
billable_unit=2, rate=0.11)
|
||||
create_ft_billing(service=service, template=template, bst_date=datetime(2019, 5, 22), notification_type='sms',
|
||||
billable_unit=1, rate=0.11)
|
||||
|
||||
service_2 = create_service(service_name='b - used free allowance')
|
||||
template_2 = create_template(service=service_2)
|
||||
|
||||
create_annual_billing(service_id=service_2.id, free_sms_fragment_limit=10, financial_year_start=2019)
|
||||
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2019, 4, 20), notification_type='sms',
|
||||
billable_unit=12, rate=0.11)
|
||||
create_ft_billing(service=service_2, template=template_2, bst_date=datetime(2019, 5, 20), notification_type='sms',
|
||||
billable_unit=3, rate=0.11)
|
||||
results = fetch_sms_billing_for_all_services(datetime(2019, 5, 1), datetime(2019, 5, 31))
|
||||
assert len(results) == 2
|
||||
|
||||
assert results[0].organisation_name == org.name
|
||||
assert results[0].service_id == service.id
|
||||
assert results[0].sms_billable_units == 3
|
||||
assert results[0].sms_remainder == 8
|
||||
assert results[0].chargeable_billable_sms == 0
|
||||
assert results[0].sms_cost == Decimal('0')
|
||||
|
||||
assert not results[1].organisation_name
|
||||
assert results[1].service_id == service_2.id
|
||||
assert results[1].sms_billable_units == 3
|
||||
assert results[1].sms_remainder == 0
|
||||
assert results[1].chargeable_billable_sms == 3
|
||||
assert results[1].sms_cost == Decimal('0.33')
|
||||
|
||||
|
||||
def test_fetch_letter_costs_for_all_services(notify_db_session):
|
||||
org, org_3, service, service_3 = set_up_letter_data()
|
||||
|
||||
results = fetch_letter_costs_for_all_services(datetime(2019, 6, 1), datetime(2019, 9, 30))
|
||||
|
||||
assert len(results) == 2
|
||||
assert results[0].organisation_id == org.id
|
||||
assert results[0].organisation_name == org.name
|
||||
assert results[0].service_id == service.id
|
||||
assert results[0].service_name == service.name
|
||||
assert results[0].letter_cost == Decimal('3.40')
|
||||
|
||||
assert results[1].organisation_id == org_3.id
|
||||
assert results[1].organisation_name == org_3.name
|
||||
assert results[1].service_id == service_3.id
|
||||
assert results[1].service_name == service_3.name
|
||||
assert results[1].letter_cost == Decimal('6.20')
|
||||
|
||||
|
||||
def test_fetch_letter_line_items_for_all_service(notify_db_session):
|
||||
set_up_letter_data()
|
||||
results = fetch_letter_line_items_for_all_services(datetime(2019, 6, 1), datetime(2019, 9, 30))
|
||||
print(results)
|
||||
assert len(results) == 4
|
||||
|
||||
|
||||
def set_up_letter_data():
|
||||
service = create_service(service_name='a - first service')
|
||||
letter_template = create_template(service=service, template_type='letter')
|
||||
org = create_organisation(name="Org for {}".format(service.name))
|
||||
dao_add_service_to_organisation(service=service, organisation_id=org.id)
|
||||
service_2 = create_service(service_name='b - second service')
|
||||
sms_template = create_template(service=service, template_type='sms')
|
||||
dao_add_service_to_organisation(service=service_2, organisation_id=org.id)
|
||||
service_3 = create_service(service_name='c - third service')
|
||||
template_3 = create_template(service=service_3)
|
||||
org_3 = create_organisation(name="Org for {}".format(service_3.name))
|
||||
dao_add_service_to_organisation(service=service_3, organisation_id=org_3.id)
|
||||
create_ft_billing(bst_date=datetime(2019, 8, 12), service=service_2, notification_type='sms',
|
||||
template=sms_template)
|
||||
create_ft_billing(bst_date=datetime(2019, 8, 15), service=service, notification_type='letter',
|
||||
template=letter_template,
|
||||
notifications_sent=2, billable_unit=1, rate=.35, postage='first')
|
||||
create_ft_billing(bst_date=datetime(2019, 8, 20), service=service, notification_type='letter',
|
||||
template=letter_template,
|
||||
notifications_sent=6, billable_unit=2, rate=.45, postage='second')
|
||||
create_ft_billing(bst_date=datetime(2019, 9, 1), service=service_3, notification_type='letter',
|
||||
template=template_3,
|
||||
notifications_sent=2, billable_unit=3, rate=.50, postage='first')
|
||||
create_ft_billing(bst_date=datetime(2019, 9, 20), service=service_3, notification_type='letter',
|
||||
template=template_3,
|
||||
notifications_sent=8, billable_unit=5, rate=.65, postage='second')
|
||||
return org, org_3, service, service_3
|
||||
|
||||
@@ -108,7 +108,8 @@ def create_service(
|
||||
check_if_service_exists=False,
|
||||
go_live_user=None,
|
||||
go_live_at=None,
|
||||
crown=True
|
||||
crown=True,
|
||||
organisation=None
|
||||
):
|
||||
if check_if_service_exists:
|
||||
service = Service.query.filter_by(name=service_name).first()
|
||||
|
||||
Reference in New Issue
Block a user