]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
allow None and str for project_dir in NominatimAPI init
[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) 2024 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_db.clicmd.api
14 import nominatim_api as napi
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
34     def test_status_simple(self, cli_call, tmp_path):
35         result = cli_call('status', '--project-dir', str(tmp_path))
36
37         assert result == 0
38
39
40     def test_status_json_format(self, cli_call, tmp_path, capsys):
41         result = cli_call('status', '--project-dir', str(tmp_path),
42                           '--format', 'json')
43
44         assert result == 0
45
46         json.loads(capsys.readouterr().out)
47
48
49 class TestCliDetailsCall:
50
51     @pytest.fixture(autouse=True)
52     def setup_status_mock(self, monkeypatch):
53         result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
54                                      napi.Point(1.0, -3.0))
55
56         monkeypatch.setattr(napi.NominatimAPI, 'details',
57                             lambda *args, **kwargs: result)
58
59     @pytest.mark.parametrize("params", [('--node', '1'),
60                                         ('--way', '1'),
61                                         ('--relation', '1'),
62                                         ('--place_id', '10001')])
63
64     def test_details_json_format(self, cli_call, tmp_path, capsys, params):
65         result = cli_call('details', '--project-dir', str(tmp_path), *params)
66
67         assert result == 0
68
69         json.loads(capsys.readouterr().out)
70
71
72 class TestCliReverseCall:
73
74     @pytest.fixture(autouse=True)
75     def setup_reverse_mock(self, monkeypatch):
76         result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
77                                     napi.Point(1.0, -3.0),
78                                     names={'name':'Name', 'name:fr': 'Nom'},
79                                     extratags={'extra':'Extra'},
80                                     locale_name='Name',
81                                     display_name='Name')
82
83         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
84                             lambda *args, **kwargs: result)
85
86
87     def test_reverse_simple(self, cli_call, tmp_path, capsys):
88         result = cli_call('reverse', '--project-dir', str(tmp_path),
89                           '--lat', '34', '--lon', '34')
90
91         assert result == 0
92
93         out = json.loads(capsys.readouterr().out)
94         assert out['name'] == 'Name'
95         assert 'address' not in out
96         assert 'extratags' not in out
97         assert 'namedetails' not in out
98
99
100     @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
101                                              ('--extratags', 'extratags'),
102                                              ('--namedetails', 'namedetails')])
103     def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
104         result = cli_call('reverse', '--project-dir', str(tmp_path),
105                           '--lat', '34', '--lon', '34', param)
106
107         assert result == 0
108
109         out = json.loads(capsys.readouterr().out)
110         assert field in out
111
112
113     def test_reverse_format(self, cli_call, tmp_path, capsys):
114         result = cli_call('reverse', '--project-dir', str(tmp_path),
115                           '--lat', '34', '--lon', '34', '--format', 'geojson')
116
117         assert result == 0
118
119         out = json.loads(capsys.readouterr().out)
120         assert out['type'] == 'FeatureCollection'
121
122
123 class TestCliLookupCall:
124
125     @pytest.fixture(autouse=True)
126     def setup_lookup_mock(self, monkeypatch):
127         result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
128                                     napi.Point(1.0, -3.0),
129                                     names={'name':'Name', 'name:fr': 'Nom'},
130                                     extratags={'extra':'Extra'},
131                                     locale_name='Name',
132                                     display_name='Name')
133
134         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
135                             lambda *args, **kwargs: napi.SearchResults([result]))
136
137     def test_lookup_simple(self, cli_call, tmp_path, capsys):
138         result = cli_call('lookup', '--project-dir', str(tmp_path),
139                           '--id', 'N34')
140
141         assert result == 0
142
143         out = json.loads(capsys.readouterr().out)
144         assert len(out) == 1
145         assert out[0]['name'] == 'Name'
146         assert 'address' not in out[0]
147         assert 'extratags' not in out[0]
148         assert 'namedetails' not in out[0]
149
150
151 @pytest.mark.parametrize('endpoint, params', [('search', ('--query', 'Berlin')),
152                                               ('search_address', ('--city', 'Berlin'))
153                                              ])
154 def test_search(cli_call, tmp_path, capsys, monkeypatch, endpoint, params):
155     result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
156                                napi.Point(1.0, -3.0),
157                                names={'name':'Name', 'name:fr': 'Nom'},
158                                extratags={'extra':'Extra'},
159                                locale_name='Name',
160                                display_name='Name')
161
162     monkeypatch.setattr(napi.NominatimAPI, endpoint,
163                         lambda *args, **kwargs: napi.SearchResults([result]))
164
165
166     result = cli_call('search', '--project-dir', str(tmp_path), *params)
167
168     assert result == 0
169
170     out = json.loads(capsys.readouterr().out)
171     assert len(out) == 1
172     assert out[0]['name'] == 'Name'
173     assert 'address' not in out[0]
174     assert 'extratags' not in out[0]
175     assert 'namedetails' not in out[0]