From dd57468147fbf11e8529c24b1f194f5d5c0c0290 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 3 Dec 2019 17:02:58 +0000 Subject: [PATCH] remove notification_type and service from create_ft_billing they can both be inferred from the template, and specifying them just leads to unnecessary risk of errors --- tests/app/billing/test_billing.py | 28 -------- tests/app/dao/test_ft_billing_dao.py | 81 +++++++--------------- tests/app/dao/test_provider_details_dao.py | 12 ++-- tests/app/dao/test_services_dao.py | 16 ++--- tests/app/db.py | 52 +++++--------- tests/app/provider_details/test_rest.py | 4 +- tests/app/service/test_rest.py | 8 +-- 7 files changed, 62 insertions(+), 139 deletions(-) diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index ceab1b354..b734f02f6 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -187,20 +187,14 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(client, notify_db_session): for day in range(1, monthrange(2016, month)[1] + 1): d = str(day).zfill(2) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, template=sms_template, - notification_type='sms', billable_unit=1, 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', billable_unit=1, rate=0.33, postage='second') @@ -242,26 +236,18 @@ def set_up_yearly_data(): for day in range(1, monthrange(2016, month)[1] + 1): d = str(day).zfill(2) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, template=sms_template, - notification_type='sms', rate=0.0162) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, template=sms_template, - notification_type='sms', rate_multiplier=2, rate=0.0162) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, template=email_template, - notification_type='email', billable_unit=0, rate=0) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, template=letter_template, - notification_type='letter', rate=0.33, postage='second') start_date, end_date = get_month_start_and_end_date_in_utc(datetime(2016, int(mon), 1)) @@ -397,45 +383,35 @@ def set_up_data_for_all_cases(): email_template = create_template(service=service, template_type="email") letter_template = create_template(service=service, template_type="letter") create_ft_billing(bst_date='2018-05-16', - notification_type='sms', template=sms_template, - service=service, rate_multiplier=1, international=False, rate=0.162, billable_unit=1, notifications_sent=1) create_ft_billing(bst_date='2018-05-17', - notification_type='sms', template=sms_template, - service=service, rate_multiplier=2, international=False, rate=0.162, billable_unit=2, notifications_sent=1) create_ft_billing(bst_date='2018-05-16', - notification_type='sms', template=sms_template, - service=service, rate_multiplier=2, international=False, rate=0.0150, billable_unit=2, notifications_sent=1) create_ft_billing(bst_date='2018-05-16', - notification_type='email', template=email_template, - service=service, rate_multiplier=1, international=False, rate=0, billable_unit=0, notifications_sent=1) create_ft_billing(bst_date='2018-05-16', - notification_type='letter', template=letter_template, - service=service, rate_multiplier=1, international=False, rate=0.33, @@ -443,9 +419,7 @@ def set_up_data_for_all_cases(): notifications_sent=1, postage='second') create_ft_billing(bst_date='2018-05-17', - notification_type='letter', template=letter_template, - service=service, rate_multiplier=1, international=False, rate=0.36, @@ -453,9 +427,7 @@ def set_up_data_for_all_cases(): notifications_sent=1, postage='second') create_ft_billing(bst_date='2018-05-18', - notification_type='letter', template=letter_template, - service=service, rate_multiplier=1, international=False, rate=0.39, diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index ae805e39a..3b5554405 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -51,20 +51,10 @@ def set_up_yearly_data(): for n in range((end_date - start_date).days): dt = start_date + timedelta(days=n) - create_ft_billing(bst_date=dt, - template=sms_template, - rate=0.162) - create_ft_billing(bst_date=dt, - template=email_template, - rate=0) - create_ft_billing(bst_date=dt, - template=letter_template, - rate=0.33, - postage='second') - create_ft_billing(bst_date=dt, - template=letter_template, - rate=0.30, - postage='second') + create_ft_billing(bst_date=dt, template=sms_template, rate=0.162) + create_ft_billing(bst_date=dt, template=email_template, rate=0) + create_ft_billing(bst_date=dt, template=letter_template, rate=0.33, postage='second') + create_ft_billing(bst_date=dt, template=letter_template, rate=0.30, postage='second') return service @@ -382,16 +372,12 @@ def test_fetch_monthly_billing_for_year(notify_db_session): template = create_template(service=service, template_type="sms") for i in range(1, 31): create_ft_billing(bst_date='2018-06-{}'.format(i), - service=service, template=template, - notification_type='sms', rate_multiplier=2, rate=0.162) for i in range(1, 32): create_ft_billing(bst_date='2018-07-{}'.format(i), - service=service, template=template, - notification_type='sms', rate=0.158) results = fetch_monthly_billing_for_year(service_id=service.id, year=2018) @@ -417,11 +403,7 @@ 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): - create_ft_billing(bst_date='2018-07-{}'.format(i), - service=service, - template=template, - notification_type='email', - rate=0.162) + create_ft_billing(bst_date='2018-07-{}'.format(i), template=template) create_notification(template=template, status='delivered') assert db.session.query(FactBilling.bst_date).count() == 31 @@ -497,11 +479,11 @@ def test_delete_billing_data(notify_db_session): other_service_template = create_template(service_2, 'sms') existing_rows_to_delete = [ # noqa - create_ft_billing('2018-01-01', 'sms', sms_template, service_1, billable_unit=1), - create_ft_billing('2018-01-01', 'email', email_template, service_1, billable_unit=2) + create_ft_billing('2018-01-01', sms_template, billable_unit=1), + create_ft_billing('2018-01-01', email_template, billable_unit=2) ] - other_day = create_ft_billing('2018-01-02', 'sms', sms_template, service_1, billable_unit=3) - other_service = create_ft_billing('2018-01-01', 'sms', other_service_template, service_2, billable_unit=4) + other_day = create_ft_billing('2018-01-02', sms_template, billable_unit=3) + other_service = create_ft_billing('2018-01-01', other_service_template, billable_unit=4) delete_billing_data_for_service_for_day('2018-01-01', service_1.id) @@ -517,22 +499,18 @@ def test_fetch_sms_free_allowance_remainder_with_two_services(notify_db_session) 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) + create_ft_billing(template=template, bst_date=datetime(2016, 4, 20), billable_unit=2, rate=0.11) + create_ft_billing(template=template, bst_date=datetime(2016, 5, 20), 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) + create_ft_billing(template=template_2, bst_date=datetime(2016, 4, 20), billable_unit=12, rate=0.11) + create_ft_billing(template=template_2, bst_date=datetime(2016, 4, 22), billable_unit=10, rate=0.11) + create_ft_billing(template=template_2, bst_date=datetime(2016, 5, 20), 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] @@ -548,8 +526,7 @@ def test_fetch_sms_billing_for_all_services_for_first_quarter(notify_db_session) 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=25000, financial_year_start=2019) - create_ft_billing(service=service, template=template, - bst_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=44, rate=0.11) + create_ft_billing(template=template, bst_date=datetime(2019, 4, 20), billable_unit=44, rate=0.11) results = fetch_sms_billing_for_all_services(datetime(2019, 4, 1), datetime(2019, 5, 30)) assert len(results) == 1 assert results[0] == (org.name, org.id, service.name, service.id, 25000, Decimal('0.11'), 25000, 44, 0, @@ -562,39 +539,33 @@ def test_fetch_sms_billing_for_all_services_with_remainder(notify_db_session): 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) + create_ft_billing(template=template, bst_date=datetime(2019, 4, 20), billable_unit=2, rate=0.11) + create_ft_billing(template=template, bst_date=datetime(2019, 5, 20), billable_unit=2, rate=0.11) + create_ft_billing(template=template, bst_date=datetime(2019, 5, 22), 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) + create_ft_billing(template=template_2, bst_date=datetime(2019, 4, 20), billable_unit=12, rate=0.11) + create_ft_billing(template=template_2, bst_date=datetime(2019, 5, 20), 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) + create_ft_billing(template=template_3, bst_date=datetime(2019, 4, 20), billable_unit=5, rate=0.11) + create_ft_billing(template=template_3, bst_date=datetime(2019, 5, 20), 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) + create_ft_billing(template=email_template, bst_date=datetime(2019, 5, 22), notifications_sent=5, + billable_unit=0, rate=0) results = fetch_sms_billing_for_all_services(datetime(2019, 5, 1), datetime(2019, 5, 31)) assert len(results) == 3 diff --git a/tests/app/dao/test_provider_details_dao.py b/tests/app/dao/test_provider_details_dao.py index f665d229b..4445f3aa9 100644 --- a/tests/app/dao/test_provider_details_dao.py +++ b/tests/app/dao/test_provider_details_dao.py @@ -214,13 +214,13 @@ def test_dao_get_provider_stats(notify_db_session): sms_template_1 = create_template(service_1, 'sms') sms_template_2 = create_template(service_2, 'sms') - create_ft_billing('2017-06-05', 'sms', sms_template_2, service_2, provider='firetext', billable_unit=4) - create_ft_billing('2018-05-31', 'sms', sms_template_1, service_1, provider='mmg', billable_unit=1) - create_ft_billing('2018-06-01', 'sms', sms_template_1, service_1, provider='mmg', + create_ft_billing('2017-06-05', sms_template_2, provider='firetext', billable_unit=4) + create_ft_billing('2018-05-31', sms_template_1, provider='mmg', billable_unit=1) + create_ft_billing('2018-06-01', sms_template_1, provider='mmg', rate_multiplier=2, billable_unit=1) - create_ft_billing('2018-06-03', 'sms', sms_template_2, service_2, provider='firetext', billable_unit=4) - create_ft_billing('2018-06-15', 'sms', sms_template_1, service_1, provider='firetext', billable_unit=1) - create_ft_billing('2018-06-28', 'sms', sms_template_2, service_2, provider='mmg', billable_unit=2) + create_ft_billing('2018-06-03', sms_template_2, provider='firetext', billable_unit=4) + create_ft_billing('2018-06-15', sms_template_1, provider='firetext', billable_unit=1) + create_ft_billing('2018-06-28', sms_template_2, provider='mmg', billable_unit=2) result = dao_get_provider_stats() diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 949c26996..281a19c17 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -444,28 +444,28 @@ def test_get_all_user_services_should_return_empty_list_if_no_services_for_user( def test_dao_fetch_live_services_data(sample_user): org = create_organisation(organisation_type='nhs_central') service = create_service(go_live_user=sample_user, go_live_at='2014-04-20T10:00:00') - template = create_template(service=service) + sms_template = create_template(service=service) service_2 = create_service(service_name='second', go_live_at='2017-04-20T10:00:00', go_live_user=sample_user) service_3 = create_service(service_name='third', go_live_at='2016-04-20T10:00:00') # below services should be filtered out: create_service(service_name='restricted', restricted=True) create_service(service_name='not_active', active=False) create_service(service_name='not_live', count_as_live=False) - template2 = create_template(service=service, template_type='email') + email_template = create_template(service=service, template_type='email') template_letter_1 = create_template(service=service, template_type='letter') template_letter_2 = create_template(service=service_2, template_type='letter') dao_add_service_to_organisation(service=service, organisation_id=org.id) # two sms billing records for 1st service within current financial year: - create_ft_billing(bst_date='2019-04-20', notification_type='sms', template=template, service=service) - create_ft_billing(bst_date='2019-04-21', notification_type='sms', template=template, service=service) + create_ft_billing(bst_date='2019-04-20', template=sms_template) + create_ft_billing(bst_date='2019-04-21', template=sms_template) # one sms billing record for 1st service from previous financial year, should not appear in the result: - create_ft_billing(bst_date='2018-04-20', notification_type='sms', template=template, service=service) + create_ft_billing(bst_date='2018-04-20', template=sms_template) # one email billing record for 1st service within current financial year: - create_ft_billing(bst_date='2019-04-20', notification_type='email', template=template2, service=service) + create_ft_billing(bst_date='2019-04-20', template=email_template) # one letter billing record for 1st service within current financial year: - create_ft_billing(bst_date='2019-04-15', notification_type='letter', template=template_letter_1, service=service) + create_ft_billing(bst_date='2019-04-15', template=template_letter_1) # one letter billing record for 2nd service within current financial year: - create_ft_billing(bst_date='2019-04-16', notification_type='letter', template=template_letter_2, service=service_2) + create_ft_billing(bst_date='2019-04-16', template=template_letter_2) # 1st service: billing from 2018 and 2019 create_annual_billing(service.id, 500, 2018) diff --git a/tests/app/db.py b/tests/app/db.py index 88e54630c..4cbcfdfc2 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -675,9 +675,8 @@ def create_daily_sorted_letter(billing_day=date(2018, 1, 18), def create_ft_billing(bst_date, - notification_type=None, - template=None, - service=None, + template, + *, provider='test', rate_multiplier=1, international=False, @@ -686,12 +685,6 @@ def create_ft_billing(bst_date, notifications_sent=1, postage='none', ): - assert template is not None - if service is not None: - assert service == template.service - if notification_type is not None: - assert notification_type == template.template_type - data = FactBilling(bst_date=bst_date, service_id=template.service_id, template_id=template.id, @@ -904,7 +897,7 @@ def set_up_usage_data(start_date): one_month_later = start_date + timedelta(days=31) service = create_service(service_name='a - with sms and letter') - letter_template = create_template(service=service, template_type='letter') + letter_template_1 = create_template(service=service, template_type='letter') sms_template_1 = create_template(service=service, template_type='sms') create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=year) org = create_organisation(name="Org for {}".format(service.name)) @@ -922,41 +915,28 @@ def set_up_usage_data(start_date): sms_template = create_template(service=service_sms_only, template_type='sms') create_annual_billing(service_id=service_sms_only.id, free_sms_fragment_limit=10, financial_year_start=year) - create_ft_billing(bst_date=one_week_earlier, service=service, notification_type='sms', - template=sms_template_1, billable_unit=2, rate=0.11) - create_ft_billing(bst_date=start_date, service=service, notification_type='sms', - template=sms_template_1, billable_unit=2, rate=0.11) - create_ft_billing(bst_date=two_days_later, service=service, notification_type='sms', - template=sms_template_1, billable_unit=1, rate=0.11) - create_ft_billing(bst_date=one_week_later, service=service, notification_type='letter', - template=letter_template, + create_ft_billing(bst_date=one_week_earlier, template=sms_template_1, billable_unit=2, rate=0.11) + create_ft_billing(bst_date=start_date, template=sms_template_1, billable_unit=2, rate=0.11) + create_ft_billing(bst_date=two_days_later, template=sms_template_1, billable_unit=1, rate=0.11) + create_ft_billing(bst_date=one_week_later, template=letter_template_1, notifications_sent=2, billable_unit=1, rate=.35, postage='first') - create_ft_billing(bst_date=one_month_later, service=service, notification_type='letter', - template=letter_template, + create_ft_billing(bst_date=one_month_later, template=letter_template_1, notifications_sent=4, billable_unit=2, rate=.45, postage='second') - create_ft_billing(bst_date=one_week_later, service=service, notification_type='letter', - template=letter_template, + create_ft_billing(bst_date=one_week_later, template=letter_template_1, notifications_sent=2, billable_unit=2, rate=.45, postage='second') - create_ft_billing(bst_date=one_week_earlier, service=service_sms_only, notification_type='sms', - template=sms_template, rate=0.11, billable_unit=12) - create_ft_billing(bst_date=two_days_later, service=service_sms_only, notification_type='sms', - template=sms_template, rate=0.11) - create_ft_billing(bst_date=one_week_later, service=service_sms_only, notification_type='sms', - template=sms_template, billable_unit=2, rate=0.11) + create_ft_billing(bst_date=one_week_earlier, template=sms_template, rate=0.11, billable_unit=12) + create_ft_billing(bst_date=two_days_later, template=sms_template, rate=0.11) + create_ft_billing(bst_date=one_week_later, template=sms_template, billable_unit=2, rate=0.11) - create_ft_billing(bst_date=start_date, service=service_3, notification_type='letter', - template=letter_template_3, + create_ft_billing(bst_date=start_date, template=letter_template_3, notifications_sent=2, billable_unit=3, rate=.50, postage='first') - create_ft_billing(bst_date=one_week_later, service=service_3, notification_type='letter', - template=letter_template_3, + create_ft_billing(bst_date=one_week_later, template=letter_template_3, notifications_sent=8, billable_unit=5, rate=.65, postage='second') - create_ft_billing(bst_date=one_month_later, service=service_3, notification_type='letter', - template=letter_template_3, + create_ft_billing(bst_date=one_month_later, template=letter_template_3, notifications_sent=12, billable_unit=5, rate=.65, postage='second') - create_ft_billing(bst_date=two_days_later, service=service_4, notification_type='letter', - template=letter_template_4, + create_ft_billing(bst_date=two_days_later, template=letter_template_4, notifications_sent=15, billable_unit=4, rate=.55, postage='second') return org, org_3, service, service_3, service_4, service_sms_only diff --git a/tests/app/provider_details/test_rest.py b/tests/app/provider_details/test_rest.py index 9e58d7c9a..307928658 100644 --- a/tests/app/provider_details/test_rest.py +++ b/tests/app/provider_details/test_rest.py @@ -32,8 +32,8 @@ def test_get_provider_details_by_id(client, notify_db): @freeze_time('2018-06-28 12:00') -def test_get_provider_contains_correct_fields(client, sample_service, sample_template): - create_ft_billing('2018-06-01', 'sms', sample_template, sample_service, provider='mmg', billable_unit=1) +def test_get_provider_contains_correct_fields(client, sample_template): + create_ft_billing('2018-06-01', sample_template, provider='mmg', billable_unit=1) response = client.get( '/provider-details', diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index a6e9ffc71..5677428be 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -169,11 +169,11 @@ def test_get_live_services_data(sample_user, admin_request): service = create_service(go_live_user=sample_user, go_live_at=datetime(2018, 1, 1)) service_2 = create_service(service_name='second', go_live_at=datetime(2019, 1, 1), go_live_user=sample_user) - template = create_template(service=service) - template2 = create_template(service=service, template_type='email') + sms_template = create_template(service=service) + email_template = create_template(service=service, template_type='email') dao_add_service_to_organisation(service=service, organisation_id=org.id) - create_ft_billing(bst_date='2019-04-20', notification_type='sms', template=template, service=service) - create_ft_billing(bst_date='2019-04-20', notification_type='email', template=template2, service=service) + create_ft_billing(bst_date='2019-04-20', template=sms_template) + create_ft_billing(bst_date='2019-04-20', template=email_template) create_annual_billing(service.id, 1, 2019) create_annual_billing(service_2.id, 2, 2018)