2 Tests for command line interface wrapper.
8 import nominatim.indexer.indexer
9 import nominatim.tools.refresh
11 def call_nominatim(*args):
12 return nominatim.cli.nominatim(module_dir='build/module',
13 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
16 phpcgi_path='/usr/bin/php-cgi',
19 class MockParamCapture:
20 """ Mock that records the parameters with which a function was called
21 as well as the number of calls.
27 def __call__(self, *args, **kwargs):
30 self.last_kwargs = kwargs
31 return self.return_value
34 def mock_run_legacy(monkeypatch):
35 mock = MockParamCapture()
36 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
40 def mock_run_api(monkeypatch):
41 mock = MockParamCapture()
42 monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
46 def test_cli_help(capsys):
47 """ Running nominatim tool without arguments prints help.
49 assert 1 == call_nominatim()
51 captured = capsys.readouterr()
52 assert captured.out.startswith('usage:')
55 @pytest.mark.parametrize("command,script", [
56 (('import', '--continue', 'load-data'), 'setup'),
57 (('freeze',), 'setup'),
58 (('special-phrases',), 'specialphrases'),
59 (('replication',), 'update'),
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, params, do_bnds, do_ranks):
88 with psycopg2.connect(database=temp_db) as conn:
89 with conn.cursor() as cur:
90 cur.execute("CREATE TABLE import_status (indexed bool)")
91 bnd_mock = MockParamCapture()
92 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', bnd_mock)
93 rank_mock = MockParamCapture()
94 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', rank_mock)
96 assert 0 == call_nominatim('index', *params)
98 assert bnd_mock.called == do_bnds
99 assert rank_mock.called == do_ranks
102 @pytest.mark.parametrize("command,params", [
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')),
109 def test_refresh_legacy_command(mock_run_legacy, command, params):
110 assert 0 == call_nominatim('refresh', '--' + command)
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
116 @pytest.mark.parametrize("command,func", [
117 ('postcodes', 'update_postcodes'),
118 ('word-counts', 'recompute_word_counts'),
120 def test_refresh_command(monkeypatch, command, func):
121 func_mock = MockParamCapture()
122 monkeypatch.setattr(nominatim.tools.refresh, func, func_mock)
124 assert 0 == call_nominatim('refresh', '--' + command)
126 assert func_mock.called == 1
128 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy):
129 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
131 assert mock_run_legacy.called == 2
132 assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
135 @pytest.mark.parametrize("params", [
136 ('search', '--query', 'new'),
137 ('reverse', '--lat', '0', '--lon', '0'),
138 ('lookup', '--id', 'N1'),
139 ('details', '--node', '1'),
140 ('details', '--way', '1'),
141 ('details', '--relation', '1'),
142 ('details', '--place_id', '10001'),
145 def test_api_commands_simple(mock_run_api, params):
146 assert 0 == call_nominatim(*params)
148 assert mock_run_api.called == 1
149 assert mock_run_api.last_args[0] == params[0]