merge from main

This commit is contained in:
Kenneth Kehl
2023-06-19 07:56:10 -07:00
20 changed files with 138 additions and 107 deletions

View File

@@ -5,7 +5,7 @@ from freezegun import freeze_time
from app.billing.rest import update_free_sms_fragment_limit_data
from app.dao.annual_billing_dao import dao_get_free_sms_fragment_limit_for_year
from app.dao.date_util import get_current_financial_year_start_year
from app.dao.date_util import get_current_calendar_year_start_year
from tests.app.db import (
create_annual_billing,
create_ft_billing,
@@ -32,7 +32,7 @@ def test_create_update_free_sms_fragment_limit_invalid_schema(admin_request, sam
def test_create_free_sms_fragment_limit_current_year_updates_future_years(admin_request, sample_service):
current_year = get_current_financial_year_start_year()
current_year = get_current_calendar_year_start_year()
future_billing = create_annual_billing(sample_service.id, 1, current_year + 1)
admin_request.post(
@@ -54,7 +54,7 @@ def test_create_or_update_free_sms_fragment_limit_past_year_doenst_update_other_
sample_service,
update_existing
):
current_year = get_current_financial_year_start_year()
current_year = get_current_calendar_year_start_year()
create_annual_billing(sample_service.id, 1, current_year)
if update_existing:
create_annual_billing(sample_service.id, 1, current_year - 1)
@@ -71,7 +71,7 @@ def test_create_or_update_free_sms_fragment_limit_past_year_doenst_update_other_
def test_create_free_sms_fragment_limit_updates_existing_year(admin_request, sample_service):
current_year = get_current_financial_year_start_year()
current_year = get_current_calendar_year_start_year()
annual_billing = create_annual_billing(sample_service.id, 1, current_year)
admin_request.post(
@@ -112,7 +112,7 @@ def test_get_free_sms_fragment_limit_current_year_creates_new_row_if_annual_bill
def test_update_free_sms_fragment_limit_data(client, sample_service):
current_year = get_current_financial_year_start_year()
current_year = get_current_calendar_year_start_year()
create_annual_billing(sample_service.id, free_sms_fragment_limit=250000, financial_year_start=current_year - 1)
update_free_sms_fragment_limit_data(sample_service.id, 9999, current_year)
@@ -128,7 +128,7 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(admin_request, notify_db_se
sms_template = create_template(service=service, template_type="sms")
email_template = create_template(service=service, template_type="email")
for dt in (date(2016, 4, 28), date(2016, 11, 10), date(2017, 2, 26)):
for dt in (date(2016, 1, 28), date(2016, 8, 10), date(2016, 12, 26)):
create_ft_billing(local_date=dt, template=sms_template, rate=0.0162)
create_ft_billing(local_date=dt, template=email_template, billable_unit=0, rate=0)
@@ -145,7 +145,7 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(admin_request, notify_db_se
sms_row = next(x for x in json_response if x['notification_type'] == 'sms')
assert sms_row["month"] == "April"
assert sms_row["month"] == "January"
assert sms_row["notification_type"] == "sms"
assert sms_row["chargeable_units"] == 1
assert sms_row["notifications_sent"] == 1
@@ -185,7 +185,7 @@ def test_get_yearly_billing_usage_summary_from_ft_billing(admin_request, notify_
sms_template = create_template(service=service, template_type="sms")
email_template = create_template(service=service, template_type="email")
for dt in (date(2016, 4, 28), date(2016, 11, 10), date(2017, 2, 26)):
for dt in (date(2016, 1, 28), date(2016, 8, 10), date(2016, 12, 26)):
create_ft_billing(local_date=dt, template=sms_template, rate=0.0162)
create_ft_billing(local_date=dt, template=email_template, billable_unit=0, rate=0)

View File

@@ -7,14 +7,14 @@ from app.dao.annual_billing_dao import (
dao_update_annual_billing_for_future_years,
set_default_free_allowance_for_service,
)
from app.dao.date_util import get_current_financial_year_start_year
from app.dao.date_util import get_current_calendar_year_start_year
from app.models import AnnualBilling
from tests.app.db import create_annual_billing, create_service
def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service):
new_limit = 9999
year = get_current_financial_year_start_year()
year = get_current_calendar_year_start_year()
dao_create_or_update_annual_billing_for_year(sample_service.id, new_limit, year)
new_free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, year)
@@ -31,7 +31,7 @@ def test_create_annual_billing(sample_service):
def test_dao_update_annual_billing_for_future_years(notify_db_session, sample_service):
current_year = get_current_financial_year_start_year()
current_year = get_current_calendar_year_start_year()
limits = [1, 2, 3, 4]
create_annual_billing(sample_service.id, limits[0], current_year - 1)
create_annual_billing(sample_service.id, limits[2], current_year + 1)
@@ -79,8 +79,8 @@ def test_set_default_free_allowance_for_service_using_correct_year(sample_servic
mock_dao.assert_called_once_with(
sample_service.id,
250000,
2020
150000,
2021
)

View File

@@ -3,23 +3,23 @@ from datetime import date, datetime
import pytest
from app.dao.date_util import (
get_april_fools,
get_financial_year,
get_financial_year_for_datetime,
get_calendar_year,
get_calendar_year_for_datetime,
get_month_start_and_end_date_in_utc,
get_new_years,
)
def test_get_financial_year():
start, end = get_financial_year(2000)
assert str(start) == '2000-04-01 00:00:00'
assert str(end) == '2001-03-31 23:59:59.999999'
def test_get_calendar_year():
start, end = get_calendar_year(2000)
assert str(start) == '2000-01-01 00:00:00'
assert str(end) == '2000-12-31 23:59:59.999999'
def test_get_april_fools():
april_fools = get_april_fools(2016)
assert str(april_fools) == '2016-04-01 00:00:00'
assert april_fools.tzinfo is None
def test_get_new_years():
new_years = get_new_years(2016)
assert str(new_years) == '2016-01-01 00:00:00'
assert new_years.tzinfo is None
@pytest.mark.parametrize("month, year, expected_start, expected_end", [
@@ -38,9 +38,9 @@ def test_get_month_start_and_end_date_in_utc(month, year, expected_start, expect
@pytest.mark.parametrize("dt, fy", [
(datetime(2018, 4, 1, 1, 0, 0), 2018),
(datetime(2019, 3, 31, 23, 59, 59), 2018),
(date(2019, 3, 31), 2018),
(datetime(2019, 3, 31, 23, 59, 59), 2019),
(date(2019, 3, 31), 2019),
(date(2019, 4, 2), 2019),
])
def test_get_financial_year_for_datetime(dt, fy):
assert get_financial_year_for_datetime(dt) == fy
def test_get_calendar_year_for_datetime(dt, fy):
assert get_calendar_year_for_datetime(dt) == fy

View File

@@ -43,13 +43,13 @@ def set_up_yearly_data():
# use different rates for adjacent financial years to make sure the query
# doesn't accidentally bleed over into them
for dt in (date(2016, 3, 31), date(2017, 4, 1)):
for dt in (date(2015, 12, 31), date(2017, 1, 1)):
create_ft_billing(local_date=dt, template=sms_template, rate=0.163)
create_ft_billing(local_date=dt, template=email_template, rate=0, billable_unit=0)
# a selection of dates that represent the extreme ends of the financial year
# and some arbitrary dates in between
for dt in (date(2016, 4, 1), date(2016, 4, 29), date(2017, 2, 6), date(2017, 3, 31)):
for dt in (date(2016, 1, 1), date(2016, 1, 31), date(2016, 12, 6), date(2016, 12, 31)):
create_ft_billing(local_date=dt, template=sms_template, rate=0.162)
create_ft_billing(local_date=dt, template=email_template, rate=0, billable_unit=0)
@@ -295,9 +295,10 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
create_annual_billing(service_id=service.id, free_sms_fragment_limit=1, financial_year_start=2016)
results = fetch_monthly_billing_for_year(service.id, 2016)
assert len(results) == 6 # 3 billed months for each type
assert len(results) == 4 # 3 billed months for each type
print(f"RESULTS {results}")
assert str(results[0].month) == "2016-04-01"
assert str(results[0].month) == "2016-01-01"
assert results[0].notification_type == 'email'
assert results[0].notifications_sent == 2
assert results[0].chargeable_units == 0
@@ -306,7 +307,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[0].free_allowance_used == 0
assert results[0].charged_units == 0
assert str(results[1].month) == "2016-04-01"
assert str(results[1].month) == "2016-01-01"
assert results[1].notification_type == 'sms'
assert results[1].notifications_sent == 2
assert results[1].chargeable_units == 2
@@ -316,8 +317,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
assert results[1].free_allowance_used == 1
assert results[1].charged_units == 1
assert str(results[2].month) == "2017-02-01"
assert str(results[5].month) == "2017-03-01"
assert str(results[2].month) == "2016-12-01"
def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
@@ -394,8 +394,8 @@ def test_fetch_billing_totals_for_year(notify_db_session):
def test_fetch_billing_totals_for_year_uses_current_annual_billing(notify_db_session):
service = set_up_yearly_data()
create_annual_billing(service_id=service.id, free_sms_fragment_limit=400, financial_year_start=2015)
create_annual_billing(service_id=service.id, free_sms_fragment_limit=0, financial_year_start=2016)
create_annual_billing(service_id=service.id, free_sms_fragment_limit=400, financial_year_start=2016)
create_annual_billing(service_id=service.id, free_sms_fragment_limit=0, financial_year_start=2017)
result = next(
result for result in
@@ -404,7 +404,10 @@ def test_fetch_billing_totals_for_year_uses_current_annual_billing(notify_db_ses
)
assert result.chargeable_units == 4
assert result.cost > 0
# No charge for 2016 because we have free sms fragments.
# There would be a charge for 2017,
# but we are only billing for 2016 so cost is zero
assert result.cost == 0
def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
@@ -741,9 +744,9 @@ def test_fetch_usage_year_for_organisation_only_queries_present_year(notify_db_s
results = fetch_usage_year_for_organisation(organisation_id=org.id, year=last_year)
assert len(results) == 1
assert results[str(service_1.id)]['sms_billable_units'] == 4
assert results[str(service_1.id)]['chargeable_billable_sms'] == 4
assert results[str(service_1.id)]['sms_cost'] == 4.0
assert results[str(service_1.id)]['sms_billable_units'] == 2
assert results[str(service_1.id)]['chargeable_billable_sms'] == 2
assert results[str(service_1.id)]['sms_cost'] == 2.0
@freeze_time('2020-02-27 13:30')
@@ -762,10 +765,10 @@ def test_fetch_usage_year_for_organisation_only_returns_data_for_live_services(n
notifications_sent=100)
create_ft_billing(local_date=datetime.utcnow().date(), template=trial_sms_template, billable_unit=200, rate=0.0158,
notifications_sent=100)
create_annual_billing(service_id=live_service.id, free_sms_fragment_limit=0, financial_year_start=2019)
create_annual_billing(service_id=trial_service.id, free_sms_fragment_limit=0, financial_year_start=2019)
create_annual_billing(service_id=live_service.id, free_sms_fragment_limit=0, financial_year_start=2020)
create_annual_billing(service_id=trial_service.id, free_sms_fragment_limit=0, financial_year_start=2020)
results = fetch_usage_year_for_organisation(organisation_id=org.id, year=2019)
results = fetch_usage_year_for_organisation(organisation_id=org.id, year=2020)
assert len(results) == 1
assert results[str(live_service.id)]['sms_billable_units'] == 19

View File

@@ -787,7 +787,7 @@ def test_get_organisation_users_returns_users_for_organisation(admin_request, sa
assert response['data'][0]['id'] == str(first.id)
@freeze_time('2020-02-24 13:30')
@freeze_time('2019-12-24 13:30')
def test_get_organisation_services_usage(admin_request, notify_db_session):
org = create_organisation(name='Organisation without live services')
service = create_service()

View File

@@ -83,17 +83,15 @@ def test_get_platform_stats_with_real_query(admin_request, notify_db_session):
@pytest.mark.parametrize('start_date, end_date',
[('2019-04-01', '2019-06-30'),
('2019-08-01', '2019-09-30'),
('2019-01-01', '2019-03-31'),
('2019-12-01', '2020-02-28')])
('2019-01-01', '2019-03-31')])
def test_validate_date_range_is_within_a_financial_year(start_date, end_date):
validate_date_range_is_within_a_financial_year(start_date, end_date)
@pytest.mark.parametrize('start_date, end_date',
[('2019-04-01', '2020-06-30'),
('2019-01-01', '2019-04-30'),
('2019-12-01', '2020-04-30'),
('2019-03-31', '2019-04-01')])
('2018-01-01', '2019-04-30'),
('2019-12-01', '2020-04-30')])
def test_validate_date_range_is_within_a_financial_year_raises(start_date, end_date):
with pytest.raises(expected_exception=InvalidRequest) as e:
validate_date_range_is_within_a_financial_year(start_date, end_date)

View File

@@ -249,17 +249,16 @@ def test_get_monthly_notification_stats_ignores_test_keys(admin_request, sample_
def test_get_monthly_notification_stats_checks_dates(admin_request, sample_service):
t = create_template(sample_service)
create_ft_notification_status(datetime(2016, 3, 31), template=t, notification_status='created')
# create_ft_notification_status(datetime(2016, 3, 31), template=t, notification_status='created')
create_ft_notification_status(datetime(2016, 4, 2), template=t, notification_status='sending')
create_ft_notification_status(datetime(2017, 3, 31), template=t, notification_status='delivered')
create_ft_notification_status(datetime(2017, 4, 11), template=t, notification_status='permanent-failure')
response = admin_request.get('service.get_monthly_notification_stats', service_id=sample_service.id, year=2016)
assert '2016-03' not in response['data']
assert '2016-04' in response['data']
assert '2017-04' not in response['data']
assert response['data']['2016-04']['sms'] == {'sending': 1}
assert response['data']['2017-03']['sms'] == {'delivered': 1}
assert response['data']['2016-04']['sms'] == {'sending': 1}
def test_get_monthly_notification_stats_only_gets_for_one_service(admin_request, notify_db_session):

View File

@@ -1,16 +1,16 @@
from freezegun import freeze_time
from app.dao.date_util import get_current_financial_year_start_year
from app.dao.date_util import get_current_calendar_year_start_year
# see get_financial_year for conversion of financial years.
@freeze_time("2017-03-31 23:59:59.999999")
def test_get_current_financial_year_start_year_before_march():
current_fy = get_current_financial_year_start_year()
assert current_fy == 2016
def test_get_current_calendar_year_start_year_before_march():
current_fy = get_current_calendar_year_start_year()
assert current_fy == 2017
@freeze_time("2017-04-01 04:00:00.000000")
def test_get_current_financial_year_start_year_after_april():
current_fy = get_current_financial_year_start_year()
def test_get_current_calendar_year_start_year_after_april():
current_fy = get_current_calendar_year_start_year()
assert current_fy == 2017