]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli.py
bd192260f3d73307b7fbcb39e264b748fbec00b5
[nominatim.git] / test / python / test_cli.py
1 """
2 Tests for command line interface wrapper.
3 """
4 import psycopg2
5 import pytest
6
7 import nominatim.cli
8 import nominatim.indexer.indexer
9 import nominatim.tools.refresh
10
11 def call_nominatim(*args):
12     return nominatim.cli.nominatim(module_dir='build/module',
13                                    osm2pgsql_path='build/osm2pgsql/osm2pgsql',
14                                    phplib_dir='lib',
15                                    data_dir='.',
16                                    phpcgi_path='/usr/bin/php-cgi',
17                                    cli_args=args)
18
19 class MockParamCapture:
20     """ Mock that records the parameters with which a function was called
21         as well as the number of calls.
22     """
23     def __init__(self):
24         self.called = 0
25         self.return_value = 0
26
27     def __call__(self, *args, **kwargs):
28         self.called += 1
29         self.last_args = args
30         self.last_kwargs = kwargs
31         return self.return_value
32
33 @pytest.fixture
34 def mock_run_legacy(monkeypatch):
35     mock = MockParamCapture()
36     monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
37     return mock
38
39 @pytest.fixture
40 def mock_run_api(monkeypatch):
41     mock = MockParamCapture()
42     monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
43     return mock
44
45
46 def test_cli_help(capsys):
47     """ Running nominatim tool without arguments prints help.
48     """
49     assert 1 == call_nominatim()
50
51     captured = capsys.readouterr()
52     assert captured.out.startswith('usage:')
53
54
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'),
63                          (('warm',), 'warm'),
64                          (('export',), 'export')
65                          ])
66 def test_legacy_commands_simple(mock_run_legacy, command, script):
67     assert 0 == call_nominatim(*command)
68
69     assert mock_run_legacy.called == 1
70     assert mock_run_legacy.last_args[0] == script + '.php'
71
72
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))
77
78     assert mock_run_legacy.called == 1
79     assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
80
81
82 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
83                           ([], 1, 1),
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)
93
94     assert 0 == call_nominatim('index', *params)
95
96     assert bnd_mock.called == do_bnds
97     assert rank_mock.called == do_ranks
98
99
100 @pytest.mark.parametrize("command,params", [
101                          ('functions', ('setup.php',)),
102                          ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
103                          ('importance', ('update.php', '--recompute-importance')),
104                          ('website', ('setup.php', '--setup-website')),
105                          ])
106 def test_refresh_legacy_command(mock_run_legacy, command, params):
107     assert 0 == call_nominatim('refresh', '--' + command)
108
109     assert mock_run_legacy.called == 1
110     assert len(mock_run_legacy.last_args) >= len(params)
111     assert mock_run_legacy.last_args[:len(params)] == params
112
113 @pytest.mark.parametrize("command,func", [
114                          ('postcodes', 'update_postcodes'),
115                          ('word-counts', 'recompute_word_counts'),
116                          ('address-levels', 'load_address_levels_from_file'),
117                          ])
118 def test_refresh_command(monkeypatch, command, func):
119     func_mock = MockParamCapture()
120     monkeypatch.setattr(nominatim.tools.refresh, func, func_mock)
121
122     assert 0 == call_nominatim('refresh', '--' + command)
123
124     assert func_mock.called == 1
125
126 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy):
127     assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
128
129     assert mock_run_legacy.called == 2
130     assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
131
132
133 @pytest.mark.parametrize("params", [
134                          ('search', '--query', 'new'),
135                          ('reverse', '--lat', '0', '--lon', '0'),
136                          ('lookup', '--id', 'N1'),
137                          ('details', '--node', '1'),
138                          ('details', '--way', '1'),
139                          ('details', '--relation', '1'),
140                          ('details', '--place_id', '10001'),
141                          ('status',)
142                          ])
143 def test_api_commands_simple(mock_run_api, params):
144     assert 0 == call_nominatim(*params)
145
146     assert mock_run_api.called == 1
147     assert mock_run_api.last_args[0] == params[0]