2 Tests for command line interface wrapper.
4 These tests just check that the various command line parameters route to the
5 correct functionionality. They use a lot of monkeypatching to avoid executing
8 from pathlib import Path
13 import nominatim.clicmd.api
14 import nominatim.clicmd.refresh
15 import nominatim.clicmd.admin
16 import nominatim.indexer.indexer
17 import nominatim.tools.admin
18 import nominatim.tools.check_database
19 import nominatim.tools.freeze
20 import nominatim.tools.refresh
22 from mocks import MockParamCapture
24 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
26 def call_nominatim(*args):
27 return nominatim.cli.nominatim(module_dir='build/module',
28 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
29 phplib_dir=str(SRC_DIR / 'lib-php'),
30 data_dir=str(SRC_DIR / 'data'),
31 phpcgi_path='/usr/bin/php-cgi',
32 sqllib_dir=str(SRC_DIR / 'lib-sql'),
33 config_dir=str(SRC_DIR / 'settings'),
38 def mock_run_legacy(monkeypatch):
39 mock = MockParamCapture()
40 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
45 def mock_func_factory(monkeypatch):
46 def get_mock(module, func):
47 mock = MockParamCapture()
48 monkeypatch.setattr(module, func, mock)
54 def test_cli_help(capsys):
55 """ Running nominatim tool without arguments prints help.
57 assert 1 == call_nominatim()
59 captured = capsys.readouterr()
60 assert captured.out.startswith('usage:')
63 @pytest.mark.parametrize("command,script", [
64 (('import', '--continue', 'load-data'), 'setup'),
65 (('special-phrases',), 'specialphrases'),
66 (('add-data', '--tiger-data', 'tiger'), 'setup'),
67 (('add-data', '--file', 'foo.osm'), 'update'),
68 (('export',), 'export')
70 def test_legacy_commands_simple(mock_run_legacy, command, script):
71 assert 0 == call_nominatim(*command)
73 assert mock_run_legacy.called == 1
74 assert mock_run_legacy.last_args[0] == script + '.php'
77 def test_freeze_command(mock_func_factory, temp_db):
78 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
79 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
81 assert 0 == call_nominatim('freeze')
83 assert mock_drop.called == 1
84 assert mock_flatnode.called == 1
87 @pytest.mark.parametrize("params", [('--warm', ),
88 ('--warm', '--reverse-only'),
89 ('--warm', '--search-only')])
90 def test_admin_command_legacy(mock_func_factory, params):
91 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
93 assert 0 == call_nominatim('admin', *params)
95 assert mock_run_legacy.called == 1
98 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
99 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
100 mock = mock_func_factory(nominatim.tools.admin, func)
102 assert 0 == call_nominatim('admin', *params)
103 assert mock.called == 1
106 def test_admin_command_check_database(mock_func_factory):
107 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
109 assert 0 == call_nominatim('admin', '--check-database')
110 assert mock.called == 1
113 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
114 ('node', 12), ('way', 8), ('relation', 32)])
115 def test_add_data_command(mock_run_legacy, name, oid):
116 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
118 assert mock_run_legacy.called == 1
119 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
122 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
124 (['--boundaries-only'], 1, 0),
125 (['--no-boundaries'], 0, 1),
126 (['--boundaries-only', '--no-boundaries'], 0, 0)])
127 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
128 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
129 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
130 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
132 assert 0 == call_nominatim('index', *params)
134 assert bnd_mock.called == do_bnds
135 assert rank_mock.called == do_ranks
138 @pytest.mark.parametrize("command,func", [
139 ('postcodes', 'update_postcodes'),
140 ('word-counts', 'recompute_word_counts'),
141 ('address-levels', 'load_address_levels_from_file'),
142 ('functions', 'create_functions'),
143 ('wiki-data', 'import_wikipedia_articles'),
144 ('importance', 'recompute_importance'),
145 ('website', 'setup_website'),
147 def test_refresh_command(mock_func_factory, temp_db, command, func):
148 func_mock = mock_func_factory(nominatim.tools.refresh, func)
150 assert 0 == call_nominatim('refresh', '--' + command)
151 assert func_mock.called == 1
154 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
156 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
157 lambda *args, **kwargs: calls.append('import') or 0)
158 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
159 lambda *args, **kwargs: calls.append('update'))
161 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
163 assert calls == ['import', 'update']
166 def test_serve_command(mock_func_factory):
167 func = mock_func_factory(nominatim.cli, 'run_php_server')
169 call_nominatim('serve')
171 assert func.called == 1
173 @pytest.mark.parametrize("params", [
174 ('search', '--query', 'new'),
175 ('reverse', '--lat', '0', '--lon', '0'),
176 ('lookup', '--id', 'N1'),
177 ('details', '--node', '1'),
178 ('details', '--way', '1'),
179 ('details', '--relation', '1'),
180 ('details', '--place_id', '10001'),
183 def test_api_commands_simple(mock_func_factory, params):
184 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
186 assert 0 == call_nominatim(*params)
188 assert mock_run_api.called == 1
189 assert mock_run_api.last_args[0] == params[0]