mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 20:36:21 -05:00
Reworking map import to handle postgres data dumps.
This commit is contained in:
parent
be4c3a5021
commit
5178f72b66
11 changed files with 194 additions and 109 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -1,16 +1,18 @@
|
|||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from modules.map import models
|
||||
|
||||
|
||||
@pytest.mark.parametrize('size,expected', [
|
||||
(0, 0),
|
||||
(-1, 0),
|
||||
(17737381, 4475),
|
||||
(63434267, 16006),
|
||||
(87745484, 22141),
|
||||
(136372996, 34411),
|
||||
(116318111, 29351),
|
||||
@pytest.mark.parametrize('path,size,expected', [
|
||||
('file.osm.pbf', 0, 0),
|
||||
('file.osm.pbf', -1, 0),
|
||||
('file.osm.pbf', 17737381, 4475),
|
||||
('file.osm.pbf', 63434267, 16006),
|
||||
('file.osm.pbf', 87745484, 22141),
|
||||
('file.osm.pbf', 136372996, 34411),
|
||||
('file.osm.pbf', 116318111, 29351),
|
||||
])
|
||||
def test_seconds_to_import_rpi4(size, expected):
|
||||
assert models.seconds_to_import(size) == expected
|
||||
def test_seconds_to_import_rpi4(path, size, expected):
|
||||
assert models.seconds_to_import(Path(path), size) == expected
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue