2 Tests for command line interface wrapper.
8 import nominatim.indexer.indexer
9 import nominatim.tools.refresh
10 import nominatim.tools.replication
12 def call_nominatim(*args):
13 return nominatim.cli.nominatim(module_dir='build/module',
14 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
17 phpcgi_path='/usr/bin/php-cgi',
20 class MockParamCapture:
21 """ Mock that records the parameters with which a function was called
22 as well as the number of calls.
28 def __call__(self, *args, **kwargs):
31 self.last_kwargs = kwargs
32 return self.return_value
35 def mock_run_legacy(monkeypatch):
36 mock = MockParamCapture()
37 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
41 def mock_run_api(monkeypatch):
42 mock = MockParamCapture()
43 monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
47 def test_cli_help(capsys):
48 """ Running nominatim tool without arguments prints help.
50 assert 1 == call_nominatim()
52 captured = capsys.readouterr()
53 assert captured.out.startswith('usage:')
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'),
64 (('export',), 'export')
66 def test_legacy_commands_simple(mock_run_legacy, command, script):
67 assert 0 == call_nominatim(*command)
69 assert mock_run_legacy.called == 1
70 assert mock_run_legacy.last_args[0] == script + '.php'
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))
78 assert mock_run_legacy.called == 1
79 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
82 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
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)
94 assert 0 == call_nominatim('index', *params)
96 assert bnd_mock.called == do_bnds
97 assert rank_mock.called == do_ranks
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')),
105 def test_refresh_legacy_command(mock_run_legacy, temp_db, command, params):
106 assert 0 == call_nominatim('refresh', '--' + command)
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
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'),
118 def test_refresh_command(monkeypatch, temp_db, command, func):
119 func_mock = MockParamCapture()
120 monkeypatch.setattr(nominatim.tools.refresh, func, func_mock)
122 assert 0 == call_nominatim('refresh', '--' + command)
123 assert func_mock.called == 1
126 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy, temp_db):
127 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
129 assert mock_run_legacy.called == 2
130 assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
133 @pytest.mark.parametrize("params,func", [
134 (('--init', '--no-update-functions'), 'init_replication'),
135 (('--check-for-updates',), 'check_for_updates')
137 def test_replication_command(monkeypatch, temp_db, params, func):
138 func_mock = MockParamCapture()
139 monkeypatch.setattr(nominatim.tools.replication, func, func_mock)
141 assert 0 == call_nominatim('replication', *params)
142 assert func_mock.called == 1
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'),
155 def test_api_commands_simple(mock_run_api, params):
156 assert 0 == call_nominatim(*params)
158 assert mock_run_api.called == 1
159 assert mock_run_api.last_args[0] == params[0]