]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
Locales and localization refactor with Locales as a localizer object.
[nominatim.git] / test / python / cli / test_cmd_api.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for API access commands of command-line interface wrapper.
9 """
10 import json
11 import pytest
12
13 import nominatim_api as napi
14
15
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')
19
20
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')
24
25
26 class TestCliStatusCall:
27
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'))
32
33     def test_status_simple(self, cli_call, tmp_path):
34         result = cli_call('status', '--project-dir', str(tmp_path))
35
36         assert result == 0
37
38     def test_status_json_format(self, cli_call, tmp_path, capsys):
39         result = cli_call('status', '--project-dir', str(tmp_path),
40                           '--format', 'json')
41
42         assert result == 0
43
44         json.loads(capsys.readouterr().out)
45
46
47 class TestCliDetailsCall:
48
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))
53
54         monkeypatch.setattr(napi.NominatimAPI, 'details',
55                             lambda *args, **kwargs: result)
56
57     @pytest.mark.parametrize("params", [('--node', '1'),
58                                         ('--way', '1'),
59                                         ('--relation', '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)
63
64         assert result == 0
65
66         json.loads(capsys.readouterr().out)
67
68
69 class TestCliReverseCall:
70
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'},
77                                     locale_name='Name')
78
79         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
80                             lambda *args, **kwargs: result)
81
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')
85
86         assert result == 0
87
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
93
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)
100
101         assert result == 0
102
103         out = json.loads(capsys.readouterr().out)
104         assert field in out
105
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')
109
110         assert result == 0
111
112         out = json.loads(capsys.readouterr().out)
113         assert out['type'] == 'FeatureCollection'
114
115
116 class TestCliLookupCall:
117
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'},
124                                    locale_name='Name')
125
126         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
127                             lambda *args, **kwargs: napi.SearchResults([result]))
128
129     def test_lookup_simple(self, cli_call, tmp_path, capsys):
130         result = cli_call('lookup', '--project-dir', str(tmp_path),
131                           '--id', 'N34')
132
133         assert result == 0
134
135         out = json.loads(capsys.readouterr().out)
136         assert len(out) == 1
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]
141
142
143 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
144                                               ('search_address', ('--city', 'Berlin'))
145                                               ])
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'},
151                                locale_name='Name')
152
153     monkeypatch.setattr(napi.NominatimAPI, endpoint,
154                         lambda *args, **kwargs: napi.SearchResults([result]))
155
156     result = cli_call('search', '--project-dir', str(tmp_path), *params)
157
158     assert result == 0
159
160     out = json.loads(capsys.readouterr().out)
161     assert len(out) == 1
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]