]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_refresh.py
release 4.5.0.post7
[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                              ])
29     def test_refresh_command(self, mock_func_factory, command, func):
30         mock_func_factory(nominatim_db.tools.refresh, 'create_functions')
31         func_mock = mock_func_factory(nominatim_db.tools.refresh, func)
32
33         assert self.call_nominatim('refresh', '--' + command) == 0
34         assert func_mock.called == 1
35
36
37     def test_refresh_word_count(self):
38         assert self.call_nominatim('refresh', '--word-count') == 0
39         assert self.tokenizer_mock.update_statistics_called
40
41
42     def test_refresh_word_tokens(self):
43         assert self.call_nominatim('refresh', '--word-tokens') == 0
44         assert self.tokenizer_mock.update_word_tokens_called
45
46
47     def test_refresh_postcodes(self, async_mock_func_factory, mock_func_factory, place_table):
48         func_mock = mock_func_factory(nominatim_db.tools.postcodes, 'update_postcodes')
49         idx_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_postcodes')
50
51         assert self.call_nominatim('refresh', '--postcodes') == 0
52         assert func_mock.called == 1
53         assert idx_mock.called == 1
54
55
56     def test_refresh_postcodes_no_place_table(self):
57         # Do nothing without the place table
58         assert self.call_nominatim('refresh', '--postcodes') == 0
59
60
61     def test_refresh_create_functions(self, mock_func_factory):
62         func_mock = mock_func_factory(nominatim_db.tools.refresh, 'create_functions')
63
64         assert self.call_nominatim('refresh', '--functions') == 0
65         assert func_mock.called == 1
66         assert self.tokenizer_mock.update_sql_functions_called
67
68
69     def test_refresh_wikidata_file_not_found(self, monkeypatch):
70         monkeypatch.setenv('NOMINATIM_WIKIPEDIA_DATA_PATH', 'gjoiergjeroi345Q')
71
72         assert self.call_nominatim('refresh', '--wiki-data') == 1
73
74
75     def test_refresh_secondary_importance_file_not_found(self):
76         assert self.call_nominatim('refresh', '--secondary-importance') == 1
77
78
79     def test_refresh_secondary_importance_new_table(self, mock_func_factory):
80         mocks = [mock_func_factory(nominatim_db.tools.refresh, 'import_secondary_importance'),
81                  mock_func_factory(nominatim_db.tools.refresh, 'create_functions')]
82
83         assert self.call_nominatim('refresh', '--secondary-importance') == 0
84         assert mocks[0].called == 1
85         assert mocks[1].called == 1
86
87
88     def test_refresh_importance_computed_after_wiki_import(self, monkeypatch, mock_func_factory):
89         calls = []
90         monkeypatch.setattr(nominatim_db.tools.refresh, 'import_wikipedia_articles',
91                             lambda *args, **kwargs: calls.append('import') or 0)
92         monkeypatch.setattr(nominatim_db.tools.refresh, 'recompute_importance',
93                             lambda *args, **kwargs: calls.append('update'))
94         func_mock = mock_func_factory(nominatim_db.tools.refresh, 'create_functions')
95
96         assert self.call_nominatim('refresh', '--importance', '--wiki-data') == 0
97
98         assert calls == ['import', 'update']
99         assert func_mock.called == 1
100
101     @pytest.mark.parametrize('params', [('--data-object', 'w234'),
102                                         ('--data-object', 'N23', '--data-object', 'N24'),
103                                         ('--data-area', 'R7723'),
104                                         ('--data-area', 'r7723', '--data-area', 'r2'),
105                                         ('--data-area', 'R9284425', '--data-object', 'n1234567894567')])
106     def test_refresh_objects(self, params, mock_func_factory):
107         func_mock = mock_func_factory(nominatim_db.tools.refresh, 'invalidate_osm_object')
108
109         assert self.call_nominatim('refresh', *params) == 0
110
111         assert func_mock.called == len(params)/2
112
113
114     @pytest.mark.parametrize('func', ('--data-object', '--data-area'))
115     @pytest.mark.parametrize('param', ('234', 'a55', 'R 453', 'Rel'))
116     def test_refresh_objects_bad_param(self, func, param, mock_func_factory):
117         func_mock = mock_func_factory(nominatim_db.tools.refresh, 'invalidate_osm_object')
118
119         self.call_nominatim('refresh', func, param) == 1
120         assert func_mock.called == 0