def test_fetch_sms_billing_for_all_services_for_first_quarter(notify_db_session): # This test is useful because the inner query resultset is empty. 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=25000, financial_year_start=2019) create_ft_billing( service=service, template=template, utc_date=datetime(2019, 4, 20), notification_type="sms", billable_unit=44, rate=0.11, ) results = fetch_sms_billing_for_all_services(datetime(2019, 4, 2), 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, Decimal("0"), )
def test_get_organisation_services_usage(admin_request, notify_db_session): org = create_organisation(name='Organisation without live services') service = create_service() template = create_template(service=service) 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(bst_date=datetime.utcnow().date(), template=template, billable_unit=19, rate=0.060, notifications_sent=19) response = admin_request.get( 'organisation.get_organisation_services_usage', organisation_id=org.id, **{"year": 2019}) assert len(response) == 1 assert len(response['services']) == 1 service_usage = response['services'][0] assert service_usage['service_id'] == str(service.id) assert service_usage['service_name'] == service.name assert service_usage['chargeable_billable_sms'] == 9.0 assert service_usage['emails_sent'] == 0 assert service_usage['free_sms_limit'] == 10 assert service_usage['letter_cost'] == 0 assert service_usage['sms_billable_units'] == 19 assert service_usage['sms_remainder'] == 10 assert service_usage['sms_cost'] == 0.54
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, utc_date=datetime(2016, 4, 20), notification_type='sms', billable_unit=2, rate=0.11) create_ft_billing(service=service, template=template, utc_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, utc_date=datetime(2016, 4, 20), notification_type='sms', billable_unit=12, rate=0.11) create_ft_billing(service=service_2, template=template_2, utc_date=datetime(2016, 4, 22), notification_type='sms', billable_unit=10, rate=0.11) create_ft_billing(service=service_2, template=template_2, utc_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_dao_fetch_live_services_data(sample_user): org = create_organisation(organisation_type='other') service = create_service(go_live_user=sample_user, go_live_at='2014-04-20T10:00:00') 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') 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(utc_date='2019-04-20', notification_type='sms', template=template, service=service) create_ft_billing(utc_date='2019-04-21', notification_type='sms', template=template, service=service) # one sms billing record for 1st service from previous financial year, should not appear in the result: create_ft_billing(utc_date='2018-04-20', notification_type='sms', template=template, service=service) # one email billing record for 1st service within current financial year: create_ft_billing(utc_date='2019-04-20', notification_type='email', template=template2, service=service) # one letter billing record for 1st service within current financial year: create_ft_billing(utc_date='2019-04-15', notification_type='letter', template=template_letter_1, service=service) # one letter billing record for 2nd service within current financial year: create_ft_billing(utc_date='2019-04-16', notification_type='letter', template=template_letter_2, service=service_2) # 1st service: billing from 2018 and 2019 create_annual_billing(service.id, 500, 2018) create_annual_billing(service.id, 100, 2019) # 2nd service: billing from 2018 create_annual_billing(service_2.id, 300, 2018) # 3rd service: billing from 2019 create_annual_billing(service_3.id, 200, 2019) results = dao_fetch_live_services_data() assert len(results) == 3
def sample_broadcast_service(notify_db_session, broadcast_organisation): user = create_user() service_name = 'Sample broadcast service' email_from = service_name.lower().replace(' ', '.') data = { 'name': service_name, 'message_limit': 1000, 'restricted': False, 'email_from': email_from, 'created_by': user, 'crown': True, 'count_as_live': False, } service = Service.query.filter_by(name=service_name).first() if not service: service = Service(**data) dao_create_service(service, user, service_permissions=[BROADCAST_TYPE]) insert_or_update_service_broadcast_settings(service, channel="severe") dao_add_service_to_organisation(service, current_app.config['BROADCAST_ORGANISATION_ID']) else: if user not in service.users: dao_add_user_to_service(service, user) return service
def test_fetch_usage_year_for_organisation(notify_db_session): fixtures = set_up_usage_data(datetime(2019, 5, 1)) service_with_emails_for_org = create_service( service_name='Service with emails for org') dao_add_service_to_organisation(service=service_with_emails_for_org, organisation_id=fixtures["org_1"].id) template = create_template(service=service_with_emails_for_org, template_type='email') create_ft_billing(bst_date=datetime(2019, 5, 1), template=template, notifications_sent=1100) results = fetch_usage_year_for_organisation(fixtures["org_1"].id, 2019) assert len(results) == 2 first_row = results[str(fixtures["service_1_sms_and_letter"].id)] assert first_row['service_id'] == fixtures["service_1_sms_and_letter"].id assert first_row['service_name'] == fixtures[ "service_1_sms_and_letter"].name assert first_row['free_sms_limit'] == 10 assert first_row['sms_remainder'] == 10 assert first_row['chargeable_billable_sms'] == 0 assert first_row['sms_cost'] == 0.0 assert first_row['letter_cost'] == 3.4 assert first_row['emails_sent'] == 0 second_row = results[str(service_with_emails_for_org.id)] assert second_row['service_id'] == service_with_emails_for_org.id assert second_row['service_name'] == service_with_emails_for_org.name assert second_row['free_sms_limit'] == 0 assert second_row['sms_remainder'] == 0 assert second_row['chargeable_billable_sms'] == 0 assert second_row['sms_cost'] == 0 assert second_row['letter_cost'] == 0 assert second_row['emails_sent'] == 1100
def test_add_service_to_organisation(notify_db, notify_db_session, sample_service, sample_organisation): assert sample_organisation.services == [] dao_add_service_to_organisation(sample_service, sample_organisation.id) assert len(sample_organisation.services) == 1 assert sample_organisation.services[0].id == sample_service.id
def test_rest_get_organisation_services(admin_request, sample_organisation, sample_service): dao_add_service_to_organisation(sample_service, sample_organisation.id) response = admin_request.get('organisation.get_organisation_services', organisation_id=str(sample_organisation.id), _expected_status=200) assert response == [sample_service.serialize_for_org_dashboard()]
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') 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) 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', 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', template=sms_template) # one email billing record for 1st service within current financial year: 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', template=template_letter_1) # one letter billing record for 2nd service within current financial year: 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) create_annual_billing(service.id, 100, 2019) # 2nd service: billing from 2018 create_annual_billing(service_2.id, 300, 2018) # 3rd service: billing from 2019 create_annual_billing(service_3.id, 200, 2019) results = dao_fetch_live_services_data() assert len(results) == 3 # checks the results and that they are ordered by date: assert results == [ {'service_id': mock.ANY, 'service_name': 'Sample service', 'organisation_name': 'test_org_1', 'organisation_type': 'nhs_central', 'consent_to_research': None, 'contact_name': 'Test User', 'contact_email': '*****@*****.**', 'contact_mobile': '+447700900986', 'live_date': datetime(2014, 4, 20, 10, 0), 'sms_volume_intent': None, 'email_volume_intent': None, 'letter_volume_intent': None, 'sms_totals': 2, 'email_totals': 1, 'letter_totals': 1, 'free_sms_fragment_limit': 100}, {'service_id': mock.ANY, 'service_name': 'third', 'organisation_name': None, 'consent_to_research': None, 'organisation_type': None, 'contact_name': None, 'contact_email': None, 'contact_mobile': None, 'live_date': datetime(2016, 4, 20, 10, 0), 'sms_volume_intent': None, 'email_volume_intent': None, 'letter_volume_intent': None, 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 0, 'free_sms_fragment_limit': 200}, {'service_id': mock.ANY, 'service_name': 'second', 'organisation_name': None, 'consent_to_research': None, 'contact_name': 'Test User', 'contact_email': '*****@*****.**', 'contact_mobile': '+447700900986', 'live_date': datetime(2017, 4, 20, 10, 0), 'sms_volume_intent': None, 'organisation_type': None, 'email_volume_intent': None, 'letter_volume_intent': None, 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 1, 'free_sms_fragment_limit': 300} ]
def link_service_to_organisation(organisation_id): data = request.get_json() validate(data, post_link_service_to_organisation_schema) service = dao_fetch_service_by_id(data["service_id"]) service.organisation = None dao_add_service_to_organisation(service, organisation_id) return "", 204
def test_add_service_to_multiple_organisation_raises_error( notify_db, notify_db_session, sample_service, sample_organisation): another_org = create_organisation() dao_add_service_to_organisation(sample_service, sample_organisation.id) with pytest.raises(IntegrityError): dao_add_service_to_organisation(sample_service, another_org.id) assert len(sample_organisation.services) == 1 assert sample_organisation.services[0] == sample_service
def link_service_to_organisation(organisation_id): data = request.get_json() validate(data, post_link_service_to_organisation_schema) service = dao_fetch_service_by_id(data['service_id']) service.organisation = None with transaction(): dao_add_service_to_organisation(service, organisation_id) set_default_free_allowance_for_service(service, year_start=None) return '', 204
def test_get_organisation_by_service_id(sample_service, sample_organisation): another_service = create_service(service_name='service 2') another_org = create_organisation() dao_add_service_to_organisation(sample_service, sample_organisation.id) dao_add_service_to_organisation(another_service, another_org.id) organisation_1 = dao_get_organisation_by_service_id(sample_service.id) organisation_2 = dao_get_organisation_by_service_id(another_service.id) assert organisation_1 == sample_organisation assert organisation_2 == another_org
def test_get_organisation_services(sample_service, sample_organisation): another_service = create_service(service_name='service 2') another_org = create_organisation() dao_add_service_to_organisation(sample_service, sample_organisation.id) dao_add_service_to_organisation(another_service, sample_organisation.id) org_services = dao_get_organisation_services(sample_organisation.id) other_org_services = dao_get_organisation_services(another_org.id) assert [sample_service.name, another_service.name] == sorted([s.name for s in org_services]) assert not other_org_services
def associate_services_to_organisations(): services = Service.get_history_model().query.filter_by(version=1).all() for s in services: created_by_user = User.query.filter_by(id=s.created_by_id).first() organisation = dao_get_organisation_by_email_address( created_by_user.email_address) service = dao_fetch_service_by_id(service_id=s.id) if organisation: dao_add_service_to_organisation(service=service, organisation_id=organisation.id) print("finished associating services to organisations")
def test_get_organisation_services_usage_sort_active_first( admin_request, notify_db_session): org = create_organisation(name='Organisation without live services') service = create_service(service_name='live service') archived_service = create_service(service_name='archived_service') template = create_template(service=service) dao_add_service_to_organisation(service=service, organisation_id=org.id) dao_add_service_to_organisation(service=archived_service, organisation_id=org.id) create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2019) create_ft_billing(bst_date=datetime.utcnow().date(), template=template, billable_unit=19, rate=0.060, notifications_sent=19) response = admin_request.get( 'organisation.get_organisation_services_usage', organisation_id=org.id, **{"year": 2019}) assert len(response) == 1 assert len(response['services']) == 2 first_service = response['services'][0] assert first_service['service_id'] == str(archived_service.id) assert first_service['service_name'] == archived_service.name assert first_service['active'] is True last_service = response['services'][1] assert last_service['service_id'] == str(service.id) assert last_service['service_name'] == service.name assert last_service['active'] is True dao_archive_service(service_id=archived_service.id) response_after_archive = admin_request.get( 'organisation.get_organisation_services_usage', organisation_id=org.id, **{"year": 2019}) first_service = response_after_archive['services'][0] assert first_service['service_id'] == str(service.id) assert first_service['service_name'] == service.name assert first_service['active'] is True last_service = response_after_archive['services'][1] assert last_service['service_id'] == str(archived_service.id) assert last_service['service_name'] == archived_service.name assert last_service['active'] is False
def test_add_service_to_organisation(sample_service, sample_organisation): assert sample_organisation.services == [] sample_service.organisation_type = "central" sample_organisation.organisation_type = "local" sample_organisation.crown = False dao_add_service_to_organisation(sample_service, sample_organisation.id) assert len(sample_organisation.services) == 1 assert sample_organisation.services[0].id == sample_service.id assert sample_service.organisation_type == sample_organisation.organisation_type assert sample_service.crown == sample_organisation.crown assert Service.get_history_model().query.filter_by( id=sample_service.id, version=2).one( ).organisation_type == sample_organisation.organisation_type assert sample_service.organisation_id == sample_organisation.id
def test_fetch_usage_year_for_organisation_only_returns_data_for_live_services( notify_db_session): org = create_organisation(name='Organisation without live services') live_service = create_service(restricted=False) sms_template = create_template(service=live_service) trial_service = create_service(restricted=True, service_name='trial_service') email_template = create_template(service=trial_service, template_type='email') trial_sms_template = create_template(service=trial_service, template_type='sms') trial_letter_template = create_template(service=trial_service, template_type='letter') dao_add_service_to_organisation(service=live_service, organisation_id=org.id) dao_add_service_to_organisation(service=trial_service, organisation_id=org.id) create_ft_billing(bst_date=datetime.utcnow().date(), template=sms_template, rate=0.0158, billable_unit=19, notifications_sent=19) create_ft_billing(bst_date=datetime.utcnow().date(), template=email_template, billable_unit=0, notifications_sent=100) create_ft_billing(bst_date=datetime.utcnow().date(), template=trial_sms_template, billable_unit=200, rate=0.0158, notifications_sent=100) create_ft_billing(bst_date=datetime.utcnow().date(), template=trial_letter_template, billable_unit=40, rate=0.30, notifications_sent=20) results = fetch_usage_year_for_organisation(organisation_id=org.id, year=2019) assert len(results) == 1 assert results[str(live_service.id)]['sms_billable_units'] == 19 assert results[str(live_service.id)]['emails_sent'] == 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, utc_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=2, rate=0.11) create_ft_billing(service=service, template=template, utc_date=datetime(2019, 5, 20), notification_type='sms', billable_unit=2, rate=0.11) create_ft_billing(service=service, template=template, utc_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, utc_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=12, rate=0.11) create_ft_billing(service=service_2, template=template_2, utc_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, utc_date=datetime(2019, 4, 20), notification_type='sms', billable_unit=5, rate=0.11) create_ft_billing(service=service_3, template=template_3, utc_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, utc_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 # (organisation_name, organisation_id, service_name, free_sms_fragment_limit, sms_rate, # sms_remainder, sms_billable_units, chargeable_billable_sms, sms_cost) assert results[0] == (org.name, org.id, service.name, service.id, 10, Decimal('0.11'), 8, 3, 0, Decimal('0')) assert results[1] == (org_2.name, org_2.id, service_2.name, service_2.id, 10, Decimal('0.11'), 0, 3, 3, Decimal('0.33')) assert results[2] == (org_3.name, org_3.id, service_3.name, service_3.id, 10, Decimal('0.11'), 5, 7, 2, Decimal('0.22'))
def test_fetch_usage_year_for_organisation_populates_ft_billing_for_today( notify_db_session): create_letter_rate(start_date=datetime.utcnow() - timedelta(days=1)) create_rate(start_date=datetime.utcnow() - timedelta(days=1), value=0.65, notification_type='sms') new_org = create_organisation(name='New organisation') service = create_service() template = create_template(service=service) dao_add_service_to_organisation(service=service, organisation_id=new_org.id) current_year = datetime.utcnow().year create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=current_year) assert FactBilling.query.count() == 0 create_notification(template=template, status='delivered') results = fetch_usage_year_for_organisation(organisation_id=new_org.id, year=current_year) assert len(results) == 1 assert FactBilling.query.count() == 1
def test_rest_get_organisation_services_is_ordered_by_name( admin_request, sample_organisation, sample_service): service_2 = create_service(service_name='service 2') service_1 = create_service(service_name='service 1') dao_add_service_to_organisation(service_1, sample_organisation.id) dao_add_service_to_organisation(service_2, sample_organisation.id) dao_add_service_to_organisation(sample_service, sample_organisation.id) response = admin_request.get('organisation.get_organisation_services', organisation_id=str(sample_organisation.id), _expected_status=200) assert response[0]['name'] == sample_service.name assert response[1]['name'] == service_1.name assert response[2]['name'] == service_2.name
def test_rest_get_organisation_services_inactive_services_at_end( admin_request, sample_organisation): inactive_service = create_service(service_name='inactive service', active=False) service = create_service() inactive_service_1 = create_service(service_name='inactive service 1', active=False) dao_add_service_to_organisation(inactive_service, sample_organisation.id) dao_add_service_to_organisation(service, sample_organisation.id) dao_add_service_to_organisation(inactive_service_1, sample_organisation.id) response = admin_request.get('organisation.get_organisation_services', organisation_id=str(sample_organisation.id), _expected_status=200) assert response[0]['name'] == service.name assert response[1]['name'] == inactive_service.name assert response[2]['name'] == inactive_service_1.name
def set_up_usage_data(start_date): year = int(start_date.strftime('%Y')) one_week_earlier = start_date - timedelta(days=7) two_days_later = start_date + timedelta(days=2) one_week_later = start_date + timedelta(days=7) one_month_later = start_date + timedelta(days=31) # service with sms and letters: service_1_sms_and_letter = create_service( service_name='a - with sms and letter', purchase_order_number="service purchase order number", billing_contact_names="service billing contact names", billing_contact_email_addresses="[email protected] [email protected]", billing_reference="service billing reference" ) letter_template_1 = create_template(service=service_1_sms_and_letter, template_type='letter') sms_template_1 = create_template(service=service_1_sms_and_letter, template_type='sms') create_annual_billing( service_id=service_1_sms_and_letter.id, free_sms_fragment_limit=10, financial_year_start=year ) org_1 = create_organisation( name="Org for {}".format(service_1_sms_and_letter.name), purchase_order_number="org1 purchase order number", billing_contact_names="org1 billing contact names", billing_contact_email_addresses="[email protected] [email protected]", billing_reference="org1 billing reference" ) dao_add_service_to_organisation( service=service_1_sms_and_letter, organisation_id=org_1.id ) 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, template=letter_template_1, notifications_sent=4, billable_unit=2, rate=.45, postage='second') create_ft_billing(bst_date=one_week_later, template=letter_template_1, notifications_sent=2, billable_unit=2, rate=.45, postage='second') # service with emails only: service_with_emails = create_service(service_name='b - emails') email_template = create_template(service=service_with_emails, template_type='email') org_2 = create_organisation( name='Org for {}'.format(service_with_emails.name), ) dao_add_service_to_organisation(service=service_with_emails, organisation_id=org_2.id) create_ft_billing(bst_date=start_date, template=email_template, notifications_sent=10) # service with letters: service_with_letters = create_service(service_name='c - letters only') letter_template_3 = create_template(service=service_with_letters, template_type='letter') org_for_service_with_letters = create_organisation( name="Org for {}".format(service_with_letters.name), purchase_order_number="org3 purchase order number", billing_contact_names="org3 billing contact names", billing_contact_email_addresses="[email protected] [email protected]", billing_reference="org3 billing reference" ) dao_add_service_to_organisation(service=service_with_letters, organisation_id=org_for_service_with_letters.id) 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, template=letter_template_3, notifications_sent=8, billable_unit=5, rate=.65, postage='second') create_ft_billing(bst_date=one_month_later, template=letter_template_3, notifications_sent=12, billable_unit=5, rate=.65, postage='second') # service with letters, without an organisation: service_with_letters_without_org = create_service(service_name='d - service without org') letter_template_4 = create_template(service=service_with_letters_without_org, template_type='letter') create_ft_billing(bst_date=two_days_later, template=letter_template_4, notifications_sent=7, billable_unit=4, rate=1.55, postage='rest-of-world') create_ft_billing(bst_date=two_days_later, template=letter_template_4, notifications_sent=8, billable_unit=4, rate=1.55, postage='europe') create_ft_billing(bst_date=two_days_later, template=letter_template_4, notifications_sent=2, billable_unit=1, rate=.35, postage='second') create_ft_billing(bst_date=two_days_later, template=letter_template_4, notifications_sent=1, billable_unit=1, rate=.50, postage='first') # service with chargeable SMS, without an organisation service_with_sms_without_org = create_service( service_name='b - chargeable sms', purchase_order_number="sms purchase order number", billing_contact_names="sms billing contact names", billing_contact_email_addresses="[email protected] [email protected]", billing_reference="sms billing reference" ) sms_template = create_template(service=service_with_sms_without_org, template_type='sms') create_annual_billing( service_id=service_with_sms_without_org.id, free_sms_fragment_limit=10, financial_year_start=year ) 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) # service with SMS within free allowance service_with_sms_within_allowance = create_service( service_name='e - sms within allowance' ) sms_template_2 = create_template(service=service_with_sms_within_allowance, template_type='sms') create_annual_billing( service_id=service_with_sms_within_allowance.id, free_sms_fragment_limit=10, financial_year_start=year ) create_ft_billing(bst_date=one_week_later, template=sms_template_2, billable_unit=2, rate=0.11) # dictionary with services and orgs to return return { "org_1": org_1, "service_1_sms_and_letter": service_1_sms_and_letter, "org_2": org_2, "service_with_emails": service_with_emails, "org_for_service_with_letters": org_for_service_with_letters, "service_with_letters": service_with_letters, "service_with_letters_without_org": service_with_letters_without_org, "service_with_sms_without_org": service_with_sms_without_org, "service_with_sms_within_allowance": service_with_sms_within_allowance, }
def set_up_usage_data(start_date): year = int(start_date.strftime('%Y')) one_week_earlier = start_date - timedelta(days=7) two_days_later = start_date + timedelta(days=2) one_week_later = start_date + timedelta(days=7) 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') 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)) dao_add_service_to_organisation(service=service, organisation_id=org.id) service_3 = create_service(service_name='c - letters only') 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) service_4 = create_service(service_name='d - service without org') template_4 = create_template(service=service_4, template_type='letter') service_sms_only = create_service(service_name='b - chargeable sms') 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) # service create_ft_billing(utc_date=one_week_earlier, service=service, notification_type='sms', template=sms_template_1, billable_unit=2, rate=0.11) create_ft_billing(utc_date=start_date, service=service, notification_type='sms', template=sms_template_1, billable_unit=2, rate=0.11) create_ft_billing(utc_date=two_days_later, service=service, notification_type='sms', template=sms_template_1, billable_unit=1, rate=0.11) create_ft_billing(utc_date=one_week_later, service=service, notification_type='letter', template=letter_template, notifications_sent=2, billable_unit=1, rate=.35, postage='first') create_ft_billing(utc_date=one_month_later, service=service, notification_type='letter', template=letter_template, notifications_sent=4, billable_unit=2, rate=.45, postage='second') create_ft_billing(utc_date=one_week_later, service=service, notification_type='letter', template=letter_template, notifications_sent=2, billable_unit=2, rate=.45, postage='second') # service SMS only create_ft_billing(utc_date=one_week_earlier, service=service_sms_only, notification_type='sms', template=sms_template, rate=0.11, billable_unit=12) create_ft_billing(utc_date=two_days_later, service=service_sms_only, notification_type='sms', template=sms_template, rate=0.11) create_ft_billing(utc_date=one_week_later, service=service_sms_only, notification_type='sms', template=sms_template, billable_unit=2, rate=0.11) # service3 create_ft_billing(utc_date=start_date, service=service_3, notification_type='letter', template=template_3, notifications_sent=2, billable_unit=3, rate=.50, postage='first') create_ft_billing(utc_date=one_week_later, service=service_3, notification_type='letter', template=template_3, notifications_sent=8, billable_unit=5, rate=.65, postage='second') create_ft_billing(utc_date=one_month_later, service=service_3, notification_type='letter', template=template_3, notifications_sent=12, billable_unit=5, rate=.65, postage='second') # service4 create_ft_billing(utc_date=two_days_later, service=service_4, notification_type='letter', template=template_4, notifications_sent=15, billable_unit=4, rate=.55, postage='second') return org, org_3, service, service_3, service_4, service_sms_only
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) 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") 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( utc_date="2019-04-20", notification_type="sms", template=template, service=service, ) create_ft_billing( utc_date="2019-04-21", notification_type="sms", template=template, service=service, ) # one sms billing record for 1st service from previous financial year, should not appear in the result: create_ft_billing( utc_date="2018-04-20", notification_type="sms", template=template, service=service, ) # one email billing record for 1st service within current financial year: create_ft_billing( utc_date="2019-04-20", notification_type="email", template=template2, service=service, ) # one letter billing record for 1st service within current financial year: create_ft_billing( utc_date="2019-04-15", notification_type="letter", template=template_letter_1, service=service, ) # one letter billing record for 2nd service within current financial year: create_ft_billing( utc_date="2019-04-16", notification_type="letter", template=template_letter_2, service=service_2, ) # 1st service: billing from 2018 and 2019 create_annual_billing(service.id, 500, 2018) create_annual_billing(service.id, 100, 2019) # 2nd service: billing from 2018 create_annual_billing(service_2.id, 300, 2018) # 3rd service: billing from 2019 create_annual_billing(service_3.id, 200, 2019) results = dao_fetch_live_services_data() assert len(results) == 3
def set_up_usage_data(start_date): year = int(start_date.strftime("%Y")) one_week_earlier = start_date - timedelta(days=7) two_days_later = start_date + timedelta(days=2) one_week_later = start_date + timedelta(days=7) 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") 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)) dao_add_service_to_organisation(service=service, organisation_id=org.id) service_3 = create_service(service_name="c - letters only") 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) service_4 = create_service(service_name="d - service without org") template_4 = create_template(service=service_4, template_type="letter") service_sms_only = create_service(service_name="b - chargeable sms") 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( utc_date=one_week_earlier, service=service, notification_type="sms", template=sms_template_1, billable_unit=2, rate=0.11, ) create_ft_billing( utc_date=start_date, service=service, notification_type="sms", template=sms_template_1, billable_unit=2, rate=0.11, ) create_ft_billing( utc_date=two_days_later, service=service, notification_type="sms", template=sms_template_1, billable_unit=1, rate=0.11, ) create_ft_billing( utc_date=one_week_later, service=service, notification_type="letter", template=letter_template, notifications_sent=2, billable_unit=1, rate=0.35, postage="first", ) create_ft_billing( utc_date=one_month_later, service=service, notification_type="letter", template=letter_template, notifications_sent=4, billable_unit=2, rate=0.45, postage="second", ) create_ft_billing( utc_date=one_week_later, service=service, notification_type="letter", template=letter_template, notifications_sent=2, billable_unit=2, rate=0.45, postage="second", ) create_ft_billing( utc_date=one_week_earlier, service=service_sms_only, notification_type="sms", template=sms_template, rate=0.11, billable_unit=12, ) create_ft_billing( utc_date=two_days_later, service=service_sms_only, notification_type="sms", template=sms_template, rate=0.11, ) create_ft_billing( utc_date=one_week_later, service=service_sms_only, notification_type="sms", template=sms_template, billable_unit=2, rate=0.11, ) create_ft_billing( utc_date=start_date, service=service_3, notification_type="letter", template=template_3, notifications_sent=2, billable_unit=3, rate=0.50, postage="first", ) create_ft_billing( utc_date=one_week_later, service=service_3, notification_type="letter", template=template_3, notifications_sent=8, billable_unit=5, rate=0.65, postage="second", ) create_ft_billing( utc_date=one_month_later, service=service_3, notification_type="letter", template=template_3, notifications_sent=12, billable_unit=5, rate=0.65, postage="second", ) create_ft_billing( utc_date=two_days_later, service=service_4, notification_type="letter", template=template_4, notifications_sent=15, billable_unit=4, rate=0.55, postage="second", ) return org, org_3, service, service_3, service_4, service_sms_only