2 Tests for command line interface wrapper.
9 def call_nominatim(*args):
10 return nominatim.cli.nominatim(module_dir='build/module',
11 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
14 phpcgi_path='/usr/bin/php-cgi',
17 class MockParamCapture:
18 """ Mock that records the parameters with which a function was called
19 as well as the number of calls.
25 def __call__(self, *args, **kwargs):
28 self.last_kwargs = kwargs
29 return self.return_value
32 def mock_run_legacy(monkeypatch):
33 mock = MockParamCapture()
34 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
38 def mock_run_api(monkeypatch):
39 mock = MockParamCapture()
40 monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
44 def test_cli_help(capsys):
45 """ Running nominatim tool without arguments prints help.
47 assert 1 == call_nominatim()
49 captured = capsys.readouterr()
50 assert captured.out.startswith('usage:')
53 @pytest.mark.parametrize("command,script", [
54 (('import', '--continue', 'load-data'), 'setup'),
55 (('freeze',), 'setup'),
56 (('special-phrases',), 'specialphrases'),
57 (('replication',), 'update'),
58 (('add-data', '--tiger-data', 'tiger'), 'setup'),
59 (('add-data', '--file', 'foo.osm'), 'update'),
60 (('check-database',), 'check_import_finished'),
62 (('export',), 'export')
64 def test_legacy_commands_simple(mock_run_legacy, command, script):
65 assert 0 == call_nominatim(*command)
67 assert mock_run_legacy.called == 1
68 assert mock_run_legacy.last_args[0] == script + '.php'
71 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
72 ('node', 12), ('way', 8), ('relation', 32)])
73 def test_add_data_command(mock_run_legacy, name, oid):
74 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
76 assert mock_run_legacy.called == 1
77 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
80 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
82 (['--boundaries-only'], 1, 0),
83 (['--no-boundaries'], 0, 1),
84 (['--boundaries-only', '--no-boundaries'], 0, 0)])
85 def test_index_command(monkeypatch, temp_db, params, do_bnds, do_ranks):
86 with psycopg2.connect(database=temp_db) as conn:
87 with conn.cursor() as cur:
88 cur.execute("CREATE TABLE import_status (indexed bool)")
89 bnd_mock = MockParamCapture()
90 monkeypatch.setattr(nominatim.cli.Indexer, 'index_boundaries', bnd_mock)
91 rank_mock = MockParamCapture()
92 monkeypatch.setattr(nominatim.cli.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 ('postcodes', ('update.php', '--calculate-postcodes')),
102 ('word-counts', ('update.php', '--recompute-word-counts')),
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_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
117 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy):
118 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
120 assert mock_run_legacy.called == 2
121 assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
124 @pytest.mark.parametrize("params", [
125 ('search', '--query', 'new'),
126 ('reverse', '--lat', '0', '--lon', '0'),
127 ('lookup', '--id', 'N1'),
128 ('details', '--node', '1'),
129 ('details', '--way', '1'),
130 ('details', '--relation', '1'),
131 ('details', '--place_id', '10001'),
134 def test_api_commands_simple(mock_run_api, params):
135 assert 0 == call_nominatim(*params)
137 assert mock_run_api.called == 1
138 assert mock_run_api.last_args[0] == params[0]