]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_api_lookup.py
Merge pull request #3670 from lonvia/flake-for-tests
[nominatim.git] / test / python / api / test_api_lookup.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 lookup API call.
9 """
10 import json
11
12 import pytest
13
14 import nominatim_api as napi
15
16
17 def test_lookup_empty_list(apiobj, frontend):
18     api = frontend(apiobj, options={'details'})
19     assert api.lookup([]) == []
20
21
22 def test_lookup_non_existing(apiobj, frontend):
23     api = frontend(apiobj, options={'details'})
24     assert api.lookup((napi.PlaceID(332), napi.OsmID('W', 4),
25                        napi.OsmID('W', 4, 'highway'))) == []
26
27
28 @pytest.mark.parametrize('idobj', (napi.PlaceID(332), napi.OsmID('W', 4),
29                                    napi.OsmID('W', 4, 'highway')))
30 def test_lookup_single_placex(apiobj, frontend, idobj):
31     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
32                       class_='highway', type='residential',
33                       name={'name': 'Road'}, address={'city': 'Barrow'},
34                       extratags={'surface': 'paved'},
35                       parent_place_id=34, linked_place_id=55,
36                       admin_level=15, country_code='gb',
37                       housenumber='4',
38                       postcode='34425', wikipedia='en:Faa',
39                       rank_search=27, rank_address=26,
40                       importance=0.01,
41                       centroid=(23, 34),
42                       geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
43
44     api = frontend(apiobj, options={'details'})
45     result = api.lookup([idobj])
46
47     assert len(result) == 1
48
49     result = result[0]
50
51     assert result.source_table.name == 'PLACEX'
52     assert result.category == ('highway', 'residential')
53     assert result.centroid == (pytest.approx(23.0), pytest.approx(34.0))
54
55     assert result.place_id == 332
56     assert result.osm_object == ('W', 4)
57
58     assert result.names == {'name': 'Road'}
59     assert result.address == {'city': 'Barrow'}
60     assert result.extratags == {'surface': 'paved'}
61
62     assert result.housenumber == '4'
63     assert result.postcode == '34425'
64     assert result.wikipedia == 'en:Faa'
65
66     assert result.rank_search == 27
67     assert result.rank_address == 26
68     assert result.importance == pytest.approx(0.01)
69
70     assert result.country_code == 'gb'
71
72     assert result.address_rows is None
73     assert result.linked_rows is None
74     assert result.parented_rows is None
75     assert result.name_keywords is None
76     assert result.address_keywords is None
77
78     assert result.geometry == {}
79
80
81 def test_lookup_multiple_places(apiobj, frontend):
82     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
83                       class_='highway', type='residential',
84                       name={'name': 'Road'}, address={'city': 'Barrow'},
85                       extratags={'surface': 'paved'},
86                       parent_place_id=34, linked_place_id=55,
87                       admin_level=15, country_code='gb',
88                       housenumber='4',
89                       postcode='34425', wikipedia='en:Faa',
90                       rank_search=27, rank_address=26,
91                       importance=0.01,
92                       centroid=(23, 34),
93                       geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
94     apiobj.add_osmline(place_id=4924, osm_id=9928,
95                        parent_place_id=12,
96                        startnumber=1, endnumber=4, step=1,
97                        country_code='gb', postcode='34425',
98                        address={'city': 'Big'},
99                        geometry='LINESTRING(23 34, 23 35)')
100
101     api = frontend(apiobj, options={'details'})
102     result = api.lookup((napi.OsmID('W', 1),
103                          napi.OsmID('W', 4),
104                          napi.OsmID('W', 9928)))
105
106     assert len(result) == 2
107
108     assert set(r.place_id for r in result) == {332, 4924}
109
110
111 @pytest.mark.parametrize('gtype', list(napi.GeometryFormat))
112 def test_simple_place_with_geometry(apiobj, frontend, gtype):
113     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
114                       class_='highway', type='residential',
115                       name={'name': 'Road'}, address={'city': 'Barrow'},
116                       extratags={'surface': 'paved'},
117                       parent_place_id=34, linked_place_id=55,
118                       admin_level=15, country_code='gb',
119                       housenumber='4',
120                       postcode='34425', wikipedia='en:Faa',
121                       rank_search=27, rank_address=26,
122                       importance=0.01,
123                       centroid=(23, 34),
124                       geometry='POLYGON((23 34, 23.1 34, 23.1 34.1, 23 34))')
125
126     api = frontend(apiobj, options={'details'})
127     result = api.lookup([napi.OsmID('W', 4)], geometry_output=gtype)
128
129     assert len(result) == 1
130     assert result[0].place_id == 332
131
132     if gtype == napi.GeometryFormat.NONE:
133         assert list(result[0].geometry.keys()) == []
134     else:
135         assert list(result[0].geometry.keys()) == [gtype.name.lower()]
136
137
138 def test_simple_place_with_geometry_simplified(apiobj, frontend):
139     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
140                       class_='highway', type='residential',
141                       name={'name': 'Road'}, address={'city': 'Barrow'},
142                       extratags={'surface': 'paved'},
143                       parent_place_id=34, linked_place_id=55,
144                       admin_level=15, country_code='gb',
145                       housenumber='4',
146                       postcode='34425', wikipedia='en:Faa',
147                       rank_search=27, rank_address=26,
148                       importance=0.01,
149                       centroid=(23, 34),
150                       geometry='POLYGON((23 34, 22.999 34, 23.1 34, 23.1 34.1, 23 34))')
151
152     api = frontend(apiobj, options={'details'})
153     result = api.lookup([napi.OsmID('W', 4)],
154                         geometry_output=napi.GeometryFormat.GEOJSON,
155                         geometry_simplification=0.1)
156
157     assert len(result) == 1
158     assert result[0].place_id == 332
159
160     geom = json.loads(result[0].geometry['geojson'])
161
162     assert geom['type'] == 'Polygon'
163     assert geom['coordinates'] == [[[23, 34], [23.1, 34], [23.1, 34.1], [23, 34]]]