1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 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
17 from nominatim_api.search.db_search_lookups import LookupAll
20 def run_search(apiobj, frontend, global_penalty, cat, cat_penalty=None, ccodes=[],
21 details=SearchDetails()):
23 class PlaceSearchData:
25 postcodes = WeightedStrings([], [])
26 countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
27 housenumbers = WeightedStrings([], [])
28 qualifiers = WeightedStrings([], [])
29 lookups = [FieldLookup('name_vector', [56], LookupAll)]
32 if ccodes is not None:
33 details.countries = ccodes
35 place_search = PlaceSearch(0.0, PlaceSearchData(), 2)
37 if cat_penalty is None:
38 cat_penalty = [0.0] * len(cat)
40 near_search = NearSearch(0.1, WeightedCategories(cat, cat_penalty), place_search)
42 api = frontend(apiobj, options=['search'])
45 async with api._async_api.begin() as conn:
46 return await near_search.lookup(conn, details)
48 results = api._loop.run_until_complete(run())
49 results.sort(key=lambda r: r.accuracy)
54 def test_no_results_inner_query(apiobj, frontend):
55 assert not run_search(apiobj, frontend, 0.4, [('this', 'that')])
58 def test_no_appropriate_results_inner_query(apiobj, frontend):
59 apiobj.add_placex(place_id=100, country_code='us',
61 geometry='POLYGON((0.0 0.0, 10.0 0.0, 10.0 2.0, 0.0 2.0, 0.0 0.0))')
62 apiobj.add_search_name(100, names=[56], country_code='us',
64 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
65 centroid=(5.6001, 4.2994))
67 assert not run_search(apiobj, frontend, 0.4, [('amenity', 'bank')])
72 @pytest.fixture(autouse=True)
73 def fill_database(self, apiobj):
74 apiobj.add_placex(place_id=100, country_code='us',
76 apiobj.add_search_name(100, names=[56], country_code='us',
78 apiobj.add_placex(place_id=101, country_code='mx',
79 centroid=(-10.3, 56.9))
80 apiobj.add_search_name(101, names=[56], country_code='mx',
81 centroid=(-10.3, 56.9))
84 def test_near_in_placex(self, apiobj, frontend):
85 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
86 centroid=(5.6001, 4.2994))
87 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
88 centroid=(5.6001, 4.2994))
90 results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')])
92 assert [r.place_id for r in results] == [22]
95 def test_multiple_types_near_in_placex(self, apiobj, frontend):
96 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
98 centroid=(5.6001, 4.2994))
99 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
101 centroid=(5.6001, 4.2994))
103 results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank'),
104 ('amenity', 'bench')])
106 assert [r.place_id for r in results] == [22, 23]
109 def test_near_in_classtype(self, apiobj, frontend):
110 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
111 centroid=(5.6, 4.34))
112 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
113 centroid=(5.6, 4.34))
114 apiobj.add_class_type_table('amenity', 'bank')
115 apiobj.add_class_type_table('amenity', 'bench')
117 results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')])
119 assert [r.place_id for r in results] == [22]
122 @pytest.mark.parametrize('cc,rid', [('us', 22), ('mx', 23)])
123 def test_restrict_by_country(self, apiobj, frontend, cc, rid):
124 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
125 centroid=(5.6001, 4.2994),
127 apiobj.add_placex(place_id=122, class_='amenity', type='bank',
128 centroid=(5.6001, 4.2994),
130 apiobj.add_placex(place_id=23, class_='amenity', type='bank',
131 centroid=(-10.3001, 56.9),
133 apiobj.add_placex(place_id=123, class_='amenity', type='bank',
134 centroid=(-10.3001, 56.9),
137 results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')], ccodes=[cc, 'fr'])
139 assert [r.place_id for r in results] == [rid]
142 @pytest.mark.parametrize('excluded,rid', [(22, 122), (122, 22)])
143 def test_exclude_place_by_id(self, apiobj, frontend, excluded, rid):
144 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
145 centroid=(5.6001, 4.2994),
147 apiobj.add_placex(place_id=122, class_='amenity', type='bank',
148 centroid=(5.6001, 4.2994),
152 results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')],
153 details=SearchDetails(excluded=[excluded]))
155 assert [r.place_id for r in results] == [rid]
158 @pytest.mark.parametrize('layer,rids', [(napi.DataLayer.POI, [22]),
159 (napi.DataLayer.MANMADE, [])])
160 def test_with_layer(self, apiobj, frontend, layer, rids):
161 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
162 centroid=(5.6001, 4.2994),
165 results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')],
166 details=SearchDetails(layers=layer))
168 assert [r.place_id for r in results] == rids