def test_get_yearly_billing_data_for_year_includes_current_day_totals( sample_template): create_rate(start_date=FEB_2016_MONTH_START, value=0.0158, notification_type=SMS_TYPE) create_monthly_billing_entry(service=sample_template.service, start_date=JUL_2016_MONTH_START, end_date=JUL_2016_MONTH_END, notification_type=SMS_TYPE) billing_data = get_billing_data_for_financial_year( service_id=sample_template.service.id, year=2016, notification_types=[SMS_TYPE]) assert len(billing_data) == 1 assert billing_data[0].notification_type == SMS_TYPE assert billing_data[0].monthly_totals == [] create_notification(template=sample_template, created_at=datetime.utcnow(), sent_at=datetime.utcnow(), status='sending', billable_units=3) billing_data = get_billing_data_for_financial_year( service_id=sample_template.service.id, year=2016, notification_types=[SMS_TYPE]) assert billing_data[0].monthly_totals[0]['billing_units'] == 3
def test_get_billing_data_for_month_where_start_date_before_rate_returns_empty( sample_template): create_rate(datetime(2016, 4, 1), 0.014, SMS_TYPE) results = get_monthly_billing_data(service_id=sample_template.service_id, year=2015) assert not results
def test_add_monthly_billing_for_single_month_populates_correctly( sample_template, sample_email_template): create_rate(start_date=JAN_2017_MONTH_START, value=0.0158, notification_type=SMS_TYPE) letter_template = sample_letter_template(sample_template.service) create_letter_rate(crown=False) create_notification(template=sample_template, created_at=JAN_2017_MONTH_START, billable_units=1, rate_multiplier=2, status='delivered') create_notification(template=sample_email_template, created_at=JAN_2017_MONTH_START, status='delivered') create_notification(template=letter_template, created_at=JAN_2017_MONTH_START, status='delivered') create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=JAN_2017_MONTH_START) monthly_billing = MonthlyBilling.query.order_by( MonthlyBilling.notification_type).all() assert len(monthly_billing) == 3 _assert_monthly_billing(monthly_billing[0], sample_template.service.id, 'email', JAN_2017_MONTH_START, JAN_2017_MONTH_END) _assert_monthly_billing_totals( monthly_billing[0].monthly_totals[0], { "billing_units": 1, "rate_multiplier": 1, "international": False, "rate": 0.0, "total_cost": 0 }) _assert_monthly_billing(monthly_billing[1], sample_template.service.id, 'sms', JAN_2017_MONTH_START, JAN_2017_MONTH_END) _assert_monthly_billing_totals( monthly_billing[1].monthly_totals[0], { "billing_units": 1, "rate_multiplier": 2, "international": False, "rate": 0.0158, "total_cost": 1 * 2 * 0.0158 }) _assert_monthly_billing(monthly_billing[2], sample_template.service.id, 'letter', JAN_2017_MONTH_START, JAN_2017_MONTH_END) _assert_monthly_billing_totals( monthly_billing[2].monthly_totals[0], { "billing_units": 1, "rate_multiplier": 1, "international": False, "rate": 0.31, "total_cost": 1 * 0.31 })
def test_get_yearly_usage_by_month_returns_correctly(client, sample_template): create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE) create_notification(template=sample_template, created_at=IN_MAY_2016, billable_units=1, rate_multiplier=2, status='delivered') create_notification(template=sample_template, created_at=IN_JUN_2016, billable_units=2, rate_multiplier=3, status='delivered') create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_MAY_2016) create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_JUN_2016) response = client.get('/service/{}/billing/monthly-usage?year=2016'.format( sample_template.service.id), headers=[create_authorization_header()]) assert response.status_code == 200 resp_json = json.loads(response.get_data(as_text=True)) _assert_dict_equals( resp_json[0], { 'billing_units': 2, 'month': 'May', 'notification_type': SMS_TYPE, 'rate': 0.12 }) _assert_dict_equals( resp_json[1], { 'billing_units': 0, 'month': 'May', 'notification_type': LETTER_TYPE, 'rate': 0 }) _assert_dict_equals( resp_json[2], { 'billing_units': 6, 'month': 'June', 'notification_type': SMS_TYPE, 'rate': 0.12 }) _assert_dict_equals( resp_json[3], { 'billing_units': 0, 'month': 'June', 'notification_type': LETTER_TYPE, 'rate': 0 })
def test_get_rate_for_sms_and_email(notify_db_session): non_letter_rates = [ create_rate(datetime(2017, 12, 1), 0.15, SMS_TYPE), create_rate(datetime(2017, 12, 1), 0, EMAIL_TYPE) ] rate = get_rate(non_letter_rates, [], SMS_TYPE, date(2018, 1, 1)) assert rate == Decimal(0.15) rate = get_rate(non_letter_rates, [], EMAIL_TYPE, date(2018, 1, 1)) assert rate == Decimal(0)
def test_get_monthly_billing_data_where_start_date_before_rate_returns_empty( sample_template): now = datetime.utcnow() create_rate(now, 0.014, SMS_TYPE) results = get_billing_data_for_month(service_id=sample_template.service_id, start_date=now - timedelta(days=2), end_date=now - timedelta(days=1), notification_type=SMS_TYPE) assert not results
def test_get_yearly_usage_by_month_returns_empty_list_if_no_usage( client, sample_template): create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE) response = client.get('/service/{}/billing/monthly-usage?year=2016'.format( sample_template.service.id), headers=[create_authorization_header()]) assert response.status_code == 200 results = json.loads(response.get_data(as_text=True)) assert results == []
def test_get_yearly_usage_by_monthly_from_ft_billing_populates_deltas(client, notify_db_session): service = create_service() sms_template = create_template(service=service, template_type="sms") create_rate(start_date=datetime.utcnow() - timedelta(days=1), value=0.158, notification_type='sms') create_notification(template=sms_template, status='delivered') assert FactBilling.query.count() == 0 response = client.get('service/{}/billing/ft-monthly-usage?year=2018'.format(service.id), headers=[('Content-Type', 'application/json'), create_authorization_header()]) assert response.status_code == 200 assert len(json.loads(response.get_data(as_text=True))) == 1 fact_billing = FactBilling.query.all() assert len(fact_billing) == 1 assert fact_billing[0].notification_type == 'sms'
def test_update_monthly_billing_overwrites_old_totals(sample_template): create_rate(APR_2016_MONTH_START, 0.123, SMS_TYPE) create_notification(template=sample_template, created_at=APR_2016_MONTH_START, billable_units=1, status='delivered') create_or_update_monthly_billing(sample_template.service_id, APR_2016_MONTH_END) first_update = get_monthly_billing_by_notification_type( sample_template.service_id, APR_2016_MONTH_START, SMS_TYPE) _assert_monthly_billing(first_update, sample_template.service.id, 'sms', APR_2016_MONTH_START, APR_2016_MONTH_END) _assert_monthly_billing_totals( first_update.monthly_totals[0], { "billing_units": 1, "rate_multiplier": 1, "international": False, "rate": 0.123, "total_cost": 0.123 }) first_updated_at = first_update.updated_at with freeze_time(APR_2016_MONTH_START + timedelta(days=3)): create_notification(template=sample_template, billable_units=2, status='delivered') create_or_update_monthly_billing(sample_template.service_id, APR_2016_MONTH_END) second_update = get_monthly_billing_by_notification_type( sample_template.service_id, APR_2016_MONTH_START, SMS_TYPE) _assert_monthly_billing_totals( second_update.monthly_totals[0], { "billing_units": 3, "rate_multiplier": 1, "international": False, "rate": 0.123, "total_cost": 0.369 }) assert second_update.updated_at == APR_2016_MONTH_START + timedelta(days=3) assert first_updated_at != second_update.updated_at
def test_get_rate(notify_db_session): create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=1.2, notification_type='email') create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=2.2, notification_type='sms') create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=3.3, notification_type='email') create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), rate=0.66, post_class='first') create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), rate=0.3, post_class='second') non_letter_rates, letter_rates = get_rates_for_billing() rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type='sms', date=date(2017, 6, 1)) letter_rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type='letter', crown=True, letter_page_count=1, date=date(2017, 6, 1)) assert rate == 2.2 assert letter_rate == Decimal('0.3')
def test_get_rates_for_billing(notify_db_session): create_rate(start_date=datetime.utcnow(), value=12, notification_type='email') create_rate(start_date=datetime.utcnow(), value=22, notification_type='sms') create_rate(start_date=datetime.utcnow(), value=33, notification_type='email') create_letter_rate(start_date=datetime.utcnow(), rate=0.66, post_class='first') create_letter_rate(start_date=datetime.utcnow(), rate=0.33, post_class='second') create_letter_rate(start_date=datetime.utcnow(), rate=0.84, post_class='europe') create_letter_rate(start_date=datetime.utcnow(), rate=0.84, post_class='rest-of-world') non_letter_rates, letter_rates = get_rates_for_billing() assert len(non_letter_rates) == 3 assert len(letter_rates) == 4
def test_get_rate(notify_db_session): create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=1.2, notification_type="email") create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=2.2, notification_type="sms") create_rate(start_date=datetime(2017, 5, 30, 23, 0), value=3.3, notification_type="email") create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), rate=0.66, post_class="first") create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), rate=0.3, post_class="second") non_letter_rates, letter_rates = get_rates_for_billing() rate = get_rate( non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type="sms", date=date(2017, 6, 1), ) letter_rate = get_rate( non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type="letter", crown=True, letter_page_count=1, date=date(2017, 6, 1), ) assert rate == 2.2 assert letter_rate == Decimal("0.3")
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_get_rates_for_billing(notify_db_session): create_rate(start_date=datetime.utcnow(), value=12, notification_type="email") create_rate(start_date=datetime.utcnow(), value=22, notification_type="sms") create_rate(start_date=datetime.utcnow(), value=33, notification_type="email") create_letter_rate(start_date=datetime.utcnow(), rate=0.66, post_class="first") create_letter_rate(start_date=datetime.utcnow(), rate=0.33, post_class="second") non_letter_rates, letter_rates = get_rates_for_billing() assert len(non_letter_rates) == 3 assert len(letter_rates) == 2
def test_get_rates_for_billing(notify_db_session): create_rate(start_date=datetime.utcnow(), value=12, notification_type='email') create_rate(start_date=datetime.utcnow(), value=22, notification_type='sms') create_rate(start_date=datetime.utcnow(), value=33, notification_type='email') non_letter_rates, letter_rates = get_rates_for_billing() assert len(non_letter_rates) == 3 assert len(letter_rates) == 10
def test_get_rate(notify_db_session): create_rate(start_date=datetime.utcnow(), value=1.2, notification_type='email') create_rate(start_date=datetime.utcnow(), value=2.2, notification_type='sms') create_rate(start_date=datetime.utcnow(), value=3.3, notification_type='email') non_letter_rates, letter_rates = get_rates_for_billing() rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type='sms', date=datetime.utcnow()) letter_rate = get_rate(non_letter_rates=non_letter_rates, letter_rates=letter_rates, notification_type='letter', crown=True, letter_page_count=1, date=datetime.utcnow()) assert rate == 2.2 assert letter_rate == Decimal('0.3')
def test_add_monthly_billing_for_multiple_months_populate_correctly( sample_template): create_rate(start_date=FEB_2016_MONTH_START - timedelta(days=1), value=0.12, notification_type=SMS_TYPE) create_notification(template=sample_template, created_at=FEB_2016_MONTH_START, billable_units=1, rate_multiplier=2, status='delivered') create_notification(template=sample_template, created_at=MAR_2016_MONTH_START, billable_units=2, rate_multiplier=3, status='delivered') create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=FEB_2016_MONTH_START) create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=MAR_2016_MONTH_START) monthly_billing = MonthlyBilling.query.order_by( MonthlyBilling.notification_type, MonthlyBilling.start_date).all() assert len(monthly_billing) == 6 _assert_monthly_billing(monthly_billing[0], sample_template.service.id, 'email', FEB_2016_MONTH_START, FEB_2016_MONTH_END) assert monthly_billing[0].monthly_totals == [] _assert_monthly_billing(monthly_billing[1], sample_template.service.id, 'email', MAR_2016_MONTH_START, MAR_2016_MONTH_END) assert monthly_billing[1].monthly_totals == [] _assert_monthly_billing(monthly_billing[2], sample_template.service.id, 'sms', FEB_2016_MONTH_START, FEB_2016_MONTH_END) _assert_monthly_billing_totals( monthly_billing[2].monthly_totals[0], { "billing_units": 1, "rate_multiplier": 2, "international": False, "rate": 0.12, "total_cost": 0.24 }) _assert_monthly_billing(monthly_billing[3], sample_template.service.id, 'sms', MAR_2016_MONTH_START, MAR_2016_MONTH_END) _assert_monthly_billing_totals( monthly_billing[3].monthly_totals[0], { "billing_units": 2, "rate_multiplier": 3, "international": False, "rate": 0.12, "total_cost": 0.72 }) _assert_monthly_billing(monthly_billing[4], sample_template.service.id, 'letter', FEB_2016_MONTH_START, FEB_2016_MONTH_END) assert monthly_billing[4].monthly_totals == [] _assert_monthly_billing(monthly_billing[5], sample_template.service.id, 'letter', MAR_2016_MONTH_START, MAR_2016_MONTH_END) assert monthly_billing[5].monthly_totals == []
def test_get_yearly_billing_summary_returns_correct_breakdown( client, sample_template): create_rate(start_date=IN_MAY_2016 - timedelta(days=1), value=0.12, notification_type=SMS_TYPE) create_notification(template=sample_template, created_at=IN_MAY_2016, billable_units=1, rate_multiplier=2, status='delivered') create_notification(template=sample_template, created_at=IN_JUN_2016, billable_units=2, rate_multiplier=3, status='delivered') create_letter_rate(crown=False, start_date=IN_MAY_2016, end_date=IN_JUN_2016) create_letter_rate(crown=False, start_date=IN_JUN_2016, rate=0.41) letter_template = create_template(service=sample_template.service, template_type=LETTER_TYPE) create_notification(template=letter_template, created_at=IN_MAY_2016, status='delivered', billable_units=1) create_notification(template=letter_template, created_at=IN_JUN_2016, status='delivered', billable_units=1) create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_MAY_2016) create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=IN_JUN_2016) response = client.get( '/service/{}/billing/yearly-usage-summary?year=2016'.format( sample_template.service.id), headers=[create_authorization_header()]) assert response.status_code == 200 resp_json = json.loads(response.get_data(as_text=True)) assert len(resp_json) == 3 _assert_dict_equals( resp_json[0], { 'notification_type': SMS_TYPE, 'billing_units': 8, 'rate': 0.0165, 'letter_total': 0 }) _assert_dict_equals( resp_json[1], { 'notification_type': EMAIL_TYPE, 'billing_units': 0, 'rate': 0, 'letter_total': 0 }) _assert_dict_equals( resp_json[2], { 'notification_type': LETTER_TYPE, 'billing_units': 4, 'rate': 0, 'letter_total': 1.38 })