Skip to content

Commit

Permalink
Merge pull request #311 from nationalarchives/AYR-884/date-validation…
Browse files Browse the repository at this point in the history
…-bug-fix

AYR-884/date validation issues fix
  • Loading branch information
pgandhizaizi authored Mar 28, 2024
2 parents 5336db6 + 956ef70 commit 2ca1095
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 43 deletions.
15 changes: 7 additions & 8 deletions app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,13 @@ def browse_consignment(_id: uuid.UUID):
date_error_fields = []

if len(request.args) > 0:
if len([k for k, v in request.args.items() if "date" in k]):
(
date_validation_errors,
from_date,
to_date,
date_filters,
date_error_fields,
) = validate_date_filters(request.args, browse_consignment=True)
(
date_validation_errors,
from_date,
to_date,
date_filters,
date_error_fields,
) = validate_date_filters(request.args, browse_consignment=True)

filters = build_browse_consignment_filters(request.args, from_date, to_date)
sorting_orders = build_sorting_orders(request.args)
Expand Down
74 changes: 45 additions & 29 deletions app/main/util/date_filters_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ def validate_date_filters(args, browse_consignment=False):
from_date = to_date = None
date_filters = {}

if browse_consignment:
date_validation_errors = _check_consignment_date_filters(args)

if "date_filter_field" in date_validation_errors:
date_filters = {
"from_day": args.get("date_from_day"),
"from_month": args.get("date_from_month"),
"from_year": args.get("date_from_year"),
"to_day": args.get("date_to_day"),
"to_month": args.get("date_to_month"),
"to_year": args.get("date_to_year"),
}
return (
date_validation_errors,
from_date,
to_date,
date_filters,
[],
)

# date validation
(
from_day,
from_month,
Expand All @@ -18,35 +39,6 @@ def validate_date_filters(args, browse_consignment=False):
error_field,
) = validate_dates(args)

date_filter_field = args.get("date_filter_field", "")

if browse_consignment and date_filter_field not in [
"date_last_modified",
"opening_date",
]:
error_field = []
date_validation_errors.clear()
date_validation_errors["date_filter_field"] = (
"Select either ‘Date of record’ or ‘Record opening date’"
)

date_filters = {
"from_day": from_day,
"from_month": from_month,
"from_year": from_year,
"to_day": to_day,
"to_month": to_month,
"to_year": to_year,
}

return (
date_validation_errors,
from_date,
to_date,
date_filters,
error_field,
)

if not (
date_validation_errors["date_from"] or date_validation_errors["date_to"]
):
Expand Down Expand Up @@ -90,6 +82,30 @@ def validate_date_filters(args, browse_consignment=False):
return date_validation_errors, from_date, to_date, date_filters, error_field


def _check_consignment_date_filters(args):
date_validation_errors = {}
date_filter_field = args.get("date_filter_field", "")

date_fields = {k: v for k, v in args.items() if "date" in k and v}
if "date_filter_field" in date_fields:
date_fields.pop("date_filter_field", None)

if date_filter_field not in [
"date_last_modified",
"opening_date",
]:
if date_fields:
date_validation_errors["date_filter_field"] = (
"Select either ‘Date of record’ or ‘Record opening date’"
)
else:
if not date_fields:
date_validation_errors["date_filter_field"] = (
"Please enter value(s) in ‘Date from’ or ‘Date to’ field"
)
return date_validation_errors


def _format_date_elements(day, month, year):
day_var = (
str(day).rjust(2, "0") if len(str(day)) == 1 else day if day else 0 * 2
Expand Down
39 changes: 33 additions & 6 deletions app/tests/test_date_filters_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ def test_validate_date_filters_without_browse_consignment(
@pytest.mark.parametrize(
"request_args, expected_results",
[
(
{
"date_filter_field": "date_last_modified",
"date_from_day": "",
"date_from_month": "",
"date_from_year": "",
"date_to_day": "",
"date_to_month": "",
"date_to_year": "",
},
(
{
"date_filter_field": "Please enter value(s) in ‘Date from’ or ‘Date to’ field",
},
None,
None,
{
"from_day": "",
"from_month": "",
"from_year": "",
"to_day": "",
"to_month": "",
"to_year": "",
},
[],
),
),
(
{
"date_filter_field": "",
Expand Down Expand Up @@ -241,12 +268,12 @@ def test_validate_date_filters_without_browse_consignment(
None,
None,
{
"from_day": 1,
"from_month": 8,
"from_year": 2023,
"to_day": 31,
"to_month": 8,
"to_year": 2023,
"from_day": "01",
"from_month": "08",
"from_year": "2023",
"to_day": "31",
"to_month": "08",
"to_year": "2023",
},
[],
),
Expand Down
46 changes: 46 additions & 0 deletions e2e_tests/test_browse_consignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,52 @@ def test_browse_consignment_filter_functionality_with_record_opening_date_filter
verify_header_row(header_rows)
assert rows == expected_rows

def test_browse_consignment_date_validation_with_record_status_selection_change(
self, standard_user_page: Page
):
standard_user_page.goto(f"{self.route_url}/{self.consignment_id}")
standard_user_page.get_by_text("Open only").click()
standard_user_page.get_by_role(
"button", name="Apply", exact=True
).click()

assert not standard_user_page.get_by_text(
"Select either ‘Date of record’ or ‘Record opening date’"
).is_visible()
assert not standard_user_page.get_by_text(
"Please enter value(s) in ‘Date from’ or ‘Date to’ field"
).is_visible()

def test_browse_consignment_date_validation_with_empty_date_fields_and_date_filter_selected(
self, standard_user_page: Page
):
standard_user_page.goto(f"{self.route_url}/{self.consignment_id}")
standard_user_page.locator("label").filter(
has_text="Date of record"
).click()
standard_user_page.get_by_role(
"button", name="Apply", exact=True
).click()

assert standard_user_page.get_by_text(
"Please enter value(s) in ‘Date from’ or ‘Date to’ field"
).is_visible()

def test_browse_consignment_date_validation_with_date_fields_and_no_date_filter_selected(
self, standard_user_page: Page
):
standard_user_page.goto(f"{self.route_url}/{self.consignment_id}")
standard_user_page.locator("#date_from_day").fill("01")
standard_user_page.locator("#date_from_month").fill("01")
standard_user_page.locator("#date_from_year").fill("2019")
standard_user_page.get_by_role(
"button", name="Apply", exact=True
).click()

assert standard_user_page.get_by_text(
"Select either ‘Date of record’ or ‘Record opening date’"
).is_visible()

def test_browse_consignment_date_filter_validation_date_from(
self, standard_user_page: Page
):
Expand Down

0 comments on commit 2ca1095

Please sign in to comment.