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'},
 
  74         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
 
  75                             lambda *args, **kwargs: result)
 
  78     def test_reverse_simple(self, cli_call, tmp_path, capsys):
 
  79         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
  80                           '--lat', '34', '--lon', '34')
 
  84         out = json.loads(capsys.readouterr().out)
 
  85         assert out['name'] == 'Name'
 
  86         assert 'address' not in out
 
  87         assert 'extratags' not in out
 
  88         assert 'namedetails' not in out
 
  91     @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
 
  92                                              ('--extratags', 'extratags'),
 
  93                                              ('--namedetails', 'namedetails')])
 
  94     def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
 
  95         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
  96                           '--lat', '34', '--lon', '34', param)
 
 100         out = json.loads(capsys.readouterr().out)
 
 104     def test_reverse_format(self, cli_call, tmp_path, capsys):
 
 105         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
 106                           '--lat', '34', '--lon', '34', '--format', 'geojson')
 
 110         out = json.loads(capsys.readouterr().out)
 
 111         assert out['type'] == 'FeatureCollection'
 
 114 class TestCliLookupCall:
 
 116     @pytest.fixture(autouse=True)
 
 117     def setup_lookup_mock(self, monkeypatch):
 
 118         result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 119                                     napi.Point(1.0, -3.0),
 
 120                                     names={'name':'Name', 'name:fr': 'Nom'},
 
 121                                     extratags={'extra':'Extra'},
 
 125         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
 
 126                             lambda *args, **kwargs: napi.SearchResults([result]))
 
 128     def test_lookup_simple(self, cli_call, tmp_path, capsys):
 
 129         result = cli_call('lookup', '--project-dir', str(tmp_path),
 
 134         out = json.loads(capsys.readouterr().out)
 
 136         assert out[0]['name'] == 'Name'
 
 137         assert 'address' not in out[0]
 
 138         assert 'extratags' not in out[0]
 
 139         assert 'namedetails' not in out[0]
 
 142 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
 
 143                                               ('search_address', ('--city', 'Berlin'))
 
 145 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
 
 146     result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 147                                napi.Point(1.0, -3.0),
 
 148                                names={'name':'Name', 'name:fr': 'Nom'},
 
 149                                extratags={'extra':'Extra'},
 
 153     monkeypatch.setattr(napi.NominatimAPI, endpoint,
 
 154                         lambda *args, **kwargs: napi.SearchResults([result]))
 
 157     result = cli_call('search', '--project-dir', str(tmp_path), *params)
 
 161     out = json.loads(capsys.readouterr().out)
 
 163     assert out[0]['name'] == 'Name'
 
 164     assert 'address' not in out[0]
 
 165     assert 'extratags' not in out[0]
 
 166     assert 'namedetails' not in out[0]