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