Reworking map import to handle postgres data dumps.

This commit is contained in:
Roland 2022-03-31 07:31:15 -06:00
parent be4c3a5021
commit 5178f72b66
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
11 changed files with 194 additions and 109 deletions

View file

@ -6,54 +6,77 @@ import mock
@mock.patch('modules.map.lib.is_pbf_file', lambda i: True)
def test_pbf_status_and_import(test_client, test_session, test_directory):
@mock.patch('modules.map.lib.is_dump_file', lambda i: True)
def test_status_and_import(test_client, test_session, test_directory):
"""PBF files can be imported, and the status of the import can be monitored."""
(test_directory / 'map/pbf').mkdir(parents=True)
(test_directory / 'map/dump').mkdir()
pbf1 = test_directory / 'map/pbf/some-country.osm.pbf'
pbf2 = test_directory / 'map/pbf/other-country.osm.pbf'
pbf1.touch()
pbf2.touch()
dump1 = test_directory / 'map/dump/dumpy.dump'
dump1.touch()
def check_status(response_json, expected_):
for pbf, expected_ in zip_longest(response_json['pbfs'], expected_):
for pbf, expected_ in zip_longest(response_json['files'], expected_):
assert pbf['imported'] == expected_['imported']
assert pbf['path'].endswith(expected_['path'])
assert response.json['importing'] is None
# The PBF files can be found, but have not yet been imported.
request, response = test_client.get('/api/map/pbf')
request, response = test_client.get('/api/map/files')
assert response.status_code == HTTPStatus.OK
expected = [
{'imported': False, 'path': 'map/dump/dumpy.dump'},
{'imported': False, 'path': 'map/pbf/other-country.osm.pbf'},
{'imported': False, 'path': 'map/pbf/some-country.osm.pbf'},
]
check_status(response.json, expected)
with mock.patch('modules.map.lib.import_pbf') as mock_import_pbf:
body = {'pbfs': [str(pbf1), ]}
with mock.patch('modules.map.lib.import_file') as mock_import_file:
body = {'files': [str(pbf1), ], }
request, response = test_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.NO_CONTENT
# Only one import is requested.
mock_import_pbf.assert_called_once()
# Only one PBF import is requested.
mock_import_file.assert_called_once()
request, response = test_client.get('/api/map/pbf')
request, response = test_client.get('/api/map/files')
assert response.status_code == HTTPStatus.OK
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'},
]
check_status(response.json, expected)
with mock.patch('modules.map.lib.import_pbf') as mock_import_pbf:
body = {'pbfs': [str(pbf1), str(pbf2)]}
with mock.patch('modules.map.lib.import_file') as mock_import_file:
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
# Both imports are requested, but only one is performed.
mock_import_pbf.assert_called_once()
# Both PBF imports are requested, but only one is performed.
mock_import_file.assert_called_once()
request, response = test_client.get('/api/map/pbf')
request, response = test_client.get('/api/map/files')
assert response.status_code == HTTPStatus.OK
expected = [
{'imported': False, 'path': 'map/dump/dumpy.dump'},
{'imported': True, 'path': 'map/pbf/other-country.osm.pbf'},
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
]
check_status(response.json, expected)
with mock.patch('modules.map.lib.import_file') as mock_import_file:
body = {'files': [str(dump1), ]}
request, response = test_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.NO_CONTENT
# Dump is imported.
mock_import_file.assert_called_once()
request, response = test_client.get('/api/map/files')
assert response.status_code == HTTPStatus.OK
expected = [
{'imported': True, 'path': 'map/dump/dumpy.dump'},
{'imported': True, 'path': 'map/pbf/other-country.osm.pbf'},
{'imported': True, 'path': 'map/pbf/some-country.osm.pbf'},
]
@ -62,8 +85,18 @@ def test_pbf_status_and_import(test_client, test_session, test_directory):
def test_empty_import(test_client, test_session, test_directory):
"""Some files must be requested."""
with mock.patch('modules.map.lib.import_pbf') as mock_import_pbf:
with mock.patch('modules.map.lib.import_file') as mock_import_file:
body = {'pbfs': []}
request, response = test_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.BAD_REQUEST
mock_import_pbf.assert_not_called()
mock_import_file.assert_not_called()
body = {'dumps': []}
request, response = test_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.BAD_REQUEST
mock_import_file.assert_not_called()
body = {'pbfs': [], 'dumps': []}
request, response = test_client.post('/api/map/import', content=json.dumps(body))
assert response.status_code == HTTPStatus.BAD_REQUEST
mock_import_file.assert_not_called()