mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
simplify validate_date_range_is_within_a_financial_year
we can use the get_financial_year_for_datetime function to massively simplify this
This commit is contained in:
@@ -2,7 +2,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
from flask import Blueprint, jsonify, request
|
from flask import Blueprint, jsonify, request
|
||||||
|
|
||||||
from app.dao.date_util import get_financial_year
|
from app.dao.date_util import get_financial_year_for_datetime
|
||||||
from app.dao.fact_billing_dao import (
|
from app.dao.fact_billing_dao import (
|
||||||
fetch_sms_billing_for_all_services, fetch_letter_costs_for_all_services,
|
fetch_sms_billing_for_all_services, fetch_letter_costs_for_all_services,
|
||||||
fetch_letter_line_items_for_all_services
|
fetch_letter_line_items_for_all_services
|
||||||
@@ -12,6 +12,7 @@ from app.errors import register_errors, InvalidRequest
|
|||||||
from app.platform_stats.platform_stats_schema import platform_stats_request
|
from app.platform_stats.platform_stats_schema import platform_stats_request
|
||||||
from app.service.statistics import format_admin_stats
|
from app.service.statistics import format_admin_stats
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
|
from app.utils import get_london_midnight_in_utc
|
||||||
|
|
||||||
platform_stats_blueprint = Blueprint('platform_stats', __name__)
|
platform_stats_blueprint = Blueprint('platform_stats', __name__)
|
||||||
|
|
||||||
@@ -42,15 +43,11 @@ def validate_date_range_is_within_a_financial_year(start_date, end_date):
|
|||||||
raise InvalidRequest(message="Input must be a date in the format: YYYY-MM-DD", status_code=400)
|
raise InvalidRequest(message="Input must be a date in the format: YYYY-MM-DD", status_code=400)
|
||||||
if end_date < start_date:
|
if end_date < start_date:
|
||||||
raise InvalidRequest(message="Start date must be before end date", status_code=400)
|
raise InvalidRequest(message="Start date must be before end date", status_code=400)
|
||||||
if 4 <= int(start_date.strftime("%m")) <= 12:
|
|
||||||
year_start, year_end = get_financial_year(year=int(start_date.strftime("%Y")))
|
start_fy = get_financial_year_for_datetime(get_london_midnight_in_utc(start_date))
|
||||||
else:
|
end_fy = get_financial_year_for_datetime(get_london_midnight_in_utc(end_date))
|
||||||
year_start, year_end = get_financial_year(year=int(start_date.strftime("%Y")) - 1)
|
|
||||||
year_start = year_start.date()
|
if start_fy != end_fy:
|
||||||
year_end = year_end.date()
|
|
||||||
if year_start <= start_date <= year_end and year_start <= end_date <= year_end:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
raise InvalidRequest(message="Date must be in a single financial year.", status_code=400)
|
raise InvalidRequest(message="Date must be in a single financial year.", status_code=400)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -85,8 +85,8 @@ def test_get_platform_stats_with_real_query(admin_request, notify_db_session):
|
|||||||
('2019-08-01', '2019-09-30'),
|
('2019-08-01', '2019-09-30'),
|
||||||
('2019-01-01', '2019-03-31'),
|
('2019-01-01', '2019-03-31'),
|
||||||
('2019-12-01', '2020-02-28')])
|
('2019-12-01', '2020-02-28')])
|
||||||
def test_validate_date_range_is_within_a_financial_year_returns_true(start_date, end_date):
|
def test_validate_date_range_is_within_a_financial_year(start_date, end_date):
|
||||||
assert 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',
|
@pytest.mark.parametrize('start_date, end_date',
|
||||||
@@ -94,7 +94,7 @@ def test_validate_date_range_is_within_a_financial_year_returns_true(start_date,
|
|||||||
('2019-01-01', '2019-04-30'),
|
('2019-01-01', '2019-04-30'),
|
||||||
('2019-12-01', '2020-04-30'),
|
('2019-12-01', '2020-04-30'),
|
||||||
('2019-03-31', '2019-04-01')])
|
('2019-03-31', '2019-04-01')])
|
||||||
def test_validate_date_range_is_within_a_financial_year_returns_false(start_date, end_date):
|
def test_validate_date_range_is_within_a_financial_year_raises(start_date, end_date):
|
||||||
with pytest.raises(expected_exception=InvalidRequest) as e:
|
with pytest.raises(expected_exception=InvalidRequest) as e:
|
||||||
validate_date_range_is_within_a_financial_year(start_date, end_date)
|
validate_date_range_is_within_a_financial_year(start_date, end_date)
|
||||||
assert e.message == 'Date must be in a single financial year.'
|
assert e.message == 'Date must be in a single financial year.'
|
||||||
|
|||||||
Reference in New Issue
Block a user