1 # SPDX-License-Identifier: GPL-2.0-only
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2023 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.clicmd.api
 
  14 import nominatim.api as napi
 
  17 class TestCliStatusCall:
 
  19     @pytest.fixture(autouse=True)
 
  20     def setup_status_mock(self, monkeypatch):
 
  21         monkeypatch.setattr(napi.NominatimAPI, 'status',
 
  22                             lambda self: napi.StatusResult(200, 'OK'))
 
  25     def test_status_simple(self, cli_call, tmp_path):
 
  26         result = cli_call('status', '--project-dir', str(tmp_path))
 
  31     def test_status_json_format(self, cli_call, tmp_path, capsys):
 
  32         result = cli_call('status', '--project-dir', str(tmp_path),
 
  37         json.loads(capsys.readouterr().out)
 
  40 class TestCliDetailsCall:
 
  42     @pytest.fixture(autouse=True)
 
  43     def setup_status_mock(self, monkeypatch):
 
  44         result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
  45                                      napi.Point(1.0, -3.0))
 
  47         monkeypatch.setattr(napi.NominatimAPI, 'details',
 
  48                             lambda *args, **kwargs: result)
 
  50     @pytest.mark.parametrize("params", [('--node', '1'),
 
  53                                         ('--place_id', '10001')])
 
  55     def test_details_json_format(self, cli_call, tmp_path, capsys, params):
 
  56         result = cli_call('details', '--project-dir', str(tmp_path), *params)
 
  60         json.loads(capsys.readouterr().out)
 
  63 class TestCliReverseCall:
 
  65     @pytest.fixture(autouse=True)
 
  66     def setup_reverse_mock(self, monkeypatch):
 
  67         result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
  68                                     napi.Point(1.0, -3.0),
 
  69                                     names={'name':'Name', 'name:fr': 'Nom'},
 
  70                                     extratags={'extra':'Extra'})
 
  72         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
 
  73                             lambda *args, **kwargs: result)
 
  76     def test_reverse_simple(self, cli_call, tmp_path, capsys):
 
  77         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
  78                           '--lat', '34', '--lon', '34')
 
  82         out = json.loads(capsys.readouterr().out)
 
  83         assert out['name'] == 'Name'
 
  84         assert 'address' not in out
 
  85         assert 'extratags' not in out
 
  86         assert 'namedetails' not in out
 
  89     @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
 
  90                                              ('--extratags', 'extratags'),
 
  91                                              ('--namedetails', 'namedetails')])
 
  92     def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
 
  93         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
  94                           '--lat', '34', '--lon', '34', param)
 
  98         out = json.loads(capsys.readouterr().out)
 
 102     def test_reverse_format(self, cli_call, tmp_path, capsys):
 
 103         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
 104                           '--lat', '34', '--lon', '34', '--format', 'geojson')
 
 108         out = json.loads(capsys.readouterr().out)
 
 109         assert out['type'] == 'FeatureCollection'
 
 112     def test_reverse_language(self, cli_call, tmp_path, capsys):
 
 113         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
 114                           '--lat', '34', '--lon', '34', '--lang', 'fr')
 
 118         out = json.loads(capsys.readouterr().out)
 
 119         assert out['name'] == 'Nom'
 
 122 class TestCliLookupCall:
 
 124     @pytest.fixture(autouse=True)
 
 125     def setup_lookup_mock(self, monkeypatch):
 
 126         result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 127                                     napi.Point(1.0, -3.0),
 
 128                                     names={'name':'Name', 'name:fr': 'Nom'},
 
 129                                     extratags={'extra':'Extra'})
 
 131         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
 
 132                             lambda *args, **kwargs: napi.SearchResults([result]))
 
 134     def test_lookup_simple(self, cli_call, tmp_path, capsys):
 
 135         result = cli_call('lookup', '--project-dir', str(tmp_path),
 
 140         out = json.loads(capsys.readouterr().out)
 
 142         assert out[0]['name'] == 'Name'
 
 143         assert 'address' not in out[0]
 
 144         assert 'extratags' not in out[0]
 
 145         assert 'namedetails' not in out[0]
 
 148 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
 
 149                                               ('search_address', ('--city', 'Berlin'))
 
 151 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
 
 152     result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 153                                 napi.Point(1.0, -3.0),
 
 154                                 names={'name':'Name', 'name:fr': 'Nom'},
 
 155                                 extratags={'extra':'Extra'})
 
 157     monkeypatch.setattr(napi.NominatimAPI, endpoint,
 
 158                         lambda *args, **kwargs: napi.SearchResults([result]))
 
 161     result = cli_call('search', '--project-dir', str(tmp_path), *params)
 
 165     out = json.loads(capsys.readouterr().out)
 
 167     assert out[0]['name'] == 'Name'
 
 168     assert 'address' not in out[0]
 
 169     assert 'extratags' not in out[0]
 
 170     assert 'namedetails' not in out[0]