From d530b79026e232740a19f58ca11772033beb2fd8 Mon Sep 17 00:00:00 2001 From: alexandru-m-g Date: Mon, 1 Jul 2024 19:19:53 +0300 Subject: [PATCH] HDX-9945 Fix for pagination when multi-valued filter is used This fixed pagination when filtering by 2 orgs or by 2 tags --- .../tests/test_dataviz_page_load.py | 1 - .../test_custom_org_controller.py | 2 +- .../test_pages/test_dataset_search_params.py | 2 +- .../controller_logic/search_logic.py | 2 +- .../ckanext/hdx_search/tests/conftest.py | 1 + .../tests/test_pages/test_pagination.py | 88 +++++++++++++++++++ .../ckanext/hdx_theme/tests/hdx_test_util.py | 4 +- 7 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 ckanext-hdx_search/ckanext/hdx_search/tests/conftest.py create mode 100644 ckanext-hdx_search/ckanext/hdx_search/tests/test_pages/test_pagination.py diff --git a/ckanext-hdx_dataviz/ckanext/hdx_dataviz/tests/test_dataviz_page_load.py b/ckanext-hdx_dataviz/ckanext/hdx_dataviz/tests/test_dataviz_page_load.py index decb54da89..8e32aa26c8 100644 --- a/ckanext-hdx_dataviz/ckanext/hdx_dataviz/tests/test_dataviz_page_load.py +++ b/ckanext-hdx_dataviz/ckanext/hdx_dataviz/tests/test_dataviz_page_load.py @@ -1,5 +1,4 @@ import pytest -import six import ckan.plugins.toolkit as tk diff --git a/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_controller/test_custom_org_controller.py b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_controller/test_custom_org_controller.py index 092fbd9256..fd8238c830 100644 --- a/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_controller/test_custom_org_controller.py +++ b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_controller/test_custom_org_controller.py @@ -140,7 +140,7 @@ def mock_get_top_line_items(): org_dict = _get_action('organization_update')(context, org_dict) data_access_cls.return_value.get_top_line_items.side_effect = mock_get_top_line_items - req_mock.args = {} + req_mock.args = mock.MagicMock() tk.g.user = sysadmin_user tk.g.userobj = None org_helper_c_mock.user = sysadmin_user diff --git a/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_pages/test_dataset_search_params.py b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_pages/test_dataset_search_params.py index 8b5f914037..3623364137 100644 --- a/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_pages/test_dataset_search_params.py +++ b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_pages/test_dataset_search_params.py @@ -46,7 +46,7 @@ def test_search_params(self): end_str = '' search_item = 'name="q"' - count = hdx_test_util.count_string_occurences(page, search_item, + count = hdx_test_util.count_string_occurrences(page, search_item, begin_str, end_str) assert count == 1, 'There should be exactly one input with name q in the form' diff --git a/ckanext-hdx_search/ckanext/hdx_search/controller_logic/search_logic.py b/ckanext-hdx_search/ckanext/hdx_search/controller_logic/search_logic.py index 12d5c50e56..4bb0cc8e36 100644 --- a/ckanext-hdx_search/ckanext/hdx_search/controller_logic/search_logic.py +++ b/ckanext-hdx_search/ckanext/hdx_search/controller_logic/search_logic.py @@ -407,7 +407,7 @@ def _page_number(self): def _params_nopage(self): params_to_skip = ['_show_filters'] # most search operations should reset the page counter: - return [(k, v) for k, v in request.args.items() + return [(k, v) for k, v in request.args.items(multi=True) if k != 'page' and k not in params_to_skip] def _set_search_url_params(self): diff --git a/ckanext-hdx_search/ckanext/hdx_search/tests/conftest.py b/ckanext-hdx_search/ckanext/hdx_search/tests/conftest.py new file mode 100644 index 0000000000..e2ee852355 --- /dev/null +++ b/ckanext-hdx_search/ckanext/hdx_search/tests/conftest.py @@ -0,0 +1 @@ +from ckanext.hdx_theme.tests.conftest import keep_db_tables_on_clean diff --git a/ckanext-hdx_search/ckanext/hdx_search/tests/test_pages/test_pagination.py b/ckanext-hdx_search/ckanext/hdx_search/tests/test_pages/test_pagination.py new file mode 100644 index 0000000000..aa10698555 --- /dev/null +++ b/ckanext-hdx_search/ckanext/hdx_search/tests/test_pages/test_pagination.py @@ -0,0 +1,88 @@ +import pytest +import ckan.tests.factories as factories + +import ckan.plugins.toolkit as tk +import ckan.model as model + +from ckan.types import Context +from ckanext.hdx_org_group.helpers.static_lists import ORGANIZATION_TYPE_LIST +from ckanext.hdx_theme.tests import hdx_test_util + +_url_for = tk.url_for +_get_action = tk.get_action + +STANDARD_USER = 'some_standard_user' +LOCATION_NAME = 'some_location' +ORG_NAME1 = 'some_org1' +ORG_NAME2 = 'some_org2' + + +def _get_dataset_dict(dataset_name, org_name): + return { + "package_creator": "test function", + "private": False, + "dataset_date": "[1960-01-01 TO 2012-12-31]", + "caveats": "These are the caveats", + "license_other": "TEST OTHER LICENSE", + "methodology": "This is a test methodology", + "dataset_source": "Test data", + "license_id": "hdx-other", + "name": dataset_name, + "notes": "This is a test dataset", + "title": "Test Dataset " + dataset_name, + "owner_org": org_name, + "groups": [{"name": LOCATION_NAME}], + "data_update_frequency": "30", + "maintainer": STANDARD_USER, + "resources": [ + { + 'url': 'http://test.ckan.test/test.csv', + 'resource_type': 'api', + 'url_type': 'api', + 'format': 'CSV', + 'name': 'data1.csv', + } + ], + } + + +@pytest.fixture() +def setup_data(): + factories.User(name=STANDARD_USER, email='some_standard_user@hdx.hdxtest.org') + group = factories.Group(name=LOCATION_NAME) + for org_name in [ORG_NAME1, ORG_NAME2]: + factories.Organization( + name=org_name, + title='ORG NAME FOR HDX_REL_URL', + users=[ + {'name': STANDARD_USER, 'capacity': 'editor'}, + ], + hdx_org_type=ORGANIZATION_TYPE_LIST[0][1], + org_url='https://hdx.hdxtest.org/' + ) + + context: Context = {'model': model, 'session': model.Session, 'user': STANDARD_USER} + _get_action('package_create')(context, _get_dataset_dict(dataset_name='dataset1', + org_name=ORG_NAME1)) + _get_action('package_create')(context, _get_dataset_dict(dataset_name='dataset2', + org_name=ORG_NAME2)) + + +@pytest.mark.usefixtures('keep_db_tables_on_clean', 'clean_db', 'clean_index', 'setup_data') +def test_pagination_2_valued_filter(app): + url = _url_for('hdx_dataset.search', organization=[ORG_NAME1, ORG_NAME2], ext_page_size=1) + response = app.get(url) + assert response.status_code == 200 + + page = response.body + + begin_str = '' + search_item1 = f'organization={ORG_NAME1}' + search_item2 = f'organization={ORG_NAME2}' + + count_org1 = hdx_test_util.count_string_occurrences(page, search_item1, begin_str, end_str) + assert count_org1 == 1 + + count_org2 = hdx_test_util.count_string_occurrences(page, search_item2, begin_str, end_str) + assert count_org2 == 1 diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/tests/hdx_test_util.py b/ckanext-hdx_theme/ckanext/hdx_theme/tests/hdx_test_util.py index ebcc0c4dff..a713daa1ef 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/tests/hdx_test_util.py +++ b/ckanext-hdx_theme/ckanext/hdx_theme/tests/hdx_test_util.py @@ -43,7 +43,7 @@ def are_strings_in_text(text, search_strings, # assert item not in section, item + ' exists in text section' -def count_string_occurences(text, search_item, +def count_string_occurrences(text, search_item, begin_str=None, end_str=None): start = 0 end = len(text) @@ -68,7 +68,7 @@ def test_string_checks(): end_str = '' search_item = 'name="q"' - count = count_string_occurences(page, search_item, begin_str, end_str) + count = count_string_occurrences(page, search_item, begin_str, end_str) assert count == 1, 'There should be exactly one input with name q in the form' page = 'lorep ipsum
/dataset lorep Test Dataset 1 name="q" ipsumlorep test_dataset_1
'