def test_get_rate_filters_letters_by_post_class(notify_db_session, letter_post_class, expected_rate): create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), sheet_count=2, rate=0.61, post_class='first') create_letter_rate(start_date=datetime(2017, 5, 30, 23, 0), sheet_count=2, rate=0.35, post_class='second') non_letter_rates, letter_rates = get_rates_for_billing() rate = get_rate(non_letter_rates, letter_rates, "letter", datetime(2018, 10, 1), True, 2, letter_post_class) assert rate == Decimal(expected_rate)
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_rate_chooses_right_rate_depending_on_date(notify_db_session, date, expected_rate): create_letter_rate(start_date=datetime(2016, 1, 1, 0, 0), sheet_count=2, rate=0.33, post_class='second') create_letter_rate(start_date=datetime(2018, 9, 30, 23, 0), sheet_count=2, rate=0.35, post_class='second') non_letter_rates, letter_rates = get_rates_for_billing() rate = get_rate(non_letter_rates, letter_rates, "letter", date, True, 2, "second") assert rate == Decimal(expected_rate)
def test_billing_letter_data_per_month_query(notify_db_session): create_letter_rate() service = create_service() template = create_template(service=service, template_type='letter') create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21), status='delivered') create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21), status='delivered') create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21), status='delivered') results = billing_letter_data_per_month_query( service_id=service.id, start_date=datetime(2017, 2, 1), end_date=datetime(2017, 2, 28)) assert len(results) == 1 assert results[0].rate == 0.31 assert results[0].billing_units == 3
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_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_rate_for_letter_latest(notify_db_session): # letter rates should be passed into the get_rate function as a tuple of start_date, crown, sheet_count, # rate and post_class new = create_letter_rate(datetime(2017, 12, 1), crown=True, sheet_count=1, rate=0.33, post_class='second') old = create_letter_rate(datetime(2016, 12, 1), crown=True, sheet_count=1, rate=0.30, post_class='second') letter_rates = [new, old] rate = get_rate([], letter_rates, LETTER_TYPE, date(2018, 1, 1), True, 1) assert rate == Decimal('0.33')
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_rate_for_letter_latest_if_crown_is_none(notify_db_session): # letter rates should be passed into the get_rate function as a tuple of start_date, crown, sheet_count, # rate and post_class crown = create_letter_rate(datetime(2017, 12, 1), crown=True, sheet_count=1, rate=0.33, post_class='second') non_crown = create_letter_rate(datetime(2017, 12, 1), crown=False, sheet_count=1, rate=0.35, post_class='second') letter_rates = [crown, non_crown] rate = get_rate([], letter_rates, LETTER_TYPE, date(2018, 1, 1), crown=None, letter_page_count=1) assert rate == Decimal('0.33')
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_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 })