mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-26 02:58:13 +00:00
add simple tests for CLI parsing
This commit is contained in:
@@ -197,13 +197,12 @@ class SetupSpecialPhrases:
|
|||||||
help='Pull special phrases from the OSM wiki.')
|
help='Pull special phrases from the OSM wiki.')
|
||||||
group = parser.add_argument_group('Output arguments')
|
group = parser.add_argument_group('Output arguments')
|
||||||
group.add_argument('-o', '--output', default='-',
|
group.add_argument('-o', '--output', default='-',
|
||||||
type=argparse.FileType('w', encoding='UTF-8'),
|
|
||||||
help="""File to write the preprocessed phrases to.
|
help="""File to write the preprocessed phrases to.
|
||||||
If omitted, it will be written to stdout.""")
|
If omitted, it will be written to stdout.""")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def run(args):
|
def run(args):
|
||||||
if args.output.name != '<stdout>':
|
if args.output != '-':
|
||||||
raise NotImplementedError('Only output to stdout is currently implemented.')
|
raise NotImplementedError('Only output to stdout is currently implemented.')
|
||||||
return run_legacy_script('specialphrases.php', '--wiki-import', nominatim_env=args)
|
return run_legacy_script('specialphrases.php', '--wiki-import', nominatim_env=args)
|
||||||
|
|
||||||
|
|||||||
82
test/python/test_cli.py
Normal file
82
test/python/test_cli.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
"""
|
||||||
|
Tests for command line interface wrapper.
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import nominatim.cli
|
||||||
|
|
||||||
|
def call_nominatim(*args):
|
||||||
|
return nominatim.cli.nominatim(module_dir='build/module',
|
||||||
|
osm2pgsql_path='build/osm2pgsql/osm2pgsql',
|
||||||
|
phplib_dir='lib',
|
||||||
|
data_dir='.',
|
||||||
|
phpcgi_path='/usr/bin/php-cgi',
|
||||||
|
cli_args=args)
|
||||||
|
|
||||||
|
class MockParamCapture:
|
||||||
|
""" Mock that records the parameters with which a function was called
|
||||||
|
as well as the number of calls.
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
self.called = 0
|
||||||
|
self.return_value = 0
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
self.called += 1
|
||||||
|
self.last_args = args
|
||||||
|
self.last_kwargs = kwargs
|
||||||
|
return self.return_value
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_run_legacy(monkeypatch):
|
||||||
|
mock = MockParamCapture()
|
||||||
|
monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
|
||||||
|
return mock
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_run_api(monkeypatch):
|
||||||
|
mock = MockParamCapture()
|
||||||
|
monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
|
||||||
|
return mock
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_help(capsys):
|
||||||
|
""" Running nominatim tool without arguments prints help.
|
||||||
|
"""
|
||||||
|
assert 1 == call_nominatim()
|
||||||
|
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert captured.out.startswith('usage:')
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("command,script", [
|
||||||
|
(('import', '--continue', 'load-data'), 'setup'),
|
||||||
|
(('freeze',), 'setup'),
|
||||||
|
(('special-phrases',), 'specialphrases'),
|
||||||
|
(('replication',), 'update'),
|
||||||
|
(('add-data', '--tiger-data', 'tiger'), 'setup'),
|
||||||
|
(('add-data', '--file', 'foo.osm'), 'update'),
|
||||||
|
(('check-database',), 'check_import_finished'),
|
||||||
|
(('warm',), 'warm'),
|
||||||
|
(('export',), 'export')
|
||||||
|
])
|
||||||
|
def test_legacy_commands_simple(mock_run_legacy, command, script):
|
||||||
|
assert 0 == call_nominatim(*command)
|
||||||
|
|
||||||
|
assert mock_run_legacy.called == 1
|
||||||
|
assert mock_run_legacy.last_args[0] == script + '.php'
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("params", [
|
||||||
|
('search', '--query', 'new'),
|
||||||
|
('reverse', '--lat', '0', '--lon', '0'),
|
||||||
|
('lookup', '--id', 'N1'),
|
||||||
|
('details', '--node', '1'),
|
||||||
|
('details', '--way', '1'),
|
||||||
|
('details', '--relation', '1'),
|
||||||
|
('details', '--place_id', '10001'),
|
||||||
|
('status',)
|
||||||
|
])
|
||||||
|
def test_api_commands_simple(mock_run_api, params):
|
||||||
|
assert 0 == call_nominatim(*params)
|
||||||
|
|
||||||
|
assert mock_run_api.called == 1
|
||||||
|
assert mock_run_api.last_args[0] == params[0]
|
||||||
Reference in New Issue
Block a user