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 API access commands of command-line interface wrapper.
 
  13 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'))
 
  33     def test_status_simple(self, cli_call, tmp_path):
 
  34         result = cli_call('status', '--project-dir', str(tmp_path))
 
  38     def test_status_json_format(self, cli_call, tmp_path, capsys):
 
  39         result = cli_call('status', '--project-dir', str(tmp_path),
 
  44         json.loads(capsys.readouterr().out)
 
  47 class TestCliDetailsCall:
 
  49     @pytest.fixture(autouse=True)
 
  50     def setup_status_mock(self, monkeypatch):
 
  51         result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
  52                                      napi.Point(1.0, -3.0))
 
  54         monkeypatch.setattr(napi.NominatimAPI, 'details',
 
  55                             lambda *args, **kwargs: result)
 
  57     @pytest.mark.parametrize("params", [('--node', '1'),
 
  60                                         ('--place_id', '10001')])
 
  61     def test_details_json_format(self, cli_call, tmp_path, capsys, params):
 
  62         result = cli_call('details', '--project-dir', str(tmp_path), *params)
 
  66         json.loads(capsys.readouterr().out)
 
  69 class TestCliReverseCall:
 
  71     @pytest.fixture(autouse=True)
 
  72     def setup_reverse_mock(self, monkeypatch):
 
  73         result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
  74                                     napi.Point(1.0, -3.0),
 
  75                                     names={'name': 'Name', 'name:fr': 'Nom'},
 
  76                                     extratags={'extra': 'Extra'},
 
  79         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
 
  80                             lambda *args, **kwargs: result)
 
  82     def test_reverse_simple(self, cli_call, tmp_path, capsys):
 
  83         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
  84                           '--lat', '34', '--lon', '34')
 
  88         out = json.loads(capsys.readouterr().out)
 
  89         assert out['name'] == 'Name'
 
  90         assert 'address' not in out
 
  91         assert 'extratags' not in out
 
  92         assert 'namedetails' not in out
 
  94     @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
 
  95                                              ('--extratags', 'extratags'),
 
  96                                              ('--namedetails', 'namedetails')])
 
  97     def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
 
  98         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
  99                           '--lat', '34', '--lon', '34', param)
 
 103         out = json.loads(capsys.readouterr().out)
 
 106     def test_reverse_format(self, cli_call, tmp_path, capsys):
 
 107         result = cli_call('reverse', '--project-dir', str(tmp_path),
 
 108                           '--lat', '34', '--lon', '34', '--format', 'geojson')
 
 112         out = json.loads(capsys.readouterr().out)
 
 113         assert out['type'] == 'FeatureCollection'
 
 116 class TestCliLookupCall:
 
 118     @pytest.fixture(autouse=True)
 
 119     def setup_lookup_mock(self, monkeypatch):
 
 120         result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 121                                    napi.Point(1.0, -3.0),
 
 122                                    names={'name': 'Name', 'name:fr': 'Nom'},
 
 123                                    extratags={'extra': 'Extra'},
 
 126         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
 
 127                             lambda *args, **kwargs: napi.SearchResults([result]))
 
 129     def test_lookup_simple(self, cli_call, tmp_path, capsys):
 
 130         result = cli_call('lookup', '--project-dir', str(tmp_path),
 
 135         out = json.loads(capsys.readouterr().out)
 
 137         assert out[0]['name'] == 'Name'
 
 138         assert 'address' not in out[0]
 
 139         assert 'extratags' not in out[0]
 
 140         assert 'namedetails' not in out[0]
 
 143 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
 
 144                                               ('search_address', ('--city', 'Berlin'))
 
 146 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
 
 147     result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
 
 148                                napi.Point(1.0, -3.0),
 
 149                                names={'name': 'Name', 'name:fr': 'Nom'},
 
 150                                extratags={'extra': 'Extra'},
 
 153     monkeypatch.setattr(napi.NominatimAPI, endpoint,
 
 154                         lambda *args, **kwargs: napi.SearchResults([result]))
 
 156     result = cli_call('search', '--project-dir', str(tmp_path), *params)
 
 160     out = json.loads(capsys.readouterr().out)
 
 162     assert out[0]['name'] == 'Name'
 
 163     assert 'address' not in out[0]
 
 164     assert 'extratags' not in out[0]
 
 165     assert 'namedetails' not in out[0]