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'},
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'},
128 monkeypatch.setattr(napi.NominatimAPI, 'lookup',
129 lambda *args, **kwargs: napi.SearchResults([result]))
131 def test_lookup_simple(self, cli_call, tmp_path, capsys):
132 result = cli_call('lookup', '--project-dir', str(tmp_path),
137 out = json.loads(capsys.readouterr().out)
139 assert out[0]['name'] == 'Name'
140 assert 'address' not in out[0]
141 assert 'extratags' not in out[0]
142 assert 'namedetails' not in out[0]
145 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
146 ('search_address', ('--city', 'Berlin'))
148 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
149 result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
150 napi.Point(1.0, -3.0),
151 names={'name': 'Name', 'name:fr': 'Nom'},
152 extratags={'extra': 'Extra'},
156 monkeypatch.setattr(napi.NominatimAPI, endpoint,
157 lambda *args, **kwargs: napi.SearchResults([result]))
159 result = cli_call('search', '--project-dir', str(tmp_path), *params)
163 out = json.loads(capsys.readouterr().out)
165 assert out[0]['name'] == 'Name'
166 assert 'address' not in out[0]
167 assert 'extratags' not in out[0]
168 assert 'namedetails' not in out[0]