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
 
  20 import nominatim_db.tools.tiger_data
 
  23 def test_cli_help(cli_call, capsys):
 
  24     """ Running nominatim tool without arguments prints help.
 
  26     assert cli_call() == 1
 
  28     captured = capsys.readouterr()
 
  29     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')
 
  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
 
  55         assert mock_run_legacy.called == 1
 
  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
 
  63         assert mock_run_legacy.called == 1
 
  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')
 
  70         assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
 
  72         assert mock.called == 1
 
  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')
 
  78         assert self.call_nominatim('freeze') == 0
 
  80         assert mock_drop.called == 1
 
  81         assert mock_flatnode.called == 1
 
  84     @pytest.mark.parametrize("params,do_bnds,do_ranks", [
 
  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')
 
  96         monkeypatch.setattr(nominatim_db.indexer.indexer.Indexer, 'has_pending', 
 
  99         assert self.call_nominatim('index', *params) == 0
 
 101         assert bnd_mock.called == do_bnds
 
 102         assert rank_mock.called == do_ranks
 
 103         assert postcode_mock.called == do_ranks
 
 106     def test_special_phrases_wiki_command(self, mock_func_factory):
 
 107         func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
 
 109         self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
 
 111         assert func.called == 1
 
 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())
 
 119         self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
 
 121         assert func.called == 1
 
 124     def test_special_phrases_csv_bad_file(self, src_dir):
 
 125         testdata = src_dir / 'something349053905.csv'
 
 127         self.call_nominatim('special-phrases', '--import-from-csv',
 
 128                             str(testdata.resolve())) == 1