]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli.py
add tests for indexer
[nominatim.git] / test / python / test_cli.py
1 """
2 Tests for command line interface wrapper.
3 """
4 import pytest
5
6 import nominatim.cli
7
8 def call_nominatim(*args):
9     return nominatim.cli.nominatim(module_dir='build/module',
10                                    osm2pgsql_path='build/osm2pgsql/osm2pgsql',
11                                    phplib_dir='lib',
12                                    data_dir='.',
13                                    phpcgi_path='/usr/bin/php-cgi',
14                                    cli_args=args)
15
16 class MockParamCapture:
17     """ Mock that records the parameters with which a function was called
18         as well as the number of calls.
19     """
20     def __init__(self):
21         self.called = 0
22         self.return_value = 0
23
24     def __call__(self, *args, **kwargs):
25         self.called += 1
26         self.last_args = args
27         self.last_kwargs = kwargs
28         return self.return_value
29
30 @pytest.fixture
31 def mock_run_legacy(monkeypatch):
32     mock = MockParamCapture()
33     monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
34     return mock
35
36 @pytest.fixture
37 def mock_run_api(monkeypatch):
38     mock = MockParamCapture()
39     monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
40     return mock
41
42
43 def test_cli_help(capsys):
44     """ Running nominatim tool without arguments prints help.
45     """
46     assert 1 == call_nominatim()
47
48     captured = capsys.readouterr()
49     assert captured.out.startswith('usage:')
50
51 @pytest.mark.parametrize("command,script", [
52                          (('import', '--continue', 'load-data'), 'setup'),
53                          (('freeze',), 'setup'),
54                          (('special-phrases',), 'specialphrases'),
55                          (('replication',), 'update'),
56                          (('add-data', '--tiger-data', 'tiger'), 'setup'),
57                          (('add-data', '--file', 'foo.osm'), 'update'),
58                          (('check-database',), 'check_import_finished'),
59                          (('warm',), 'warm'),
60                          (('export',), 'export')
61                          ])
62 def test_legacy_commands_simple(mock_run_legacy, command, script):
63     assert 0 == call_nominatim(*command)
64
65     assert mock_run_legacy.called == 1
66     assert mock_run_legacy.last_args[0] == script + '.php'
67
68 @pytest.mark.parametrize("params", [
69                          ('search', '--query', 'new'),
70                          ('reverse', '--lat', '0', '--lon', '0'),
71                          ('lookup', '--id', 'N1'),
72                          ('details', '--node', '1'),
73                          ('details', '--way', '1'),
74                          ('details', '--relation', '1'),
75                          ('details', '--place_id', '10001'),
76                          ('status',)
77                          ])
78 def test_api_commands_simple(mock_run_api, params):
79     assert 0 == call_nominatim(*params)
80
81     assert mock_run_api.called == 1
82     assert mock_run_api.last_args[0] == params[0]