diff --git a/modules/map/lib.py b/modules/map/lib.py index fbb5b441..3614a22e 100644 --- a/modules/map/lib.py +++ b/modules/map/lib.py @@ -94,6 +94,11 @@ async def import_files(paths: List[str]): any_success = False try: if pbfs: + with get_db_session(commit=True) as session: + # Any previously imported PBFs are no longer imported. + for pbf_file in session.query(MapFile): + pbf_file.imported = False + success = False try: api_app.shared_ctx.map_importing.update(dict( @@ -115,8 +120,6 @@ async def import_files(paths: List[str]): for pbf_path in pbfs: pbf_file = get_or_create_map_file(pbf_path, session) pbf_file.imported = True - for pbf_file in session.query(MapFile): - pbf_file.imported = pbf_file.path in pbfs for path in dumps: # Import each dump individually. diff --git a/modules/map/test/test_api.py b/modules/map/test/test_api.py index 486f31ce..2bdee384 100644 --- a/modules/map/test/test_api.py +++ b/modules/map/test/test_api.py @@ -9,7 +9,9 @@ def assert_map_file_status(test_client, expected, status_code=HTTPStatus.OK): request, response = test_client.get('/api/map/files') assert response.status_code == status_code for pbf, expected_ in zip_longest(response.json['files'], expected): - assert pbf['imported'] == expected_['imported'] + imported = 'to be imported' if expected_['imported'] else 'to NOT be imported' + if not pbf['imported'] == expected_['imported']: + raise AssertionError(f'Expected {pbf["path"]} {imported}') assert pbf['path'].endswith(expected_['path']) assert response.json['pending'] is None @@ -32,21 +34,11 @@ def test_status_and_import(test_client, test_session, make_files_structure, mock ] assert_map_file_status(test_client, expected) - body = {'files': [str(pbf1), ], } - request, response = test_client.post('/api/map/import', content=json.dumps(body)) - assert response.status_code == HTTPStatus.NO_CONTENT - - expected = [ - {'imported': False, 'path': 'map/dump/dumpy.dump'}, - {'imported': False, 'path': 'map/pbf/other-country.osm.pbf'}, - {'imported': True, 'path': 'map/pbf/some-country.osm.pbf'}, - ] - assert_map_file_status(test_client, expected) - body = {'files': [str(pbf1), str(pbf2)]} request, response = test_client.post('/api/map/import', content=json.dumps(body)) assert response.status_code == HTTPStatus.NO_CONTENT + # Can import multiple pbf files. expected = [ {'imported': False, 'path': 'map/dump/dumpy.dump'}, {'imported': True, 'path': 'map/pbf/other-country.osm.pbf'}, @@ -54,13 +46,26 @@ def test_status_and_import(test_client, test_session, make_files_structure, mock ] assert_map_file_status(test_client, expected) + body = {'files': [str(pbf1), ], } + request, response = test_client.post('/api/map/import', content=json.dumps(body)) + assert response.status_code == HTTPStatus.NO_CONTENT + + # Importing one pbf resets the import statuses. + expected = [ + {'imported': False, 'path': 'map/dump/dumpy.dump'}, + {'imported': False, 'path': 'map/pbf/other-country.osm.pbf'}, + {'imported': True, 'path': 'map/pbf/some-country.osm.pbf'}, + ] + assert_map_file_status(test_client, expected) + body = {'files': [str(dump1), ]} request, response = test_client.post('/api/map/import', content=json.dumps(body)) assert response.status_code == HTTPStatus.NO_CONTENT + # Importing a dump also resets statuses. expected = [ {'imported': True, 'path': 'map/dump/dumpy.dump'}, - {'imported': True, 'path': 'map/pbf/other-country.osm.pbf'}, + {'imported': False, 'path': 'map/pbf/other-country.osm.pbf'}, {'imported': True, 'path': 'map/pbf/some-country.osm.pbf'}, ] assert_map_file_status(test_client, expected) diff --git a/scripts/import_map.sh b/scripts/import_map.sh index 26422f98..306d3eb1 100755 --- a/scripts/import_map.sh +++ b/scripts/import_map.sh @@ -107,5 +107,6 @@ fi # Clear map tile cache only after successful import. yes | /bin/bash /opt/wrolpi/scripts/clear_map_cache.sh -# Use curl to fetch the first few layers of map tiles so the map is ready to use. -bash /opt/wrolpi/wrolpi/scripts/initialize_map_tiles.sh +# Use wget to fetch the first few layers of map tiles so the map is ready to use. +# Failure does not mean the import failed. +bash /opt/wrolpi/wrolpi/scripts/initialize_map_tiles.sh || :