From d5f67c3ea48bd1f3b7be01caaf4fece7bdcebc6c Mon Sep 17 00:00:00 2001 From: Pavan Gandhi Date: Thu, 28 Mar 2024 14:36:25 +0000 Subject: [PATCH 1/2] fixed issues with date filter validation for browse consignment view, updated logic in date_filters_validator.py, removed unwanted line from routes.py, updated test cases in test_date_filters_validator.py and e2e_tests/test_browse_consignment.py --- app/main/routes.py | 15 ++++---- app/main/util/date_filters_validator.py | 42 ++++++++++++++++------ app/tests/test_date_filters_validator.py | 27 ++++++++++++++ e2e_tests/test_browse_consignment.py | 46 ++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 19 deletions(-) diff --git a/app/main/routes.py b/app/main/routes.py index 3df69f34..3443ffdc 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -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) diff --git a/app/main/util/date_filters_validator.py b/app/main/util/date_filters_validator.py index ce6fe0e0..7d55bc6f 100644 --- a/app/main/util/date_filters_validator.py +++ b/app/main/util/date_filters_validator.py @@ -18,18 +18,12 @@ 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’" + if browse_consignment: + date_validation_errors = _check_consignment_date_filters( + args, date_validation_errors ) + if "date_filter_field" in date_validation_errors: date_filters = { "from_day": from_day, "from_month": from_month, @@ -38,7 +32,7 @@ def validate_date_filters(args, browse_consignment=False): "to_month": to_month, "to_year": to_year, } - + error_field = [] return ( date_validation_errors, from_date, @@ -90,6 +84,32 @@ 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", "") + + # create dictionary for date fields only + date_fields = {k: v for k, v in args.items() if "date" in k} + 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 len([k for k, v in date_fields.items() if len(v) > 0]): + date_validation_errors.clear() + date_validation_errors["date_filter_field"] = ( + "Select either ‘Date of record’ or ‘Record opening date’" + ) + else: + if not len([k for k, v in date_fields.items() if len(v) > 0]): + date_validation_errors.clear() + 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 diff --git a/app/tests/test_date_filters_validator.py b/app/tests/test_date_filters_validator.py index 04b78a4c..bb131326 100644 --- a/app/tests/test_date_filters_validator.py +++ b/app/tests/test_date_filters_validator.py @@ -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": "", diff --git a/e2e_tests/test_browse_consignment.py b/e2e_tests/test_browse_consignment.py index 29a2bdd7..8488fb73 100644 --- a/e2e_tests/test_browse_consignment.py +++ b/e2e_tests/test_browse_consignment.py @@ -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 ): From 956ef70f48ba00a31438f704b6fae3f5727922a1 Mon Sep 17 00:00:00 2001 From: Pavan Gandhi Date: Thu, 28 Mar 2024 16:04:49 +0000 Subject: [PATCH 2/2] updated changes in date_filters_validator.py based on PR review, updated test case in test_date_filters_validator.py --- app/main/util/date_filters_validator.py | 56 +++++++++++------------- app/tests/test_date_filters_validator.py | 12 ++--- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/app/main/util/date_filters_validator.py b/app/main/util/date_filters_validator.py index 7d55bc6f..5f839485 100644 --- a/app/main/util/date_filters_validator.py +++ b/app/main/util/date_filters_validator.py @@ -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, @@ -18,29 +39,6 @@ def validate_date_filters(args, browse_consignment=False): error_field, ) = validate_dates(args) - if browse_consignment: - date_validation_errors = _check_consignment_date_filters( - args, date_validation_errors - ) - - if "date_filter_field" in date_validation_errors: - 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, - } - error_field = [] - 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"] ): @@ -84,11 +82,11 @@ 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): +def _check_consignment_date_filters(args): + date_validation_errors = {} date_filter_field = args.get("date_filter_field", "") - # create dictionary for date fields only - date_fields = {k: v for k, v in args.items() if "date" in k} + 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) @@ -96,14 +94,12 @@ def _check_consignment_date_filters(args, date_validation_errors): "date_last_modified", "opening_date", ]: - if len([k for k, v in date_fields.items() if len(v) > 0]): - date_validation_errors.clear() + if date_fields: date_validation_errors["date_filter_field"] = ( "Select either ‘Date of record’ or ‘Record opening date’" ) else: - if not len([k for k, v in date_fields.items() if len(v) > 0]): - date_validation_errors.clear() + if not date_fields: date_validation_errors["date_filter_field"] = ( "Please enter value(s) in ‘Date from’ or ‘Date to’ field" ) diff --git a/app/tests/test_date_filters_validator.py b/app/tests/test_date_filters_validator.py index bb131326..ad996bec 100644 --- a/app/tests/test_date_filters_validator.py +++ b/app/tests/test_date_filters_validator.py @@ -268,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", }, [], ),