1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for command line interface wrapper.
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
17 import nominatim_db.indexer.indexer
18 import nominatim_db.tools.add_osm_data
19 import nominatim_db.tools.freeze
22 def test_cli_help(cli_call, capsys):
23 """ Running nominatim tool without arguments prints help.
25 assert cli_call() == 1
27 captured = capsys.readouterr()
28 assert captured.out.startswith('usage:')
30 def test_cli_version(cli_call, capsys):
31 """ Running nominatim tool --version prints a version string.
33 assert cli_call('--version') == 0
35 captured = capsys.readouterr()
36 assert captured.out.startswith('Nominatim version')
38 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
39 def test_cli_add_data_file_command(cli_call, mock_func_factory, name, oid):
40 mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_data_from_file')
41 assert cli_call('add-data', '--' + name, str(oid)) == 0
43 assert mock_run_legacy.called == 1
46 @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
47 def test_cli_add_data_object_command(cli_call, mock_func_factory, name, oid):
48 mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_osm_object')
49 assert cli_call('add-data', '--' + name, str(oid)) == 0
51 assert mock_run_legacy.called == 1
55 def test_cli_add_data_tiger_data(cli_call, cli_tokenizer_mock, mock_func_factory):
56 mock = mock_func_factory(nominatim_db.tools.tiger_data, 'add_tiger_data')
58 assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
60 assert mock.called == 1
63 def test_cli_serve_php(cli_call, mock_func_factory):
64 func = mock_func_factory(nominatim_db.cli, 'run_php_server')
66 cli_call('serve', '--engine', 'php') == 0
68 assert func.called == 1
71 def test_cli_serve_starlette_custom_server(cli_call, mock_func_factory):
72 pytest.importorskip("starlette")
73 mod = pytest.importorskip("uvicorn")
74 func = mock_func_factory(mod, "run")
76 cli_call('serve', '--engine', 'starlette', '--server', 'foobar:4545') == 0
78 assert func.called == 1
79 assert func.last_kwargs['host'] == 'foobar'
80 assert func.last_kwargs['port'] == 4545
83 def test_cli_serve_starlette_custom_server_bad_port(cli_call, mock_func_factory):
84 pytest.importorskip("starlette")
85 mod = pytest.importorskip("uvicorn")
86 func = mock_func_factory(mod, "run")
88 cli_call('serve', '--engine', 'starlette', '--server', 'foobar:45:45') == 1
91 @pytest.mark.parametrize("engine", ['falcon', 'starlette'])
92 def test_cli_serve_uvicorn_based(cli_call, engine, mock_func_factory):
93 pytest.importorskip(engine)
94 mod = pytest.importorskip("uvicorn")
95 func = mock_func_factory(mod, "run")
97 cli_call('serve', '--engine', engine) == 0
99 assert func.called == 1
100 assert func.last_kwargs['host'] == '127.0.0.1'
101 assert func.last_kwargs['port'] == 8088
106 @pytest.fixture(autouse=True)
107 def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock):
108 self.call_nominatim = cli_call
109 self.tokenizer_mock = cli_tokenizer_mock
112 def test_freeze_command(self, mock_func_factory):
113 mock_drop = mock_func_factory(nominatim_db.tools.freeze, 'drop_update_tables')
114 mock_flatnode = mock_func_factory(nominatim_db.tools.freeze, 'drop_flatnode_file')
116 assert self.call_nominatim('freeze') == 0
118 assert mock_drop.called == 1
119 assert mock_flatnode.called == 1
122 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
124 (['--boundaries-only'], 1, 0),
125 (['--no-boundaries'], 0, 1),
126 (['--boundaries-only', '--no-boundaries'], 0, 0)])
127 def test_index_command(self, mock_func_factory, table_factory,
128 params, do_bnds, do_ranks):
129 table_factory('import_status', 'indexed bool')
130 bnd_mock = mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_boundaries')
131 rank_mock = mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_by_rank')
132 postcode_mock = mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_postcodes')
134 assert self.call_nominatim('index', *params) == 0
136 assert bnd_mock.called == do_bnds
137 assert rank_mock.called == do_ranks
138 assert postcode_mock.called == do_ranks
141 def test_special_phrases_wiki_command(self, mock_func_factory):
142 func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
144 self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
146 assert func.called == 1
149 def test_special_phrases_csv_command(self, src_dir, mock_func_factory):
150 func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
151 testdata = src_dir / 'test' / 'testdb'
152 csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
154 self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
156 assert func.called == 1
159 def test_special_phrases_csv_bad_file(self, src_dir):
160 testdata = src_dir / 'something349053905.csv'
162 self.call_nominatim('special-phrases', '--import-from-csv',
163 str(testdata.resolve())) == 1