From f516a428430d482b1e46fa3f741a81d4c2dc86ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Nowaczyk?= <61748900+x-Milosz@users.noreply.github.com> Date: Sat, 18 Jan 2025 01:49:10 +0100 Subject: [PATCH] Feature: accessibility table added --- .github/workflows/tests.yml | 2 +- Makefile | 2 +- goodmap/core.py | 29 +++++- goodmap/templates/map.html | 1 + .../basic-test/accesibility_table_test.cy.js | 24 +++++ tests/e2e_tests/e2e_test_config.yml | 1 + tests/unit_tests/test_core.py | 98 ++++++++++++++++++- 7 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 tests/e2e_tests/cypress/e2e/basic-test/accesibility_table_test.cy.js diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6f627bbf..c9465854 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -43,7 +43,7 @@ jobs: - name: Install cypress working-directory: "./tests/e2e_tests" run: | - npm install cypress + npm install cypress && npm install cypress-browser-permissions - name: Run e2e tests run: | PYTHONUNBUFFERED=1;FLASK_ENV=development;FLASK_APP="goodmap.goodmap:create_app(config_path='./tests/e2e_tests/e2e_test_config.yml')" poetry run flask run & diff --git a/Makefile b/Makefile index 3531c93d..32827530 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ unit-tests-no-coverage: poetry run python -m pytest -m "skip_coverage" e2e-tests: - cd tests/e2e_tests && node_modules/cypress/bin/cypress run --browser chromium --spec cypress/e2e/basic-test/* + cd tests/e2e_tests && node_modules/cypress/bin/cypress run --browser chromium --spec "cypress/e2e/basic-test/*.cy.js" e2e-stress-tests-generate-data: python tests/e2e_tests/cypress/support/generate_stress_test_data.py diff --git a/goodmap/core.py b/goodmap/core.py index bacc4774..73e2bdb3 100644 --- a/goodmap/core.py +++ b/goodmap/core.py @@ -1,3 +1,5 @@ +from typing import Any, Dict, List + # TODO move filtering to db site @@ -10,10 +12,35 @@ def does_fulfill_requirement(entry, requirements): return all(matches) +def sort_by_distance(data: List[Dict[str, Any]], query_params: Dict[str, List[str]]): + try: + if "lat" in query_params and "lon" in query_params: + lat = float(query_params["lat"][0]) + lon = float(query_params["lon"][0]) + data.sort(key=lambda x: (x["position"][0] - lat) ** 2 + (x["position"][1] - lon) ** 2) + return data + return data + except (ValueError, KeyError, IndexError): + return data + + +def limit(data, query_params): + try: + if "limit" in query_params: + limit = int(query_params["limit"][0]) + data = data[:limit] + return data + return data + except (ValueError, KeyError, IndexError): + return data + + def get_queried_data(all_data, categories, query_params): requirements = [] for key in categories.keys(): requirements.append((key, query_params.get(key))) filtered_data = [x for x in all_data if does_fulfill_requirement(x, requirements)] - return filtered_data + final_data = sort_by_distance(filtered_data, query_params) + final_data = limit(final_data, query_params) + return final_data diff --git a/goodmap/templates/map.html b/goodmap/templates/map.html index b4c39e24..44326ad1 100644 --- a/goodmap/templates/map.html +++ b/goodmap/templates/map.html @@ -116,6 +116,7 @@ window.SHOW_SUGGEST_NEW_POINT_BUTTON = {{ feature_flags.SHOW_SUGGEST_NEW_POINT_BUTTON | default(false) | tojson }}; window.SHOW_SEARCH_BAR = {{ feature_flags.SHOW_SEARCH_BAR | default(false) | tojson }}; window.USE_LAZY_LOADING = {{ feature_flags.USE_LAZY_LOADING | default(false) | tojson }}; +window.SHOW_ACCESSIBILITY_TABLE = {{ feature_flags.SHOW_ACCESSIBILITY_TABLE | default(false) | tojson }}; {% endblock %} diff --git a/tests/e2e_tests/cypress/e2e/basic-test/accesibility_table_test.cy.js b/tests/e2e_tests/cypress/e2e/basic-test/accesibility_table_test.cy.js new file mode 100644 index 00000000..d28f76f3 --- /dev/null +++ b/tests/e2e_tests/cypress/e2e/basic-test/accesibility_table_test.cy.js @@ -0,0 +1,24 @@ +import { fakeLocation } from '../../utils/fake-location'; + +function rowElements() { + return cy.get('tr'); +} + +describe('Accessibility table test', () => { + beforeEach(() => { + const latitude = 51.10655; + const longitude = 17.0555; + cy.visit('/', fakeLocation(latitude, longitude)); + cy.get('button[id="listViewButton"]').click(); + }); + + it('should properly display places', () => { + rowElements() + // Header + 2 rows + .should('have.length', 3); + }); + + it("should 'Zwierzyniecka' be first row", () => { + rowElements().eq(1).find('td').should('contain', 'Zwierzyniecka'); + }); +}); diff --git a/tests/e2e_tests/e2e_test_config.yml b/tests/e2e_tests/e2e_test_config.yml index 187f0967..d9f4f91a 100644 --- a/tests/e2e_tests/e2e_test_config.yml +++ b/tests/e2e_tests/e2e_test_config.yml @@ -18,3 +18,4 @@ LANGUAGES: FEATURE_FLAGS: USE_LAZY_LOADING: False + SHOW_ACCESSIBILITY_TABLE: True diff --git a/tests/unit_tests/test_core.py b/tests/unit_tests/test_core.py index 4cef7b49..0a7999d0 100644 --- a/tests/unit_tests/test_core.py +++ b/tests/unit_tests/test_core.py @@ -1,4 +1,4 @@ -from goodmap.core import does_fulfill_requirement, get_queried_data +from goodmap.core import does_fulfill_requirement, get_queried_data, limit, sort_by_distance test_data = [ { @@ -79,3 +79,99 @@ def test_category_match_if_not_specified(): ] filtered_data = list(filter(lambda x: does_fulfill_requirement(x, requirements), test_data)) assert filtered_data == expected_data + + +def test_that_limit_works_properly(): + test_data_15_items = [ + { + "name": f"PCK_{i+1}", + "position": [51.1, 17.05], + "types": ["clothes"], + "gender": ["male"], + } + for i in range(15) + ] + expected_data = test_data_15_items[:10] + query_params = {"limit": ["10"]} + limited_data = limit(test_data_15_items, query_params) + assert limited_data == expected_data + + +def test_that_limit_does_not_change_data_if_limit_not_specified(): + expected_data = [ + { + "name": "LASSO", + "position": [51.113, 17.06], + "types": ["shoes"], + "gender": ["male", "female"], + }, + { + "name": "PCK", + "position": [51.1, 17.05], + "types": ["clothes"], + "gender": ["male"], + }, + ] + limit_returned_data = limit(test_data, {}) + assert limit_returned_data == expected_data + + +def test_that_sort_by_distance_works_as_intended(): + expected_data = [ + { + "name": "PCK", + "position": [51.1, 17.05], + "types": ["clothes"], + "gender": ["male"], + }, + { + "name": "LASSO", + "position": [51.113, 17.06], + "types": ["shoes"], + "gender": ["male", "female"], + }, + ] + + query_params = {"lat": ["51.1"], "lon": ["17.05"]} + sorted_data = sort_by_distance(test_data, query_params) + assert sorted_data == expected_data + + +def test_that_sort_by_distance_returns_data_when_query_params_are_corrupted(): + expected_data = [ + { + "name": "PCK", + "position": [51.1, 17.05], + "types": ["clothes"], + "gender": ["male"], + }, + { + "name": "LASSO", + "position": [51.113, 17.06], + "types": ["shoes"], + "gender": ["male", "female"], + }, + ] + query_params = {"lat": ["51c.o1ruppted"], "lon": ["17c.05ruptted"]} + sorted_data = sort_by_distance(test_data, query_params) + assert sorted_data == expected_data + + +def test_that_limit_returns_data_when_query_params_are_corrupted(): + expected_data = [ + { + "name": "PCK", + "position": [51.1, 17.05], + "types": ["clothes"], + "gender": ["male"], + }, + { + "name": "LASSO", + "position": [51.113, 17.06], + "types": ["shoes"], + "gender": ["male", "female"], + }, + ] + query_params = {"limit": ["1c0rupte0d"]} + limit_data = limit(test_data, query_params) + assert limit_data == expected_data