From: Sarah Hoffmann Date: Wed, 20 Jan 2021 08:53:47 +0000 (+0100) Subject: add simple tests for CLI parsing X-Git-Tag: v3.7.0~49^2~3 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/c26f323bf5e7679c8f4411862de32fae20709cf8 add simple tests for CLI parsing --- diff --git a/nominatim/cli.py b/nominatim/cli.py index 02b30f19..75790b13 100644 --- a/nominatim/cli.py +++ b/nominatim/cli.py @@ -197,13 +197,12 @@ class SetupSpecialPhrases: help='Pull special phrases from the OSM wiki.') group = parser.add_argument_group('Output arguments') group.add_argument('-o', '--output', default='-', - type=argparse.FileType('w', encoding='UTF-8'), help="""File to write the preprocessed phrases to. If omitted, it will be written to stdout.""") @staticmethod def run(args): - if args.output.name != '': + if args.output != '-': raise NotImplementedError('Only output to stdout is currently implemented.') return run_legacy_script('specialphrases.php', '--wiki-import', nominatim_env=args) diff --git a/test/python/test_cli.py b/test/python/test_cli.py new file mode 100644 index 00000000..36da3cd2 --- /dev/null +++ b/test/python/test_cli.py @@ -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]