2022-03-08 13:26:03 -07:00
|
|
|
import json
|
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
from itertools import zip_longest
|
|
|
|
|
|
|
|
|
|
import mock
|
2025-10-29 22:16:33 -06:00
|
|
|
import pytest
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
|
2025-10-29 22:16:33 -06:00
|
|
|
async def assert_map_file_status(async_client, expected, status_code=HTTPStatus.OK):
|
|
|
|
|
request, response = await async_client.get('/api/map/files')
|
2022-10-27 20:22:11 -06:00
|
|
|
assert response.status_code == status_code
|
|
|
|
|
for pbf, expected_ in zip_longest(response.json['files'], expected):
|
2025-03-13 15:52:40 -06:00
|
|
|
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}')
|
2022-10-27 20:22:11 -06:00
|
|
|
assert pbf['path'].endswith(expected_['path'])
|
|
|
|
|
assert response.json['pending'] is None
|
|
|
|
|
|
|
|
|
|
|
2025-10-29 22:16:33 -06:00
|
|
|
@pytest.mark.asyncio
|
2022-03-08 13:26:03 -07:00
|
|
|
@mock.patch('modules.map.lib.is_pbf_file', lambda i: True)
|
2022-03-31 07:31:15 -06:00
|
|
|
@mock.patch('modules.map.lib.is_dump_file', lambda i: True)
|
2025-10-29 22:16:33 -06:00
|
|
|
async def test_status_and_import(async_client, test_session, make_files_structure, mock_run_command):
|
2022-03-08 13:26:03 -07:00
|
|
|
"""PBF files can be imported, and the status of the import can be monitored."""
|
2022-10-27 20:22:11 -06:00
|
|
|
pbf1, pbf2, dump1 = make_files_structure([
|
|
|
|
|
'map/pbf/some-country.osm.pbf',
|
|
|
|
|
'map/pbf/other-country.osm.pbf',
|
|
|
|
|
'map/dump/dumpy.dump',
|
|
|
|
|
])
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
# The PBF files can be found, but have not yet been imported.
|
|
|
|
|
expected = [
|
2022-03-31 07:31:15 -06:00
|
|
|
{'imported': False, 'path': 'map/dump/dumpy.dump'},
|
2022-03-08 13:26:03 -07:00
|
|
|
{'imported': False, 'path': 'map/pbf/other-country.osm.pbf'},
|
|
|
|
|
{'imported': False, 'path': 'map/pbf/some-country.osm.pbf'},
|
|
|
|
|
]
|
2025-10-29 22:16:33 -06:00
|
|
|
await assert_map_file_status(async_client, expected)
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2025-03-13 15:52:40 -06:00
|
|
|
body = {'files': [str(pbf1), str(pbf2)]}
|
2025-10-29 22:16:33 -06:00
|
|
|
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
|
2025-01-26 12:44:16 -07:00
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2025-03-13 15:52:40 -06:00
|
|
|
# Can import multiple pbf files.
|
2022-03-08 13:26:03 -07:00
|
|
|
expected = [
|
2022-03-31 07:31:15 -06:00
|
|
|
{'imported': False, 'path': 'map/dump/dumpy.dump'},
|
2025-03-13 15:52:40 -06:00
|
|
|
{'imported': True, 'path': 'map/pbf/other-country.osm.pbf'},
|
2022-03-08 13:26:03 -07:00
|
|
|
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
|
|
|
|
|
]
|
2025-10-29 22:16:33 -06:00
|
|
|
await assert_map_file_status(async_client, expected)
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2025-03-13 15:52:40 -06:00
|
|
|
body = {'files': [str(pbf1), ], }
|
2025-10-29 22:16:33 -06:00
|
|
|
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
|
2025-01-26 12:44:16 -07:00
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2025-03-13 15:52:40 -06:00
|
|
|
# Importing one pbf resets the import statuses.
|
2022-03-08 13:26:03 -07:00
|
|
|
expected = [
|
2022-03-31 07:31:15 -06:00
|
|
|
{'imported': False, 'path': 'map/dump/dumpy.dump'},
|
2025-03-13 15:52:40 -06:00
|
|
|
{'imported': False, 'path': 'map/pbf/other-country.osm.pbf'},
|
2022-03-31 07:31:15 -06:00
|
|
|
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
|
|
|
|
|
]
|
2025-10-29 22:16:33 -06:00
|
|
|
await assert_map_file_status(async_client, expected)
|
2022-03-31 07:31:15 -06:00
|
|
|
|
2025-01-26 12:44:16 -07:00
|
|
|
body = {'files': [str(dump1), ]}
|
2025-10-29 22:16:33 -06:00
|
|
|
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
|
2025-01-26 12:44:16 -07:00
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-03-31 07:31:15 -06:00
|
|
|
|
2025-03-13 15:52:40 -06:00
|
|
|
# Importing a dump also resets statuses.
|
2022-03-31 07:31:15 -06:00
|
|
|
expected = [
|
|
|
|
|
{'imported': True, 'path': 'map/dump/dumpy.dump'},
|
2025-03-13 15:52:40 -06:00
|
|
|
{'imported': False, 'path': 'map/pbf/other-country.osm.pbf'},
|
2022-03-08 13:26:03 -07:00
|
|
|
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
|
|
|
|
|
]
|
2025-10-29 22:16:33 -06:00
|
|
|
await assert_map_file_status(async_client, expected)
|
2022-10-27 20:22:11 -06:00
|
|
|
|
|
|
|
|
|
2025-10-29 22:16:33 -06:00
|
|
|
@pytest.mark.asyncio
|
2022-10-27 20:22:11 -06:00
|
|
|
@mock.patch('modules.map.lib.is_pbf_file', lambda i: True)
|
|
|
|
|
@mock.patch('modules.map.lib.is_dump_file', lambda i: True)
|
2025-10-29 22:16:33 -06:00
|
|
|
async def test_multiple_import(async_client, test_session, test_directory, make_files_structure, mock_run_command):
|
2022-10-27 20:22:11 -06:00
|
|
|
"""Multiple PBFs can be imported. Importing a second time overwrites the previous imports."""
|
|
|
|
|
pbf1, pbf2, pbf3, dump = make_files_structure([
|
|
|
|
|
'map/pbf/country1.osm.pbf',
|
|
|
|
|
'map/pbf/country2.osm.pbf',
|
|
|
|
|
'map/pbf/country3.osm.pbf',
|
|
|
|
|
'map/dumpy.dump',
|
|
|
|
|
])
|
|
|
|
|
|
2025-01-26 12:44:16 -07:00
|
|
|
body = {'files': [str(pbf2), str(pbf1)]}
|
2025-10-29 22:16:33 -06:00
|
|
|
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
|
2025-01-26 12:44:16 -07:00
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-10-27 20:22:11 -06:00
|
|
|
|
|
|
|
|
# Two PBFs can be merged and imported.
|
|
|
|
|
expected = [
|
|
|
|
|
{'imported': False, 'path': 'map/dumpy.dump'},
|
|
|
|
|
{'imported': True, 'path': 'map/pbf/country1.osm.pbf'},
|
|
|
|
|
{'imported': True, 'path': 'map/pbf/country2.osm.pbf'},
|
|
|
|
|
{'imported': False, 'path': 'map/pbf/country3.osm.pbf'},
|
|
|
|
|
]
|
2025-10-29 22:16:33 -06:00
|
|
|
await assert_map_file_status(async_client, expected)
|
2022-10-27 20:22:11 -06:00
|
|
|
|
2025-01-26 12:44:16 -07:00
|
|
|
body = {'files': [str(pbf3), ]}
|
2025-10-29 22:16:33 -06:00
|
|
|
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
|
2025-01-26 12:44:16 -07:00
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-10-27 20:22:11 -06:00
|
|
|
|
|
|
|
|
# New imports overwrite old imports.
|
|
|
|
|
expected = [
|
|
|
|
|
{'imported': False, 'path': 'map/dumpy.dump'},
|
|
|
|
|
{'imported': False, 'path': 'map/pbf/country1.osm.pbf'},
|
|
|
|
|
{'imported': False, 'path': 'map/pbf/country2.osm.pbf'},
|
|
|
|
|
{'imported': True, 'path': 'map/pbf/country3.osm.pbf'},
|
|
|
|
|
]
|
2025-10-29 22:16:33 -06:00
|
|
|
await assert_map_file_status(async_client, expected)
|
2022-10-27 20:22:11 -06:00
|
|
|
|
2025-01-26 12:44:16 -07:00
|
|
|
# Can't mix PBF and dump.
|
|
|
|
|
body = {'files': [str(dump), ]}
|
2025-10-29 22:16:33 -06:00
|
|
|
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
|
2025-01-26 12:44:16 -07:00
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-10-27 20:22:11 -06:00
|
|
|
|
|
|
|
|
# Dump does not overwrite.
|
|
|
|
|
expected = [
|
|
|
|
|
{'imported': True, 'path': 'map/dumpy.dump'},
|
|
|
|
|
{'imported': False, 'path': 'map/pbf/country1.osm.pbf'},
|
|
|
|
|
{'imported': False, 'path': 'map/pbf/country2.osm.pbf'},
|
|
|
|
|
{'imported': True, 'path': 'map/pbf/country3.osm.pbf'},
|
|
|
|
|
]
|
2025-10-29 22:16:33 -06:00
|
|
|
await assert_map_file_status(async_client, expected)
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
|
2025-10-29 22:16:33 -06:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_empty_import(async_client, test_session, test_directory):
|
2022-03-08 13:26:03 -07:00
|
|
|
"""Some files must be requested."""
|
2022-10-27 20:22:11 -06:00
|
|
|
body = {'files': []}
|
2025-10-29 22:16:33 -06:00
|
|
|
request, response = await async_client.post('/api/map/import', content=json.dumps(body))
|
2022-10-27 20:22:11 -06:00
|
|
|
assert response.status_code == HTTPStatus.BAD_REQUEST
|
2024-05-01 17:42:26 -06:00
|
|
|
assert 'validate' in response.json['message']
|