]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / cli / test_cmd_api.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 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.clicmd.api
14 import nominatim.api as napi
15
16
17 class TestCliStatusCall:
18
19     @pytest.fixture(autouse=True)
20     def setup_status_mock(self, monkeypatch):
21         monkeypatch.setattr(napi.NominatimAPI, 'status',
22                             lambda self: napi.StatusResult(200, 'OK'))
23
24
25     def test_status_simple(self, cli_call, tmp_path):
26         result = cli_call('status', '--project-dir', str(tmp_path))
27
28         assert result == 0
29
30
31     def test_status_json_format(self, cli_call, tmp_path, capsys):
32         result = cli_call('status', '--project-dir', str(tmp_path),
33                           '--format', 'json')
34
35         assert result == 0
36
37         json.loads(capsys.readouterr().out)
38
39
40 class TestCliDetailsCall:
41
42     @pytest.fixture(autouse=True)
43     def setup_status_mock(self, monkeypatch):
44         result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
45                                      napi.Point(1.0, -3.0))
46
47         monkeypatch.setattr(napi.NominatimAPI, 'details',
48                             lambda *args, **kwargs: result)
49
50     @pytest.mark.parametrize("params", [('--node', '1'),
51                                         ('--way', '1'),
52                                         ('--relation', '1'),
53                                         ('--place_id', '10001')])
54
55     def test_details_json_format(self, cli_call, tmp_path, capsys, params):
56         result = cli_call('details', '--project-dir', str(tmp_path), *params)
57
58         assert result == 0
59
60         json.loads(capsys.readouterr().out)
61
62
63 class TestCliReverseCall:
64
65     @pytest.fixture(autouse=True)
66     def setup_reverse_mock(self, monkeypatch):
67         result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
68                                     napi.Point(1.0, -3.0),
69                                     names={'name':'Name', 'name:fr': 'Nom'},
70                                     extratags={'extra':'Extra'},
71                                     locale_name='Name',
72                                     display_name='Name')
73
74         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
75                             lambda *args, **kwargs: result)
76
77
78     def test_reverse_simple(self, cli_call, tmp_path, capsys):
79         result = cli_call('reverse', '--project-dir', str(tmp_path),
80                           '--lat', '34', '--lon', '34')
81
82         assert result == 0
83
84         out = json.loads(capsys.readouterr().out)
85         assert out['name'] == 'Name'
86         assert 'address' not in out
87         assert 'extratags' not in out
88         assert 'namedetails' not in out
89
90
91     @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
92                                              ('--extratags', 'extratags'),
93                                              ('--namedetails', 'namedetails')])
94     def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
95         result = cli_call('reverse', '--project-dir', str(tmp_path),
96                           '--lat', '34', '--lon', '34', param)
97
98         assert result == 0
99
100         out = json.loads(capsys.readouterr().out)
101         assert field in out
102
103
104     def test_reverse_format(self, cli_call, tmp_path, capsys):
105         result = cli_call('reverse', '--project-dir', str(tmp_path),
106                           '--lat', '34', '--lon', '34', '--format', 'geojson')
107
108         assert result == 0
109
110         out = json.loads(capsys.readouterr().out)
111         assert out['type'] == 'FeatureCollection'
112
113
114 class TestCliLookupCall:
115
116     @pytest.fixture(autouse=True)
117     def setup_lookup_mock(self, monkeypatch):
118         result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
119                                     napi.Point(1.0, -3.0),
120                                     names={'name':'Name', 'name:fr': 'Nom'},
121                                     extratags={'extra':'Extra'},
122                                     locale_name='Name',
123                                     display_name='Name')
124
125         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
126                             lambda *args, **kwargs: napi.SearchResults([result]))
127
128     def test_lookup_simple(self, cli_call, tmp_path, capsys):
129         result = cli_call('lookup', '--project-dir', str(tmp_path),
130                           '--id', 'N34')
131
132         assert result == 0
133
134         out = json.loads(capsys.readouterr().out)
135         assert len(out) == 1
136         assert out[0]['name'] == 'Name'
137         assert 'address' not in out[0]
138         assert 'extratags' not in out[0]
139         assert 'namedetails' not in out[0]
140
141
142 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
143                                               ('search_address', ('--city', 'Berlin'))
144                                              ])
145 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
146     result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
147                                napi.Point(1.0, -3.0),
148                                names={'name':'Name', 'name:fr': 'Nom'},
149                                extratags={'extra':'Extra'},
150                                locale_name='Name',
151                                display_name='Name')
152
153     monkeypatch.setattr(napi.NominatimAPI, endpoint,
154                         lambda *args, **kwargs: napi.SearchResults([result]))
155
156
157     result = cli_call('search', '--project-dir', str(tmp_path), *params)
158
159     assert result == 0
160
161     out = json.loads(capsys.readouterr().out)
162     assert len(out) == 1
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]