]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli.py
move update code for postcode and word count to Python
[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, 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)
95
96     assert 0 == call_nominatim('index', *params)
97
98     assert bnd_mock.called == do_bnds
99     assert rank_mock.called == do_ranks
100
101
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')),
108                          ])
109 def test_refresh_legacy_command(mock_run_legacy, command, params):
110     assert 0 == call_nominatim('refresh', '--' + command)
111
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
115
116 @pytest.mark.parametrize("command,func", [
117                          ('postcodes', 'update_postcodes'),
118                          ('word-counts', 'recompute_word_counts'),
119                          ])
120 def test_refresh_command(monkeypatch, command, func):
121     func_mock = MockParamCapture()
122     monkeypatch.setattr(nominatim.tools.refresh, func, func_mock)
123
124     assert 0 == call_nominatim('refresh', '--' + command)
125
126     assert func_mock.called == 1
127
128 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy):
129     assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
130
131     assert mock_run_legacy.called == 2
132     assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
133
134
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'),
143                          ('status',)
144                          ])
145 def test_api_commands_simple(mock_run_api, params):
146     assert 0 == call_nominatim(*params)
147
148     assert mock_run_api.called == 1
149     assert mock_run_api.last_args[0] == params[0]