]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli.py
71e3ff65cd8e9aed9005223353fff0c08ba9aca0
[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 import nominatim.indexer.indexer
9 import nominatim.tools.refresh
10 import nominatim.tools.replication
11
12 def call_nominatim(*args):
13     return nominatim.cli.nominatim(module_dir='build/module',
14                                    osm2pgsql_path='build/osm2pgsql/osm2pgsql',
15                                    phplib_dir='lib',
16                                    data_dir='.',
17                                    phpcgi_path='/usr/bin/php-cgi',
18                                    cli_args=args)
19
20 class MockParamCapture:
21     """ Mock that records the parameters with which a function was called
22         as well as the number of calls.
23     """
24     def __init__(self):
25         self.called = 0
26         self.return_value = 0
27
28     def __call__(self, *args, **kwargs):
29         self.called += 1
30         self.last_args = args
31         self.last_kwargs = kwargs
32         return self.return_value
33
34 @pytest.fixture
35 def mock_run_legacy(monkeypatch):
36     mock = MockParamCapture()
37     monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
38     return mock
39
40 @pytest.fixture
41 def mock_run_api(monkeypatch):
42     mock = MockParamCapture()
43     monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
44     return mock
45
46
47 def test_cli_help(capsys):
48     """ Running nominatim tool without arguments prints help.
49     """
50     assert 1 == call_nominatim()
51
52     captured = capsys.readouterr()
53     assert captured.out.startswith('usage:')
54
55
56 @pytest.mark.parametrize("command,script", [
57                          (('import', '--continue', 'load-data'), 'setup'),
58                          (('freeze',), 'setup'),
59                          (('special-phrases',), 'specialphrases'),
60                          (('add-data', '--tiger-data', 'tiger'), 'setup'),
61                          (('add-data', '--file', 'foo.osm'), 'update'),
62                          (('check-database',), 'check_import_finished'),
63                          (('warm',), 'warm'),
64                          (('export',), 'export')
65                          ])
66 def test_legacy_commands_simple(mock_run_legacy, command, script):
67     assert 0 == call_nominatim(*command)
68
69     assert mock_run_legacy.called == 1
70     assert mock_run_legacy.last_args[0] == script + '.php'
71
72
73 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
74                                       ('node', 12), ('way', 8), ('relation', 32)])
75 def test_add_data_command(mock_run_legacy, name, oid):
76     assert 0 == call_nominatim('add-data', '--' + name, str(oid))
77
78     assert mock_run_legacy.called == 1
79     assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
80
81
82 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
83                           ([], 1, 1),
84                           (['--boundaries-only'], 1, 0),
85                           (['--no-boundaries'], 0, 1),
86                           (['--boundaries-only', '--no-boundaries'], 0, 0)])
87 def test_index_command(monkeypatch, temp_db_cursor, params, do_bnds, do_ranks):
88     temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
89     bnd_mock = MockParamCapture()
90     monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', bnd_mock)
91     rank_mock = MockParamCapture()
92     monkeypatch.setattr(nominatim.indexer.indexer.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                          ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
102                          ('importance', ('update.php', '--recompute-importance')),
103                          ('website', ('setup.php', '--setup-website')),
104                          ])
105 def test_refresh_legacy_command(mock_run_legacy, temp_db, 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 @pytest.mark.parametrize("command,func", [
113                          ('postcodes', 'update_postcodes'),
114                          ('word-counts', 'recompute_word_counts'),
115                          ('address-levels', 'load_address_levels_from_file'),
116                          ('functions', 'create_functions'),
117                          ])
118 def test_refresh_command(monkeypatch, temp_db, command, func):
119     func_mock = MockParamCapture()
120     monkeypatch.setattr(nominatim.tools.refresh, func, func_mock)
121
122     assert 0 == call_nominatim('refresh', '--' + command)
123     assert func_mock.called == 1
124
125
126 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy, temp_db):
127     assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
128
129     assert mock_run_legacy.called == 2
130     assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
131
132
133 @pytest.mark.parametrize("params,func", [
134                          (('--init', '--no-update-functions'), 'init_replication'),
135                          (('--check-for-updates',), 'check_for_updates')
136                          ])
137 def test_replication_command(monkeypatch, temp_db, params, func):
138     func_mock = MockParamCapture()
139     monkeypatch.setattr(nominatim.tools.replication, func, func_mock)
140
141     assert 0 == call_nominatim('replication', *params)
142     assert func_mock.called == 1
143
144
145 @pytest.mark.parametrize("params", [
146                          ('search', '--query', 'new'),
147                          ('reverse', '--lat', '0', '--lon', '0'),
148                          ('lookup', '--id', 'N1'),
149                          ('details', '--node', '1'),
150                          ('details', '--way', '1'),
151                          ('details', '--relation', '1'),
152                          ('details', '--place_id', '10001'),
153                          ('status',)
154                          ])
155 def test_api_commands_simple(mock_run_api, params):
156     assert 0 == call_nominatim(*params)
157
158     assert mock_run_api.called == 1
159     assert mock_run_api.last_args[0] == params[0]