1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2025 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
 
  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
 
  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:')
 
  31 def test_cli_version(cli_call, capsys):
 
  32     """ Running nominatim tool --version prints a version string.
 
  34     assert cli_call('--version') == 0
 
  36     captured = capsys.readouterr()
 
  37     assert captured.out.startswith('Nominatim version')
 
  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')
 
  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
 
  54         assert mock_run_legacy.called == 1
 
  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
 
  61         assert mock_run_legacy.called == 1
 
  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')
 
  66         assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
 
  68         assert mock.called == 1
 
  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')
 
  74         assert self.call_nominatim('freeze') == 0
 
  76         assert mock_drop.called == 1
 
  77         assert mock_flatnode.called == 1
 
  79     @pytest.mark.parametrize("params,do_bnds,do_ranks", [
 
  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,
 
  89         rank_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer,
 
  91         postcode_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer,
 
  94         monkeypatch.setattr(nominatim_db.indexer.indexer.Indexer, 'has_pending',
 
  97         assert self.call_nominatim('index', *params) == 0
 
  99         assert bnd_mock.called == do_bnds
 
 100         assert rank_mock.called == do_ranks
 
 101         assert postcode_mock.called == do_ranks
 
 103     def test_special_phrases_wiki_command(self, mock_func_factory):
 
 104         func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
 
 106         self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
 
 108         assert func.called == 1
 
 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())
 
 115         self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
 
 117         assert func.called == 1
 
 119     def test_special_phrases_csv_bad_file(self, src_dir):
 
 120         testdata = src_dir / 'something349053905.csv'
 
 122         self.call_nominatim('special-phrases', '--import-from-csv',
 
 123                             str(testdata.resolve())) == 1