]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_refresh.py
lift restrictions on search with frequent terms slightly
[nominatim.git] / test / python / cli / test_cmd_refresh.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2024 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_db.tools.refresh
13 import nominatim_db.tools.postcodes
14 import nominatim_db.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                              ('importance', 'recompute_importance'),
28                              ('website', 'setup_website'),
29                              ])
30     def test_refresh_command(self, mock_func_factory, command, func):
31         mock_func_factory(nominatim_db.tools.refresh, 'create_functions')
32         func_mock = mock_func_factory(nominatim_db.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, async_mock_func_factory, mock_func_factory, place_table):
49         func_mock = mock_func_factory(nominatim_db.tools.postcodes, 'update_postcodes')
50         idx_mock = async_mock_func_factory(nominatim_db.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_db.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
76     def test_refresh_secondary_importance_file_not_found(self):
77         assert self.call_nominatim('refresh', '--secondary-importance') == 1
78
79
80     def test_refresh_secondary_importance_new_table(self, mock_func_factory):
81         mocks = [mock_func_factory(nominatim_db.tools.refresh, 'import_secondary_importance'),
82                  mock_func_factory(nominatim_db.tools.refresh, 'create_functions')]
83
84         assert self.call_nominatim('refresh', '--secondary-importance') == 0
85         assert mocks[0].called == 1
86         assert mocks[1].called == 1
87
88
89     def test_refresh_importance_computed_after_wiki_import(self, monkeypatch, mock_func_factory):
90         calls = []
91         monkeypatch.setattr(nominatim_db.tools.refresh, 'import_wikipedia_articles',
92                             lambda *args, **kwargs: calls.append('import') or 0)
93         monkeypatch.setattr(nominatim_db.tools.refresh, 'recompute_importance',
94                             lambda *args, **kwargs: calls.append('update'))
95         func_mock = mock_func_factory(nominatim_db.tools.refresh, 'create_functions')
96
97         assert self.call_nominatim('refresh', '--importance', '--wiki-data') == 0
98
99         assert calls == ['import', 'update']
100         assert func_mock.called == 1
101
102     @pytest.mark.parametrize('params', [('--data-object', 'w234'),
103                                         ('--data-object', 'N23', '--data-object', 'N24'),
104                                         ('--data-area', 'R7723'),
105                                         ('--data-area', 'r7723', '--data-area', 'r2'),
106                                         ('--data-area', 'R9284425', '--data-object', 'n1234567894567')])
107     def test_refresh_objects(self, params, mock_func_factory):
108         func_mock = mock_func_factory(nominatim_db.tools.refresh, 'invalidate_osm_object')
109
110         assert self.call_nominatim('refresh', *params) == 0
111
112         assert func_mock.called == len(params)/2
113
114
115     @pytest.mark.parametrize('func', ('--data-object', '--data-area'))
116     @pytest.mark.parametrize('param', ('234', 'a55', 'R 453', 'Rel'))
117     def test_refresh_objects_bad_param(self, func, param, mock_func_factory):
118         func_mock = mock_func_factory(nominatim_db.tools.refresh, 'invalidate_osm_object')
119
120         self.call_nominatim('refresh', func, param) == 1
121         assert func_mock.called == 0