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 def test_very_large_housenumber(apiobj):
271 apiobj.add_placex(place_id=93, class_='place', type='house',
272 parent_place_id=2000,
273 housenumber='2467463524544', country_code='pt')
274 apiobj.add_placex(place_id=2000, class_='highway', type='residential',
275 rank_search=26, rank_address=26,
277 apiobj.add_search_name(2000, names=[1,2],
278 search_rank=26, address_rank=26,
281 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
283 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['2467463524544'],
284 details=SearchDetails())
287 assert [r.place_id for r in results] == [93, 2000]
290 class TestInterpolations:
292 @pytest.fixture(autouse=True)
293 def fill_database(self, apiobj):
294 apiobj.add_placex(place_id=990, class_='highway', type='service',
295 rank_search=27, rank_address=27,
296 centroid=(10.0, 10.0),
297 geometry='LINESTRING(9.995 10, 10.005 10)')
298 apiobj.add_search_name(990, names=[111],
299 search_rank=27, address_rank=27)
300 apiobj.add_placex(place_id=991, class_='place', type='house',
302 rank_search=30, rank_address=30,
304 centroid=(10.0, 10.00002))
305 apiobj.add_osmline(place_id=992,
307 startnumber=21, endnumber=29, step=2,
308 centroid=(10.0, 10.00001),
309 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
312 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
313 def test_lookup_housenumber(self, apiobj, hnr, res):
314 lookup = FieldLookup('name_vector', [111], 'lookup_all')
316 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
318 assert [r.place_id for r in results] == res + [990]
323 @pytest.fixture(autouse=True)
324 def fill_database(self, apiobj):
325 apiobj.add_placex(place_id=990, class_='highway', type='service',
326 rank_search=27, rank_address=27,
328 centroid=(10.0, 10.0),
329 geometry='LINESTRING(9.995 10, 10.005 10)')
330 apiobj.add_search_name(990, names=[111], country_code='us',
331 search_rank=27, address_rank=27)
332 apiobj.add_placex(place_id=991, class_='place', type='house',
334 rank_search=30, rank_address=30,
337 centroid=(10.0, 10.00002))
338 apiobj.add_tiger(place_id=992,
340 startnumber=21, endnumber=29, step=2,
341 centroid=(10.0, 10.00001),
342 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
345 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
346 def test_lookup_housenumber(self, apiobj, hnr, res):
347 lookup = FieldLookup('name_vector', [111], 'lookup_all')
349 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
351 assert [r.place_id for r in results] == res + [990]
354 class TestLayersRank30:
356 @pytest.fixture(autouse=True)
357 def fill_database(self, apiobj):
358 apiobj.add_placex(place_id=223, class_='place', type='house',
362 apiobj.add_search_name(223, names=[34],
364 address_rank=30, search_rank=30)
365 apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
368 apiobj.add_search_name(224, names=[34],
370 address_rank=30, search_rank=30)
371 apiobj.add_placex(place_id=225, class_='man_made', type='tower',
374 apiobj.add_search_name(225, names=[34],
376 address_rank=0, search_rank=30)
377 apiobj.add_placex(place_id=226, class_='railway', type='station',
380 apiobj.add_search_name(226, names=[34],
382 address_rank=0, search_rank=30)
383 apiobj.add_placex(place_id=227, class_='natural', type='cave',
386 apiobj.add_search_name(227, names=[34],
388 address_rank=0, search_rank=30)
391 @pytest.mark.parametrize('layer,res', [(napi.DataLayer.ADDRESS, [223]),
392 (napi.DataLayer.POI, [224]),
393 (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
394 (napi.DataLayer.MANMADE, [225]),
395 (napi.DataLayer.RAILWAY, [226]),
396 (napi.DataLayer.NATURAL, [227]),
397 (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
398 (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
399 def test_layers_rank30(self, apiobj, layer, res):
400 lookup = FieldLookup('name_vector', [34], 'lookup_any')
402 results = run_search(apiobj, 0.1, [lookup], [],
403 details=SearchDetails(layers=layer))
405 assert [r.place_id for r in results] == res