]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_refresh.py
Integrated OSM views into importance computation
[nominatim.git] / test / python / cli / test_cmd_refresh.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for command line interface wrapper for refresk command.
9 """
10 import pytest
11
12 import nominatim.tools.refresh
13 import nominatim.tools.postcodes
14 import nominatim.indexer.indexer
15
16 class TestRefresh:
17
18     @pytest.fixture(autouse=True)
19     def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock):
20         self.call_nominatim = cli_call
21         self.tokenizer_mock = cli_tokenizer_mock
22
23
24     @pytest.mark.parametrize("command,func", [
25                              ('address-levels', 'load_address_levels_from_config'),
26                              ('wiki-data', 'import_wikipedia_articles'),
27                              ('osm-views', 'import_osm_views_geotiff'),
28                              ('importance', 'recompute_importance'),
29                              ('website', 'setup_website'),
30                              ])
31     def test_refresh_command(self, mock_func_factory, command, func):
32         func_mock = mock_func_factory(nominatim.tools.refresh, func)
33
34         assert self.call_nominatim('refresh', '--' + command) == 0
35         assert func_mock.called == 1
36
37
38     def test_refresh_word_count(self):
39         assert self.call_nominatim('refresh', '--word-count') == 0
40         assert self.tokenizer_mock.update_statistics_called
41
42
43     def test_refresh_word_tokens(self):
44         assert self.call_nominatim('refresh', '--word-tokens') == 0
45         assert self.tokenizer_mock.update_word_tokens_called
46
47
48     def test_refresh_postcodes(self, mock_func_factory, place_table):
49         func_mock = mock_func_factory(nominatim.tools.postcodes, 'update_postcodes')
50         idx_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_postcodes')
51
52         assert self.call_nominatim('refresh', '--postcodes') == 0
53         assert func_mock.called == 1
54         assert idx_mock.called == 1
55
56
57     def test_refresh_postcodes_no_place_table(self):
58         # Do nothing without the place table
59         assert self.call_nominatim('refresh', '--postcodes') == 0
60
61
62     def test_refresh_create_functions(self, mock_func_factory):
63         func_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
64
65         assert self.call_nominatim('refresh', '--functions') == 0
66         assert func_mock.called == 1
67         assert self.tokenizer_mock.update_sql_functions_called
68
69
70     def test_refresh_wikidata_file_not_found(self, monkeypatch):
71         monkeypatch.setenv('NOMINATIM_WIKIPEDIA_DATA_PATH', 'gjoiergjeroi345Q')
72
73         assert self.call_nominatim('refresh', '--wiki-data') == 1
74
75     def test_refresh_osm_views_geotiff_file_not_found(self):
76         assert self.call_nominatim('refresh', '--osm-views') == 1
77
78     def test_refresh_importance_computed_after_wiki_import(self, monkeypatch):
79         calls = []
80         monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
81                             lambda *args, **kwargs: calls.append('import') or 0)
82         monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
83                             lambda *args, **kwargs: calls.append('update'))
84
85         assert self.call_nominatim('refresh', '--importance', '--wiki-data') == 0
86
87         assert calls == ['import', 'update']
88
89     @pytest.mark.parametrize('params', [('--data-object', 'w234'),
90                                         ('--data-object', 'N23', '--data-object', 'N24'),
91                                         ('--data-area', 'R7723'),
92                                         ('--data-area', 'r7723', '--data-area', 'r2'),
93                                         ('--data-area', 'R9284425', '--data-object', 'n1234567894567')])
94     def test_refresh_objects(self, params, mock_func_factory):
95         func_mock = mock_func_factory(nominatim.tools.refresh, 'invalidate_osm_object')
96
97         assert self.call_nominatim('refresh', *params) == 0
98
99         assert func_mock.called == len(params)/2
100
101
102     @pytest.mark.parametrize('func', ('--data-object', '--data-area'))
103     @pytest.mark.parametrize('param', ('234', 'a55', 'R 453', 'Rel'))
104     def test_refresh_objects_bad_param(self, func, param, mock_func_factory):
105         func_mock = mock_func_factory(nominatim.tools.refresh, 'invalidate_osm_object')
106
107         self.call_nominatim('refresh', func, param) == 1
108         assert func_mock.called == 0