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 API access commands of command-line interface wrapper.
 
  13 import nominatim_db.clicmd.api
 
  14 import nominatim_api as napi
 
  16 @pytest.mark.parametrize('call', ['search', 'reverse', 'lookup', 'details', 'status'])
 
  17 def test_list_format(cli_call, call):
 
  18     assert 0 == cli_call(call, '--list-formats')
 
  21 @pytest.mark.parametrize('call', ['search', 'reverse', 'lookup', 'details', 'status'])
 
  22 def test_bad_format(cli_call, call):
 
  23     assert 1 == cli_call(call, '--format', 'rsdfsdfsdfsaefsdfsd')
 
  26 class TestCliStatusCall:
 
  28     @pytest.fixture(autouse=True)
 
  29     def setup_status_mock(self, monkeypatch):
 
  30         monkeypatch.setattr(napi.NominatimAPI, 'status',
 
  31                             lambda self: napi.StatusResult(200, 'OK'))
 
  34     def test_status_simple(self, cli_call, tmp_path):
 
  35         result = cli_call('status', '--project-dir', str(tmp_path))
 
  40     def test_status_json_format(self, cli_call, tmp_path, capsys):
 
  41         result = cli_call('status', '--project-dir', str(tmp_path),
 
  46         json.loads(capsys.readouterr().out)
 
  49 class TestCliDetailsCall:
 
  51     @pytest.fixture(autouse=True)
 
  52     def setup_status_mock(self, monkeypatch):
 
  53         result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
  54                                      napi.Point(1.0, -3.0))
 
  56         monkeypatch.setattr(napi.NominatimAPI, 'details',
 
  57                             lambda *args, **kwargs: result)
 
  59     @pytest.mark.parametrize("params", [('--node', '1'),
 
  62                                         ('--place_id', '10001')])
 
  64     def test_details_json_format(self, cli_call, tmp_path, capsys, params):
 
  65         result = cli_call('details', '--project-dir', str(tmp_path), *params)
 
  69         json.loads(capsys.readouterr().out)
 
  72 class TestCliReverseCall:
 
  74     @pytest.fixture(autouse=True)
 
  75     def setup_reverse_mock(self, monkeypatch):
 
  76         result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
  77                                     napi.Point(1.0, -3.0),
 
  78                                     names={'name':'Name', 'name:fr': 'Nom'},
 
  79                                     extratags={'extra':'Extra'},
 
  83         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
 
  84                             lambda *args, **kwargs: result)
 
  87     def test_reverse_simple(self, cli_call, tmp_path, capsys):
 
  88         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
  89                           '--lat', '34', '--lon', '34')
 
  93         out = json.loads(capsys.readouterr().out)
 
  94         assert out['name'] == 'Name'
 
  95         assert 'address' not in out
 
  96         assert 'extratags' not in out
 
  97         assert 'namedetails' not in out
 
 100     @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
 
 101                                              ('--extratags', 'extratags'),
 
 102                                              ('--namedetails', 'namedetails')])
 
 103     def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
 
 104         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
 105                           '--lat', '34', '--lon', '34', param)
 
 109         out = json.loads(capsys.readouterr().out)
 
 113     def test_reverse_format(self, cli_call, tmp_path, capsys):
 
 114         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
 115                           '--lat', '34', '--lon', '34', '--format', 'geojson')
 
 119         out = json.loads(capsys.readouterr().out)
 
 120         assert out['type'] == 'FeatureCollection'
 
 123 class TestCliLookupCall:
 
 125     @pytest.fixture(autouse=True)
 
 126     def setup_lookup_mock(self, monkeypatch):
 
 127         result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 128                                     napi.Point(1.0, -3.0),
 
 129                                     names={'name':'Name', 'name:fr': 'Nom'},
 
 130                                     extratags={'extra':'Extra'},
 
 134         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
 
 135                             lambda *args, **kwargs: napi.SearchResults([result]))
 
 137     def test_lookup_simple(self, cli_call, tmp_path, capsys):
 
 138         result = cli_call('lookup', '--project-dir', str(tmp_path),
 
 143         out = json.loads(capsys.readouterr().out)
 
 145         assert out[0]['name'] == 'Name'
 
 146         assert 'address' not in out[0]
 
 147         assert 'extratags' not in out[0]
 
 148         assert 'namedetails' not in out[0]
 
 151 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
 
 152                                               ('search_address', ('--city', 'Berlin'))
 
 154 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
 
 155     result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 156                                napi.Point(1.0, -3.0),
 
 157                                names={'name':'Name', 'name:fr': 'Nom'},
 
 158                                extratags={'extra':'Extra'},
 
 162     monkeypatch.setattr(napi.NominatimAPI, endpoint,
 
 163                         lambda *args, **kwargs: napi.SearchResults([result]))
 
 166     result = cli_call('search', '--project-dir', str(tmp_path), *params)
 
 170     out = json.loads(capsys.readouterr().out)
 
 172     assert out[0]['name'] == 'Name'
 
 173     assert 'address' not in out[0]
 
 174     assert 'extratags' not in out[0]
 
 175     assert 'namedetails' not in out[0]