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)
44 def mock_func_factory(monkeypatch):
45 def get_mock(module, func):
46 mock = MockParamCapture()
47 monkeypatch.setattr(module, func, mock)
52 def test_cli_help(capsys):
53 """ Running nominatim tool without arguments prints help.
55 assert 1 == call_nominatim()
57 captured = capsys.readouterr()
58 assert captured.out.startswith('usage:')
61 @pytest.mark.parametrize("command,script", [
62 (('import', '--continue', 'load-data'), 'setup'),
63 (('special-phrases',), 'specialphrases'),
64 (('add-data', '--tiger-data', 'tiger'), 'setup'),
65 (('add-data', '--file', 'foo.osm'), 'update'),
66 (('export',), 'export')
68 def test_legacy_commands_simple(mock_run_legacy, command, script):
69 assert 0 == call_nominatim(*command)
71 assert mock_run_legacy.called == 1
72 assert mock_run_legacy.last_args[0] == script + '.php'
75 def test_freeze_command(mock_func_factory, temp_db):
76 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
77 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
79 assert 0 == call_nominatim('freeze')
81 assert mock_drop.called == 1
82 assert mock_flatnode.called == 1
85 @pytest.mark.parametrize("params", [('--warm', ),
86 ('--warm', '--reverse-only'),
87 ('--warm', '--search-only')])
88 def test_admin_command_legacy(mock_func_factory, params):
89 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
91 assert 0 == call_nominatim('admin', *params)
93 assert mock_run_legacy.called == 1
96 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
97 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
98 mock = mock_func_factory(nominatim.tools.admin, func)
100 assert 0 == call_nominatim('admin', *params)
101 assert mock.called == 1
104 def test_admin_command_check_database(mock_func_factory):
105 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
107 assert 0 == call_nominatim('admin', '--check-database')
108 assert mock.called == 1
111 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
112 ('node', 12), ('way', 8), ('relation', 32)])
113 def test_add_data_command(mock_run_legacy, name, oid):
114 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
116 assert mock_run_legacy.called == 1
117 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
120 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
122 (['--boundaries-only'], 1, 0),
123 (['--no-boundaries'], 0, 1),
124 (['--boundaries-only', '--no-boundaries'], 0, 0)])
125 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
126 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
127 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
128 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
130 assert 0 == call_nominatim('index', *params)
132 assert bnd_mock.called == do_bnds
133 assert rank_mock.called == do_ranks
136 @pytest.mark.parametrize("command,params", [
137 ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
138 ('importance', ('update.php', '--recompute-importance')),
140 def test_refresh_legacy_command(mock_func_factory, temp_db, command, params):
141 mock_run_legacy = mock_func_factory(nominatim.clicmd.refresh, 'run_legacy_script')
143 assert 0 == call_nominatim('refresh', '--' + command)
145 assert mock_run_legacy.called == 1
146 assert len(mock_run_legacy.last_args) >= len(params)
147 assert mock_run_legacy.last_args[:len(params)] == params
149 @pytest.mark.parametrize("command,func", [
150 ('postcodes', 'update_postcodes'),
151 ('word-counts', 'recompute_word_counts'),
152 ('address-levels', 'load_address_levels_from_file'),
153 ('functions', 'create_functions'),
154 ('website', 'setup_website'),
156 def test_refresh_command(mock_func_factory, temp_db, command, func):
157 func_mock = mock_func_factory(nominatim.tools.refresh, func)
159 assert 0 == call_nominatim('refresh', '--' + command)
160 assert func_mock.called == 1
163 def test_refresh_importance_computed_after_wiki_import(mock_func_factory, temp_db):
164 mock_run_legacy = mock_func_factory(nominatim.clicmd.refresh, 'run_legacy_script')
166 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
168 assert mock_run_legacy.called == 2
169 assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
172 def test_serve_command(mock_func_factory):
173 func = mock_func_factory(nominatim.cli, 'run_php_server')
175 call_nominatim('serve')
177 assert func.called == 1
179 @pytest.mark.parametrize("params", [
180 ('search', '--query', 'new'),
181 ('reverse', '--lat', '0', '--lon', '0'),
182 ('lookup', '--id', 'N1'),
183 ('details', '--node', '1'),
184 ('details', '--way', '1'),
185 ('details', '--relation', '1'),
186 ('details', '--place_id', '10001'),
189 def test_api_commands_simple(mock_func_factory, params):
190 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
192 assert 0 == call_nominatim(*params)
194 assert mock_run_api.called == 1
195 assert mock_run_api.last_args[0] == params[0]