diff --git a/app/dao/date_util.py b/app/dao/date_util.py index 77a46c69e..1b795226c 100644 --- a/app/dao/date_util.py +++ b/app/dao/date_util.py @@ -63,8 +63,9 @@ def get_current_financial_year_start_year(): return financial_year_start -def which_financial_year(start_date): - if start_date <= get_april_fools(int(start_date.strftime('%Y'))): - return int(start_date.strftime('%Y')) - 1 +def get_financial_year_for_datetime(start_date): + year = int(start_date.strftime('%Y')) + if start_date < get_april_fools(year): + return year - 1 else: - return int(start_date.strftime('%Y')) + return year diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 40e4b5a57..07f8f15ae 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -9,7 +9,7 @@ from app import db from app.dao.date_util import ( get_april_fools as financial_year_start, get_financial_year, - which_financial_year + get_financial_year_for_datetime ) from app.models import ( @@ -33,7 +33,7 @@ from app.utils import get_london_midnight_in_utc def fetch_sms_free_allowance_remainder(start_date): # ASSUMPTION: AnnualBilling has been populated for year. - billing_year = which_financial_year(start_date) + billing_year = get_financial_year_for_datetime(start_date) print(billing_year) start_of_year = convert_utc_to_bst(financial_year_start(billing_year)) query = db.session.query( @@ -60,7 +60,7 @@ def fetch_sms_free_allowance_remainder(start_date): def fetch_sms_billing_for_all_services(start_date, end_date): # ASSUMPTION: AnnualBilling has been populated for year. - billing_year = which_financial_year(start_date) + billing_year = get_financial_year_for_datetime(start_date) free_allowance_remainder = fetch_sms_free_allowance_remainder(start_date).subquery() sms_billable_units = func.sum(FactBilling.billable_units * FactBilling.rate_multiplier) sms_remainder = func.coalesce(free_allowance_remainder.c.sms_remainder, AnnualBilling.free_sms_fragment_limit) diff --git a/tests/app/dao/test_date_utils.py b/tests/app/dao/test_date_utils.py index afa2b4897..b76f87466 100644 --- a/tests/app/dao/test_date_utils.py +++ b/tests/app/dao/test_date_utils.py @@ -2,7 +2,12 @@ from datetime import datetime import pytest -from app.dao.date_util import get_financial_year, get_april_fools, get_month_start_and_end_date_in_utc +from app.dao.date_util import ( + get_financial_year, + get_april_fools, + get_month_start_and_end_date_in_utc, + get_financial_year_for_datetime, +) def test_get_financial_year(): @@ -29,3 +34,13 @@ def test_get_month_start_and_end_date_in_utc(month, year, expected_start, expect result = get_month_start_and_end_date_in_utc(month_year) assert result[0] == expected_start assert result[1] == expected_end + + +@pytest.mark.parametrize("dt, fy", [ + (datetime(2018, 3, 31, 23, 0, 0), 2018), + (datetime(2019, 3, 31, 22, 59, 59), 2018), + (datetime(2019, 3, 31, 23, 0, 0), 2019), + (datetime(2020, 3, 31, 22, 59, 0), 2019), +]) +def test_get_financial_year_for_datetime(dt, fy): + assert get_financial_year_for_datetime(dt) == fy