]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli.py
add more tests for CLI parameter parser
[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
52 @pytest.mark.parametrize("command,script", [
53                          (('import', '--continue', 'load-data'), 'setup'),
54                          (('freeze',), 'setup'),
55                          (('special-phrases',), 'specialphrases'),
56                          (('replication',), 'update'),
57                          (('add-data', '--tiger-data', 'tiger'), 'setup'),
58                          (('add-data', '--file', 'foo.osm'), 'update'),
59                          (('check-database',), 'check_import_finished'),
60                          (('warm',), 'warm'),
61                          (('export',), 'export')
62                          ])
63 def test_legacy_commands_simple(mock_run_legacy, command, script):
64     assert 0 == call_nominatim(*command)
65
66     assert mock_run_legacy.called == 1
67     assert mock_run_legacy.last_args[0] == script + '.php'
68
69
70 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
71                                       ('node', 12), ('way', 8), ('relation', 32)])
72 def test_add_data_command(mock_run_legacy, name, oid):
73     assert 0 == call_nominatim('add-data', '--' + name, str(oid))
74
75     assert mock_run_legacy.called == 1
76     assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
77
78
79 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
80                           ([], 1, 1),
81                           (['--boundaries-only'], 1, 0),
82                           (['--no-boundaries'], 0, 1),
83                           (['--boundaries-only', '--no-boundaries'], 0, 0)])
84 def test_index_command(monkeypatch, params, do_bnds, do_ranks):
85     bnd_mock = MockParamCapture()
86     monkeypatch.setattr(nominatim.cli.Indexer, 'index_boundaries', bnd_mock)
87     rank_mock = MockParamCapture()
88     monkeypatch.setattr(nominatim.cli.Indexer, 'index_by_rank', rank_mock)
89
90     assert 0 == call_nominatim('index', *params)
91
92     assert bnd_mock.called == do_bnds
93     assert rank_mock.called == do_ranks
94
95
96 @pytest.mark.parametrize("command,params", [
97                          ('postcodes', ('update.php', '--calculate-postcodes')),
98                          ('word-counts', ('update.php', '--recompute-word-counts')),
99                          ('address-levels', ('update.php', '--update-address-levels')),
100                          ('functions', ('setup.php',)),
101                          ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
102                          ('importance', ('update.php', '--recompute-importance')),
103                          ('website', ('setup.php', '--setup-website')),
104                          ])
105 def test_refresh_command(mock_run_legacy, command, params):
106     assert 0 == call_nominatim('refresh', '--' + command)
107
108     assert mock_run_legacy.called == 1
109     assert len(mock_run_legacy.last_args) >= len(params)
110     assert mock_run_legacy.last_args[:len(params)] == params
111
112
113 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy):
114     assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
115
116     assert mock_run_legacy.called == 2
117     assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
118
119
120 @pytest.mark.parametrize("params", [
121                          ('search', '--query', 'new'),
122                          ('reverse', '--lat', '0', '--lon', '0'),
123                          ('lookup', '--id', 'N1'),
124                          ('details', '--node', '1'),
125                          ('details', '--way', '1'),
126                          ('details', '--relation', '1'),
127                          ('details', '--place_id', '10001'),
128                          ('status',)
129                          ])
130 def test_api_commands_simple(mock_run_api, params):
131     assert 0 == call_nominatim(*params)
132
133     assert mock_run_api.called == 1
134     assert mock_run_api.last_args[0] == params[0]