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 near searcher.
12 import nominatim.api as napi
13 from nominatim.api.types import SearchDetails
14 from nominatim.api.search.db_searches import NearSearch, PlaceSearch
15 from nominatim.api.search.db_search_fields import WeightedStrings, WeightedCategories,\
16 FieldLookup, FieldRanking, RankedTokens
19 def run_search(apiobj, global_penalty, cat, cat_penalty=None, ccodes=[],
20 details=SearchDetails()):
22 class PlaceSearchData:
24 postcodes = WeightedStrings([], [])
25 countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
26 housenumbers = WeightedStrings([], [])
27 qualifiers = WeightedStrings([], [])
28 lookups = [FieldLookup('name_vector', [56], 'lookup_all')]
31 if ccodes is not None:
32 details.countries = ccodes
34 place_search = PlaceSearch(0.0, PlaceSearchData(), 2)
36 if cat_penalty is None:
37 cat_penalty = [0.0] * len(cat)
39 near_search = NearSearch(0.1, WeightedCategories(cat, cat_penalty), place_search)
42 async with apiobj.api._async_api.begin() as conn:
43 return await near_search.lookup(conn, details)
45 results = apiobj.async_to_sync(run())
46 results.sort(key=lambda r: r.accuracy)
51 def test_no_results_inner_query(apiobj):
52 assert not run_search(apiobj, 0.4, [('this', 'that')])
55 def test_no_appropriate_results_inner_query(apiobj):
56 apiobj.add_placex(place_id=100, country_code='us',
58 geometry='POLYGON((0.0 0.0, 10.0 0.0, 10.0 2.0, 0.0 2.0, 0.0 0.0))')
59 apiobj.add_search_name(100, names=[56], country_code='us',
61 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
62 centroid=(5.6001, 4.2994))
64 assert not run_search(apiobj, 0.4, [('amenity', 'bank')])
69 @pytest.fixture(autouse=True)
70 def fill_database(self, apiobj):
71 apiobj.add_placex(place_id=100, country_code='us',
73 apiobj.add_search_name(100, names=[56], country_code='us',
75 apiobj.add_placex(place_id=101, country_code='mx',
76 centroid=(-10.3, 56.9))
77 apiobj.add_search_name(101, names=[56], country_code='mx',
78 centroid=(-10.3, 56.9))
81 def test_near_in_placex(self, apiobj):
82 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
83 centroid=(5.6001, 4.2994))
84 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
85 centroid=(5.6001, 4.2994))
87 results = run_search(apiobj, 0.1, [('amenity', 'bank')])
89 assert [r.place_id for r in results] == [22]
92 def test_multiple_types_near_in_placex(self, apiobj):
93 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
95 centroid=(5.6001, 4.2994))
96 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
98 centroid=(5.6001, 4.2994))
100 results = run_search(apiobj, 0.1, [('amenity', 'bank'),
101 ('amenity', 'bench')])
103 assert [r.place_id for r in results] == [22, 23]
106 def test_near_in_classtype(self, apiobj):
107 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
108 centroid=(5.6, 4.34))
109 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
110 centroid=(5.6, 4.34))
111 apiobj.add_class_type_table('amenity', 'bank')
112 apiobj.add_class_type_table('amenity', 'bench')
114 results = run_search(apiobj, 0.1, [('amenity', 'bank')])
116 assert [r.place_id for r in results] == [22]
119 @pytest.mark.parametrize('cc,rid', [('us', 22), ('mx', 23)])
120 def test_restrict_by_country(self, apiobj, cc, rid):
121 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
122 centroid=(5.6001, 4.2994),
124 apiobj.add_placex(place_id=122, class_='amenity', type='bank',
125 centroid=(5.6001, 4.2994),
127 apiobj.add_placex(place_id=23, class_='amenity', type='bank',
128 centroid=(-10.3001, 56.9),
130 apiobj.add_placex(place_id=123, class_='amenity', type='bank',
131 centroid=(-10.3001, 56.9),
134 results = run_search(apiobj, 0.1, [('amenity', 'bank')], ccodes=[cc, 'fr'])
136 assert [r.place_id for r in results] == [rid]
139 @pytest.mark.parametrize('excluded,rid', [(22, 122), (122, 22)])
140 def test_exclude_place_by_id(self, apiobj, excluded, rid):
141 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
142 centroid=(5.6001, 4.2994),
144 apiobj.add_placex(place_id=122, class_='amenity', type='bank',
145 centroid=(5.6001, 4.2994),
149 results = run_search(apiobj, 0.1, [('amenity', 'bank')],
150 details=SearchDetails(excluded=[excluded]))
152 assert [r.place_id for r in results] == [rid]
155 @pytest.mark.parametrize('layer,rids', [(napi.DataLayer.POI, [22]),
156 (napi.DataLayer.MANMADE, [])])
157 def test_with_layer(self, apiobj, layer, rids):
158 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
159 centroid=(5.6001, 4.2994),
162 results = run_search(apiobj, 0.1, [('amenity', 'bank')],
163 details=SearchDetails(layers=layer))
165 assert [r.place_id for r in results] == rids