rename which_financial_year to get_financial_year_for_datetime

also fix bug on the very second of rollover, march 31st 23:00:00, and
add tests
This commit is contained in:
Leo Hemsted
2019-08-28 14:27:08 +01:00
parent 2abcda47a3
commit b7e8f1baa2
3 changed files with 24 additions and 8 deletions

View File

@@ -63,8 +63,9 @@ def get_current_financial_year_start_year():
return financial_year_start return financial_year_start
def which_financial_year(start_date): def get_financial_year_for_datetime(start_date):
if start_date <= get_april_fools(int(start_date.strftime('%Y'))): year = int(start_date.strftime('%Y'))
return int(start_date.strftime('%Y')) - 1 if start_date < get_april_fools(year):
return year - 1
else: else:
return int(start_date.strftime('%Y')) return year

View File

@@ -9,7 +9,7 @@ from app import db
from app.dao.date_util import ( from app.dao.date_util import (
get_april_fools as financial_year_start, get_april_fools as financial_year_start,
get_financial_year, get_financial_year,
which_financial_year get_financial_year_for_datetime
) )
from app.models import ( 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): def fetch_sms_free_allowance_remainder(start_date):
# ASSUMPTION: AnnualBilling has been populated for year. # 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) print(billing_year)
start_of_year = convert_utc_to_bst(financial_year_start(billing_year)) start_of_year = convert_utc_to_bst(financial_year_start(billing_year))
query = db.session.query( 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): def fetch_sms_billing_for_all_services(start_date, end_date):
# ASSUMPTION: AnnualBilling has been populated for year. # 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() free_allowance_remainder = fetch_sms_free_allowance_remainder(start_date).subquery()
sms_billable_units = func.sum(FactBilling.billable_units * FactBilling.rate_multiplier) 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) sms_remainder = func.coalesce(free_allowance_remainder.c.sms_remainder, AnnualBilling.free_sms_fragment_limit)

View File

@@ -2,7 +2,12 @@ from datetime import datetime
import pytest 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(): 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) result = get_month_start_and_end_date_in_utc(month_year)
assert result[0] == expected_start assert result[0] == expected_start
assert result[1] == expected_end 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