From d08531121b6a1081e3fbdedb026d3b6c139ea042 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 16 Dec 2024 19:40:38 +0100 Subject: [PATCH] feat: parse files in parallel at import when multiple --- umap/static/umap/js/modules/data/layer.js | 10 ++++----- .../fixtures/test_upload_simple_marker.json | 19 +++++++++++++++++ umap/tests/integration/test_import.py | 21 ++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 umap/tests/fixtures/test_upload_simple_marker.json diff --git a/umap/static/umap/js/modules/data/layer.js b/umap/static/umap/js/modules/data/layer.js index 0c89e286..dde942b4 100644 --- a/umap/static/umap/js/modules/data/layer.js +++ b/umap/static/umap/js/modules/data/layer.js @@ -526,15 +526,13 @@ export class DataLayer extends ServerStored { } async importFromFiles(files, type) { - let all = [] + const toLoad = [] for (const file of files) { - const features = await this.importFromFile(file, type) - if (features) { - all = all.concat(features) - } + toLoad.push(this.importFromFile(file, type)) } + const features = await Promise.all(toLoad) return new Promise((resolve) => { - resolve(all) + resolve([].concat(...features)) }) } diff --git a/umap/tests/fixtures/test_upload_simple_marker.json b/umap/tests/fixtures/test_upload_simple_marker.json new file mode 100644 index 00000000..85f40c91 --- /dev/null +++ b/umap/tests/fixtures/test_upload_simple_marker.json @@ -0,0 +1,19 @@ +{ + "crs": null, + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "coordinates": [ + -1.09314, + 50.791718 + ], + "type": "Point" + }, + "properties": { + "name": "Portsmouth" + } + } + ] +} diff --git a/umap/tests/integration/test_import.py b/umap/tests/integration/test_import.py index ec242e6c..9cd42646 100644 --- a/umap/tests/integration/test_import.py +++ b/umap/tests/integration/test_import.py @@ -9,7 +9,6 @@ from playwright.sync_api import expect from umap.models import DataLayer -from ..base import mock_tiles from .helpers import save_and_get_json pytestmark = pytest.mark.django_db @@ -765,3 +764,23 @@ def test_import_georss_from_textarea(tilelayer, live_server, page): # A layer has been created expect(layers).to_have_count(1) expect(markers).to_have_count(1) + + +def test_import_from_multiple_files(live_server, page, tilelayer): + page.goto(f"{live_server.url}/map/new/") + page.get_by_title("Import data").click() + file_input = page.locator("input[type='file']") + with page.expect_file_chooser() as fc_info: + file_input.click() + file_chooser = fc_info.value + FIXTURES = Path(__file__).parent.parent / "fixtures" + paths = [ + FIXTURES / "test_upload_data.json", + FIXTURES / "test_upload_simple_marker.json", + ] + file_chooser.set_files(paths) + markers = page.locator(".leaflet-marker-icon") + expect(markers).to_have_count(0) + page.get_by_role("button", name="Import data", exact=True).click() + # Two in one file, one in the other + expect(markers).to_have_count(3)