]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cli.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / cli / test_cli.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.
9
10 These tests just check that the various command line parameters route to the
11 correct functionality. They use a lot of monkeypatching to avoid executing
12 the actual functions.
13 """
14 import importlib
15 import pytest
16
17 import nominatim_db.indexer.indexer
18 import nominatim_db.tools.add_osm_data
19 import nominatim_db.tools.freeze
20 import nominatim_db.tools.tiger_data
21
22
23 def test_cli_help(cli_call, capsys):
24     """ Running nominatim tool without arguments prints help.
25     """
26     assert cli_call() == 1
27
28     captured = capsys.readouterr()
29     assert captured.out.startswith('usage:')
30
31 def test_cli_version(cli_call, capsys):
32     """ Running nominatim tool --version prints a version string.
33     """
34     assert cli_call('--version') == 0
35
36     captured = capsys.readouterr()
37     assert captured.out.startswith('Nominatim version')
38
39
40 class TestCliWithDb:
41
42     @pytest.fixture(autouse=True)
43     def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock, table_factory):
44         self.call_nominatim = cli_call
45         self.tokenizer_mock = cli_tokenizer_mock
46         # Make sure tools.freeze.is_frozen doesn't report database as frozen. Monkeypatching failed
47         table_factory('place')
48
49
50     @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
51     def test_cli_add_data_file_command(self, cli_call, mock_func_factory, name, oid):
52         mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_data_from_file')
53         assert cli_call('add-data', '--' + name, str(oid)) == 0
54
55         assert mock_run_legacy.called == 1
56
57
58     @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
59     def test_cli_add_data_object_command(self, cli_call, mock_func_factory, name, oid):
60         mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_osm_object')
61         assert cli_call('add-data', '--' + name, str(oid)) == 0
62
63         assert mock_run_legacy.called == 1
64
65
66
67     def test_cli_add_data_tiger_data(self, cli_call, cli_tokenizer_mock, async_mock_func_factory):
68         mock = async_mock_func_factory(nominatim_db.tools.tiger_data, 'add_tiger_data')
69
70         assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
71
72         assert mock.called == 1
73
74     def test_freeze_command(self, mock_func_factory):
75         mock_drop = mock_func_factory(nominatim_db.tools.freeze, 'drop_update_tables')
76         mock_flatnode = mock_func_factory(nominatim_db.tools.freeze, 'drop_flatnode_file')
77
78         assert self.call_nominatim('freeze') == 0
79
80         assert mock_drop.called == 1
81         assert mock_flatnode.called == 1
82
83
84     @pytest.mark.parametrize("params,do_bnds,do_ranks", [
85                               ([], 2, 2),
86                               (['--boundaries-only'], 2, 0),
87                               (['--no-boundaries'], 0, 2),
88                               (['--boundaries-only', '--no-boundaries'], 0, 0)])
89     def test_index_command(self, monkeypatch, async_mock_func_factory, table_factory,
90                            params, do_bnds, do_ranks):
91         table_factory('import_status', 'indexed bool')
92         bnd_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_boundaries')
93         rank_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_by_rank')
94         postcode_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_postcodes')
95
96         monkeypatch.setattr(nominatim_db.indexer.indexer.Indexer, 'has_pending', 
97                             [False, True].pop)
98
99         assert self.call_nominatim('index', *params) == 0
100
101         assert bnd_mock.called == do_bnds
102         assert rank_mock.called == do_ranks
103         assert postcode_mock.called == do_ranks
104
105
106     def test_special_phrases_wiki_command(self, mock_func_factory):
107         func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
108
109         self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
110
111         assert func.called == 1
112
113
114     def test_special_phrases_csv_command(self, src_dir, mock_func_factory):
115         func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
116         testdata = src_dir / 'test' / 'testdb'
117         csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
118
119         self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
120
121         assert func.called == 1
122
123
124     def test_special_phrases_csv_bad_file(self, src_dir):
125         testdata = src_dir / 'something349053905.csv'
126
127         self.call_nominatim('special-phrases', '--import-from-csv',
128                             str(testdata.resolve())) == 1