]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
enable flake for Python tests
[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                                     display_name='Name')
79
80         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
81                             lambda *args, **kwargs: result)
82
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')
86
87         assert result == 0
88
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
94
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)
101
102         assert result == 0
103
104         out = json.loads(capsys.readouterr().out)
105         assert field in out
106
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')
110
111         assert result == 0
112
113         out = json.loads(capsys.readouterr().out)
114         assert out['type'] == 'FeatureCollection'
115
116
117 class TestCliLookupCall:
118
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'},
125                                    locale_name='Name',
126                                    display_name='Name')
127
128         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
129                             lambda *args, **kwargs: napi.SearchResults([result]))
130
131     def test_lookup_simple(self, cli_call, tmp_path, capsys):
132         result = cli_call('lookup', '--project-dir', str(tmp_path),
133                           '--id', 'N34')
134
135         assert result == 0
136
137         out = json.loads(capsys.readouterr().out)
138         assert len(out) == 1
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]
143
144
145 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
146                                               ('search_address', ('--city', 'Berlin'))
147                                               ])
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'},
153                                locale_name='Name',
154                                display_name='Name')
155
156     monkeypatch.setattr(napi.NominatimAPI, endpoint,
157                         lambda *args, **kwargs: napi.SearchResults([result]))
158
159     result = cli_call('search', '--project-dir', str(tmp_path), *params)
160
161     assert result == 0
162
163     out = json.loads(capsys.readouterr().out)
164     assert len(out) == 1
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]