]> git.openstreetmap.org Git - nominatim.git/commitdiff
add simple tests for CLI parsing
authorSarah Hoffmann <lonvia@denofr.de>
Wed, 20 Jan 2021 08:53:47 +0000 (09:53 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Wed, 20 Jan 2021 20:30:27 +0000 (21:30 +0100)
nominatim/cli.py
test/python/test_cli.py [new file with mode: 0644]

index 02b30f19e438e516ceb15a8e34ea197d1c49380d..75790b13917024089e8d74102c10a574df6ba25a 100644 (file)
@@ -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 != '<stdout>':
+        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 (file)
index 0000000..36da3cd
--- /dev/null
@@ -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]