1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for running the generic place searcher.
12 import nominatim.api as napi
13 from nominatim.api.types import SearchDetails
14 from nominatim.api.search.db_searches import PlaceSearch
15 from nominatim.api.search.db_search_fields import WeightedStrings, WeightedCategories,\
16 FieldLookup, FieldRanking, RankedTokens
18 def run_search(apiobj, global_penalty, lookup, ranking, count=2,
19 hnrs=[], pcs=[], ccodes=[], quals=[],
20 details=SearchDetails()):
22 penalty = global_penalty
23 postcodes = WeightedStrings(pcs, [0.0] * len(pcs))
24 countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
25 housenumbers = WeightedStrings(hnrs, [0.0] * len(hnrs))
26 qualifiers = WeightedCategories(quals, [0.0] * len(quals))
30 search = PlaceSearch(0.0, MySearchData(), count)
33 async with apiobj.api._async_api.begin() as conn:
34 return await search.lookup(conn, details)
36 results = apiobj.async_to_sync(run())
37 results.sort(key=lambda r: r.accuracy)
42 class TestNameOnlySearches:
44 @pytest.fixture(autouse=True)
45 def fill_database(self, apiobj):
46 apiobj.add_placex(place_id=100, country_code='us',
48 apiobj.add_search_name(100, names=[1,2,10,11], country_code='us',
50 apiobj.add_placex(place_id=101, country_code='mx',
51 centroid=(-10.3, 56.9))
52 apiobj.add_search_name(101, names=[1,2,20,21], country_code='mx',
53 centroid=(-10.3, 56.9))
56 @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict'])
57 @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
59 def test_lookup_all_match(self, apiobj, lookup_type, rank, res):
60 lookup = FieldLookup('name_vector', [1,2], lookup_type)
61 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
63 results = run_search(apiobj, 0.1, [lookup], [ranking])
65 assert [r.place_id for r in results] == res
68 @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict'])
69 def test_lookup_all_partial_match(self, apiobj, lookup_type):
70 lookup = FieldLookup('name_vector', [1,20], lookup_type)
71 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
73 results = run_search(apiobj, 0.1, [lookup], [ranking])
75 assert len(results) == 1
76 assert results[0].place_id == 101
78 @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
80 def test_lookup_any_match(self, apiobj, rank, res):
81 lookup = FieldLookup('name_vector', [11,21], 'lookup_any')
82 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
84 results = run_search(apiobj, 0.1, [lookup], [ranking])
86 assert [r.place_id for r in results] == res
89 def test_lookup_any_partial_match(self, apiobj):
90 lookup = FieldLookup('name_vector', [20], 'lookup_all')
91 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
93 results = run_search(apiobj, 0.1, [lookup], [ranking])
95 assert len(results) == 1
96 assert results[0].place_id == 101
99 @pytest.mark.parametrize('cc,res', [('us', 100), ('mx', 101)])
100 def test_lookup_restrict_country(self, apiobj, cc, res):
101 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
102 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
104 results = run_search(apiobj, 0.1, [lookup], [ranking], ccodes=[cc])
106 assert [r.place_id for r in results] == [res]
109 def test_lookup_restrict_placeid(self, apiobj):
110 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
111 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
113 results = run_search(apiobj, 0.1, [lookup], [ranking],
114 details=SearchDetails(excluded=[101]))
116 assert [r.place_id for r in results] == [100]
119 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
120 napi.GeometryFormat.KML,
121 napi.GeometryFormat.SVG,
122 napi.GeometryFormat.TEXT])
123 def test_return_geometries(self, apiobj, geom):
124 lookup = FieldLookup('name_vector', [20], 'lookup_all')
125 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
127 results = run_search(apiobj, 0.1, [lookup], [ranking],
128 details=SearchDetails(geometry_output=geom))
130 assert geom.name.lower() in results[0].geometry
133 @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.7,4.0,6.0,5.0'])
134 def test_prefer_viewbox(self, apiobj, viewbox):
135 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
136 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
138 results = run_search(apiobj, 0.1, [lookup], [ranking])
139 assert [r.place_id for r in results] == [101, 100]
141 results = run_search(apiobj, 0.1, [lookup], [ranking],
142 details=SearchDetails.from_kwargs({'viewbox': viewbox}))
143 assert [r.place_id for r in results] == [100, 101]
146 def test_force_viewbox(self, apiobj):
147 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
149 details=SearchDetails.from_kwargs({'viewbox': '5.0,4.0,6.0,5.0',
150 'bounded_viewbox': True})
152 results = run_search(apiobj, 0.1, [lookup], [], details=details)
153 assert [r.place_id for r in results] == [100]
156 def test_prefer_near(self, apiobj):
157 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
158 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
160 results = run_search(apiobj, 0.1, [lookup], [ranking])
161 assert [r.place_id for r in results] == [101, 100]
163 results = run_search(apiobj, 0.1, [lookup], [ranking],
164 details=SearchDetails.from_kwargs({'near': '5.6,4.3'}))
165 results.sort(key=lambda r: -r.importance)
166 assert [r.place_id for r in results] == [100, 101]
169 def test_force_near(self, apiobj):
170 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
172 details=SearchDetails.from_kwargs({'near': '5.6,4.3',
173 'near_radius': 0.11})
175 results = run_search(apiobj, 0.1, [lookup], [], details=details)
177 assert [r.place_id for r in results] == [100]
180 class TestStreetWithHousenumber:
182 @pytest.fixture(autouse=True)
183 def fill_database(self, apiobj):
184 apiobj.add_placex(place_id=1, class_='place', type='house',
185 parent_place_id=1000,
186 housenumber='20 a', country_code='es')
187 apiobj.add_placex(place_id=2, class_='place', type='house',
188 parent_place_id=1000,
189 housenumber='21;22', country_code='es')
190 apiobj.add_placex(place_id=1000, class_='highway', type='residential',
191 rank_search=26, rank_address=26,
193 apiobj.add_search_name(1000, names=[1,2,10,11],
194 search_rank=26, address_rank=26,
196 apiobj.add_placex(place_id=91, class_='place', type='house',
197 parent_place_id=2000,
198 housenumber='20', country_code='pt')
199 apiobj.add_placex(place_id=92, class_='place', type='house',
200 parent_place_id=2000,
201 housenumber='22', country_code='pt')
202 apiobj.add_placex(place_id=93, class_='place', type='house',
203 parent_place_id=2000,
204 housenumber='24', country_code='pt')
205 apiobj.add_placex(place_id=2000, class_='highway', type='residential',
206 rank_search=26, rank_address=26,
208 apiobj.add_search_name(2000, names=[1,2,20,21],
209 search_rank=26, address_rank=26,
213 @pytest.mark.parametrize('hnr,res', [('20', [91, 1]), ('20 a', [1]),
214 ('21', [2]), ('22', [2, 92]),
215 ('24', [93]), ('25', [])])
216 def test_lookup_by_single_housenumber(self, apiobj, hnr, res):
217 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
218 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
220 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=[hnr])
222 assert [r.place_id for r in results] == res + [1000, 2000]
225 @pytest.mark.parametrize('cc,res', [('es', [2, 1000]), ('pt', [92, 2000])])
226 def test_lookup_with_country_restriction(self, apiobj, cc, res):
227 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
228 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
230 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
233 assert [r.place_id for r in results] == res
236 def test_lookup_exclude_housenumber_placeid(self, apiobj):
237 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
238 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
240 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
241 details=SearchDetails(excluded=[92]))
243 assert [r.place_id for r in results] == [2, 1000, 2000]
246 def test_lookup_exclude_street_placeid(self, apiobj):
247 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
248 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
250 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
251 details=SearchDetails(excluded=[1000]))
253 assert [r.place_id for r in results] == [2, 92, 2000]
256 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
257 napi.GeometryFormat.KML,
258 napi.GeometryFormat.SVG,
259 napi.GeometryFormat.TEXT])
260 def test_return_geometries(self, apiobj, geom):
261 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
263 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['20', '21', '22'],
264 details=SearchDetails(geometry_output=geom))
267 assert all(geom.name.lower() in r.geometry for r in results)
270 class TestInterpolations:
272 @pytest.fixture(autouse=True)
273 def fill_database(self, apiobj):
274 apiobj.add_placex(place_id=990, class_='highway', type='service',
275 rank_search=27, rank_address=27,
276 centroid=(10.0, 10.0),
277 geometry='LINESTRING(9.995 10, 10.005 10)')
278 apiobj.add_search_name(990, names=[111],
279 search_rank=27, address_rank=27)
280 apiobj.add_placex(place_id=991, class_='place', type='house',
282 rank_search=30, rank_address=30,
284 centroid=(10.0, 10.00002))
285 apiobj.add_osmline(place_id=992,
287 startnumber=21, endnumber=29, step=2,
288 centroid=(10.0, 10.00001),
289 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
292 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
293 def test_lookup_housenumber(self, apiobj, hnr, res):
294 lookup = FieldLookup('name_vector', [111], 'lookup_all')
296 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
298 assert [r.place_id for r in results] == res + [990]
303 @pytest.fixture(autouse=True)
304 def fill_database(self, apiobj):
305 apiobj.add_placex(place_id=990, class_='highway', type='service',
306 rank_search=27, rank_address=27,
308 centroid=(10.0, 10.0),
309 geometry='LINESTRING(9.995 10, 10.005 10)')
310 apiobj.add_search_name(990, names=[111], country_code='us',
311 search_rank=27, address_rank=27)
312 apiobj.add_placex(place_id=991, class_='place', type='house',
314 rank_search=30, rank_address=30,
317 centroid=(10.0, 10.00002))
318 apiobj.add_tiger(place_id=992,
320 startnumber=21, endnumber=29, step=2,
321 centroid=(10.0, 10.00001),
322 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
325 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
326 def test_lookup_housenumber(self, apiobj, hnr, res):
327 lookup = FieldLookup('name_vector', [111], 'lookup_all')
329 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
331 assert [r.place_id for r in results] == res + [990]
334 class TestLayersRank30:
336 @pytest.fixture(autouse=True)
337 def fill_database(self, apiobj):
338 apiobj.add_placex(place_id=223, class_='place', type='house',
342 apiobj.add_search_name(223, names=[34],
344 address_rank=30, search_rank=30)
345 apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
348 apiobj.add_search_name(224, names=[34],
350 address_rank=30, search_rank=30)
351 apiobj.add_placex(place_id=225, class_='man_made', type='tower',
354 apiobj.add_search_name(225, names=[34],
356 address_rank=0, search_rank=30)
357 apiobj.add_placex(place_id=226, class_='railway', type='station',
360 apiobj.add_search_name(226, names=[34],
362 address_rank=0, search_rank=30)
363 apiobj.add_placex(place_id=227, class_='natural', type='cave',
366 apiobj.add_search_name(227, names=[34],
368 address_rank=0, search_rank=30)
371 @pytest.mark.parametrize('layer,res', [(napi.DataLayer.ADDRESS, [223]),
372 (napi.DataLayer.POI, [224]),
373 (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
374 (napi.DataLayer.MANMADE, [225]),
375 (napi.DataLayer.RAILWAY, [226]),
376 (napi.DataLayer.NATURAL, [227]),
377 (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
378 (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
379 def test_layers_rank30(self, apiobj, layer, res):
380 lookup = FieldLookup('name_vector', [34], 'lookup_any')
382 results = run_search(apiobj, 0.1, [lookup], [],
383 details=SearchDetails(layers=layer))
385 assert [r.place_id for r in results] == res