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
14 from nominatim_api.types import EntranceDetails
17 @pytest.mark.parametrize('call', ['search', 'reverse', 'lookup', 'details', 'status'])
18 def test_list_format(cli_call, call):
19 assert 0 == cli_call(call, '--list-formats')
22 @pytest.mark.parametrize('call', ['search', 'reverse', 'lookup', 'details', 'status'])
23 def test_bad_format(cli_call, call):
24 assert 1 == cli_call(call, '--format', 'rsdfsdfsdfsaefsdfsd')
27 class TestCliStatusCall:
29 @pytest.fixture(autouse=True)
30 def setup_status_mock(self, monkeypatch):
31 monkeypatch.setattr(napi.NominatimAPI, 'status',
32 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))
39 def test_status_json_format(self, cli_call, tmp_path, capsys):
40 result = cli_call('status', '--project-dir', str(tmp_path),
45 json.loads(capsys.readouterr().out)
48 class TestCliDetailsCall:
50 @pytest.fixture(autouse=True)
51 def setup_status_mock(self, monkeypatch):
52 result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
53 napi.Point(1.0, -3.0))
55 monkeypatch.setattr(napi.NominatimAPI, 'details',
56 lambda *args, **kwargs: result)
58 @pytest.mark.parametrize("params", [('--node', '1'),
61 ('--place_id', '10001')])
62 def test_details_json_format(self, cli_call, tmp_path, capsys, params):
63 result = cli_call('details', '--project-dir', str(tmp_path), *params)
67 json.loads(capsys.readouterr().out)
70 class TestCliReverseCall:
72 @pytest.fixture(autouse=True)
73 def setup_reverse_mock(self, monkeypatch):
74 result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
75 napi.Point(1.0, -3.0),
76 names={'name': 'Name', 'name:fr': 'Nom'},
77 extratags={'extra': 'Extra'},
80 monkeypatch.setattr(napi.NominatimAPI, 'reverse',
81 lambda *args, **kwargs: result)
83 def test_reverse_simple(self, cli_call, tmp_path, capsys):
84 result = cli_call('reverse', '--project-dir', str(tmp_path),
85 '--lat', '34', '--lon', '34')
89 out = json.loads(capsys.readouterr().out)
90 assert out['name'] == 'Name'
91 assert 'address' not in out
92 assert 'extratags' not in out
93 assert 'namedetails' not in out
95 @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
96 ('--extratags', 'extratags'),
97 ('--namedetails', 'namedetails')])
98 def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
99 result = cli_call('reverse', '--project-dir', str(tmp_path),
100 '--lat', '34', '--lon', '34', param)
104 out = json.loads(capsys.readouterr().out)
107 def test_reverse_format(self, cli_call, tmp_path, capsys):
108 result = cli_call('reverse', '--project-dir', str(tmp_path),
109 '--lat', '34', '--lon', '34', '--format', 'geojson')
113 out = json.loads(capsys.readouterr().out)
114 assert out['type'] == 'FeatureCollection'
117 class TestCliLookupCall:
119 @pytest.fixture(autouse=True)
120 def setup_lookup_mock(self, monkeypatch):
121 result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
122 napi.Point(1.0, -3.0),
123 names={'name': 'Name', 'name:fr': 'Nom'},
124 extratags={'extra': 'Extra'},
127 monkeypatch.setattr(napi.NominatimAPI, 'lookup',
128 lambda *args, **kwargs: napi.SearchResults([result]))
130 def test_lookup_simple(self, cli_call, tmp_path, capsys):
131 result = cli_call('lookup', '--project-dir', str(tmp_path),
136 out = json.loads(capsys.readouterr().out)
138 assert out[0]['name'] == 'Name'
139 assert 'address' not in out[0]
140 assert 'extratags' not in out[0]
141 assert 'namedetails' not in out[0]
144 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
145 ('search_address', ('--city', 'Berlin'))
147 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
148 result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
149 napi.Point(1.0, -3.0),
150 names={'name': 'Name', 'name:fr': 'Nom'},
151 extratags={'extra': 'Extra'},
154 monkeypatch.setattr(napi.NominatimAPI, endpoint,
155 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]
169 @pytest.mark.parametrize('call, params, rtype',
170 [('search', ('--query', 'Berlin'), napi.SearchResult),
171 ('reverse', ('--lat', '34', '--lon', '34'), napi.ReverseResult),
172 ('lookup', ('--id', 'N34'), napi.SearchResult),
173 ('details', ('--node', '34'), napi.DetailedResult)])
174 def test_entrances_parameter(cli_call, tmp_path, capsys, monkeypatch, call, params, rtype):
175 result = rtype(napi.SourceTable.PLACEX, ('place', 'thing'), napi.Point(1.0, -3.0))
177 def _api_call(*args, entrances=False, **kwargs):
178 # Like the real API, only add entrances when they were requested.
180 result.entrances = [EntranceDetails(45, 'main', napi.Point(1.0, -3.0), {})]
181 return napi.SearchResults([result]) if rtype is napi.SearchResult else result
183 monkeypatch.setattr(napi.NominatimAPI, call, _api_call)
185 assert cli_call(call, '--project-dir', str(tmp_path), *params, '--entrances') == 0
186 assert '"osm_id": 45' in capsys.readouterr().out