]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cli.py
enable flake for Python tests
[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) 2025 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 pytest
15
16 import nominatim_db.indexer.indexer
17 import nominatim_db.tools.add_osm_data
18 import nominatim_db.tools.freeze
19 import nominatim_db.tools.tiger_data
20
21
22 def test_cli_help(cli_call, capsys):
23     """ Running nominatim tool without arguments prints help.
24     """
25     assert cli_call() == 1
26
27     captured = capsys.readouterr()
28     assert captured.out.startswith('usage:')
29
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     @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
50     def test_cli_add_data_file_command(self, cli_call, mock_func_factory, name, oid):
51         mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_data_from_file')
52         assert cli_call('add-data', '--' + name, str(oid)) == 0
53
54         assert mock_run_legacy.called == 1
55
56     @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
57     def test_cli_add_data_object_command(self, cli_call, mock_func_factory, name, oid):
58         mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_osm_object')
59         assert cli_call('add-data', '--' + name, str(oid)) == 0
60
61         assert mock_run_legacy.called == 1
62
63     def test_cli_add_data_tiger_data(self, cli_call, cli_tokenizer_mock, async_mock_func_factory):
64         mock = async_mock_func_factory(nominatim_db.tools.tiger_data, 'add_tiger_data')
65
66         assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
67
68         assert mock.called == 1
69
70     def test_freeze_command(self, mock_func_factory):
71         mock_drop = mock_func_factory(nominatim_db.tools.freeze, 'drop_update_tables')
72         mock_flatnode = mock_func_factory(nominatim_db.tools.freeze, 'drop_flatnode_file')
73
74         assert self.call_nominatim('freeze') == 0
75
76         assert mock_drop.called == 1
77         assert mock_flatnode.called == 1
78
79     @pytest.mark.parametrize("params,do_bnds,do_ranks", [
80                               ([], 2, 2),
81                               (['--boundaries-only'], 2, 0),
82                               (['--no-boundaries'], 0, 2),
83                               (['--boundaries-only', '--no-boundaries'], 0, 0)])
84     def test_index_command(self, monkeypatch, async_mock_func_factory, table_factory,
85                            params, do_bnds, do_ranks):
86         table_factory('import_status', 'indexed bool')
87         bnd_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer,
88                                            'index_boundaries')
89         rank_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer,
90                                             'index_by_rank')
91         postcode_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer,
92                                                 'index_postcodes')
93
94         monkeypatch.setattr(nominatim_db.indexer.indexer.Indexer, 'has_pending',
95                             [False, True].pop)
96
97         assert self.call_nominatim('index', *params) == 0
98
99         assert bnd_mock.called == do_bnds
100         assert rank_mock.called == do_ranks
101         assert postcode_mock.called == do_ranks
102
103     def test_special_phrases_wiki_command(self, mock_func_factory):
104         func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
105
106         self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
107
108         assert func.called == 1
109
110     def test_special_phrases_csv_command(self, src_dir, mock_func_factory):
111         func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
112         testdata = src_dir / 'test' / 'testdb'
113         csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
114
115         self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
116
117         assert func.called == 1
118
119     def test_special_phrases_csv_bad_file(self, src_dir):
120         testdata = src_dir / 'something349053905.csv'
121
122         self.call_nominatim('special-phrases', '--import-from-csv',
123                             str(testdata.resolve())) == 1