]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli.py
remove API comparison util
[nominatim.git] / test / python / test_cli.py
1 """
2 Tests for command line interface wrapper.
3 """
4 import psycopg2
5 import pytest
6
7 import nominatim.cli
8
9 def call_nominatim(*args):
10     return nominatim.cli.nominatim(module_dir='build/module',
11                                    osm2pgsql_path='build/osm2pgsql/osm2pgsql',
12                                    phplib_dir='lib',
13                                    data_dir='.',
14                                    phpcgi_path='/usr/bin/php-cgi',
15                                    cli_args=args)
16
17 class MockParamCapture:
18     """ Mock that records the parameters with which a function was called
19         as well as the number of calls.
20     """
21     def __init__(self):
22         self.called = 0
23         self.return_value = 0
24
25     def __call__(self, *args, **kwargs):
26         self.called += 1
27         self.last_args = args
28         self.last_kwargs = kwargs
29         return self.return_value
30
31 @pytest.fixture
32 def mock_run_legacy(monkeypatch):
33     mock = MockParamCapture()
34     monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
35     return mock
36
37 @pytest.fixture
38 def mock_run_api(monkeypatch):
39     mock = MockParamCapture()
40     monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
41     return mock
42
43
44 def test_cli_help(capsys):
45     """ Running nominatim tool without arguments prints help.
46     """
47     assert 1 == call_nominatim()
48
49     captured = capsys.readouterr()
50     assert captured.out.startswith('usage:')
51
52
53 @pytest.mark.parametrize("command,script", [
54                          (('import', '--continue', 'load-data'), 'setup'),
55                          (('freeze',), 'setup'),
56                          (('special-phrases',), 'specialphrases'),
57                          (('replication',), 'update'),
58                          (('add-data', '--tiger-data', 'tiger'), 'setup'),
59                          (('add-data', '--file', 'foo.osm'), 'update'),
60                          (('check-database',), 'check_import_finished'),
61                          (('warm',), 'warm'),
62                          (('export',), 'export')
63                          ])
64 def test_legacy_commands_simple(mock_run_legacy, command, script):
65     assert 0 == call_nominatim(*command)
66
67     assert mock_run_legacy.called == 1
68     assert mock_run_legacy.last_args[0] == script + '.php'
69
70
71 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
72                                       ('node', 12), ('way', 8), ('relation', 32)])
73 def test_add_data_command(mock_run_legacy, name, oid):
74     assert 0 == call_nominatim('add-data', '--' + name, str(oid))
75
76     assert mock_run_legacy.called == 1
77     assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
78
79
80 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
81                           ([], 1, 1),
82                           (['--boundaries-only'], 1, 0),
83                           (['--no-boundaries'], 0, 1),
84                           (['--boundaries-only', '--no-boundaries'], 0, 0)])
85 def test_index_command(monkeypatch, temp_db, params, do_bnds, do_ranks):
86     with psycopg2.connect(database=temp_db) as conn:
87         with conn.cursor() as cur:
88             cur.execute("CREATE TABLE import_status (indexed bool)")
89     bnd_mock = MockParamCapture()
90     monkeypatch.setattr(nominatim.cli.Indexer, 'index_boundaries', bnd_mock)
91     rank_mock = MockParamCapture()
92     monkeypatch.setattr(nominatim.cli.Indexer, 'index_by_rank', rank_mock)
93
94     assert 0 == call_nominatim('index', *params)
95
96     assert bnd_mock.called == do_bnds
97     assert rank_mock.called == do_ranks
98
99
100 @pytest.mark.parametrize("command,params", [
101                          ('postcodes', ('update.php', '--calculate-postcodes')),
102                          ('word-counts', ('update.php', '--recompute-word-counts')),
103                          ('address-levels', ('update.php', '--update-address-levels')),
104                          ('functions', ('setup.php',)),
105                          ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
106                          ('importance', ('update.php', '--recompute-importance')),
107                          ('website', ('setup.php', '--setup-website')),
108                          ])
109 def test_refresh_command(mock_run_legacy, command, params):
110     assert 0 == call_nominatim('refresh', '--' + command)
111
112     assert mock_run_legacy.called == 1
113     assert len(mock_run_legacy.last_args) >= len(params)
114     assert mock_run_legacy.last_args[:len(params)] == params
115
116
117 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy):
118     assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
119
120     assert mock_run_legacy.called == 2
121     assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
122
123
124 @pytest.mark.parametrize("params", [
125                          ('search', '--query', 'new'),
126                          ('reverse', '--lat', '0', '--lon', '0'),
127                          ('lookup', '--id', 'N1'),
128                          ('details', '--node', '1'),
129                          ('details', '--way', '1'),
130                          ('details', '--relation', '1'),
131                          ('details', '--place_id', '10001'),
132                          ('status',)
133                          ])
134 def test_api_commands_simple(mock_run_api, params):
135     assert 0 == call_nominatim(*params)
136
137     assert mock_run_api.called == 1
138     assert mock_run_api.last_args[0] == params[0]