]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_near.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / api / search / test_search_near.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) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for running the near searcher.
9 """
10 import pytest
11
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
18
19
20 def run_search(apiobj, frontend, global_penalty, cat, cat_penalty=None, ccodes=[],
21                details=SearchDetails()):
22
23     class PlaceSearchData:
24         penalty = 0.0
25         postcodes = WeightedStrings([], [])
26         countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
27         housenumbers = WeightedStrings([], [])
28         qualifiers = WeightedStrings([], [])
29         lookups = [FieldLookup('name_vector', [56], LookupAll)]
30         rankings = []
31
32     if ccodes is not None:
33         details.countries = ccodes
34
35     place_search = PlaceSearch(0.0, PlaceSearchData(), 2)
36
37     if cat_penalty is None:
38         cat_penalty = [0.0] * len(cat)
39
40     near_search = NearSearch(0.1, WeightedCategories(cat, cat_penalty), place_search)
41
42     api = frontend(apiobj, options=['search'])
43
44     async def run():
45         async with api._async_api.begin() as conn:
46             return await near_search.lookup(conn, details)
47
48     results = api._loop.run_until_complete(run())
49     results.sort(key=lambda r: r.accuracy)
50
51     return results
52
53
54 def test_no_results_inner_query(apiobj, frontend):
55     assert not run_search(apiobj, frontend, 0.4, [('this', 'that')])
56
57
58 def test_no_appropriate_results_inner_query(apiobj, frontend):
59     apiobj.add_placex(place_id=100, country_code='us',
60                       centroid=(5.6, 4.3),
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',
63                            centroid=(5.6, 4.3))
64     apiobj.add_placex(place_id=22, class_='amenity', type='bank',
65                       centroid=(5.6001, 4.2994))
66
67     assert not run_search(apiobj, frontend, 0.4, [('amenity', 'bank')])
68
69
70 class TestNearSearch:
71
72     @pytest.fixture(autouse=True)
73     def fill_database(self, apiobj):
74         apiobj.add_placex(place_id=100, country_code='us',
75                           centroid=(5.6, 4.3))
76         apiobj.add_search_name(100, names=[56], country_code='us',
77                                centroid=(5.6, 4.3))
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))
82
83
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))
89
90         results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')])
91
92         assert [r.place_id for r in results] == [22]
93
94
95     def test_multiple_types_near_in_placex(self, apiobj, frontend):
96         apiobj.add_placex(place_id=22, class_='amenity', type='bank',
97                           importance=0.002,
98                           centroid=(5.6001, 4.2994))
99         apiobj.add_placex(place_id=23, class_='amenity', type='bench',
100                           importance=0.001,
101                           centroid=(5.6001, 4.2994))
102
103         results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank'),
104                                                      ('amenity', 'bench')])
105
106         assert [r.place_id for r in results] == [22, 23]
107
108
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')
116
117         results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')])
118
119         assert [r.place_id for r in results] == [22]
120
121
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),
126                           country_code='us')
127         apiobj.add_placex(place_id=122, class_='amenity', type='bank',
128                           centroid=(5.6001, 4.2994),
129                           country_code='mx')
130         apiobj.add_placex(place_id=23, class_='amenity', type='bank',
131                           centroid=(-10.3001, 56.9),
132                           country_code='mx')
133         apiobj.add_placex(place_id=123, class_='amenity', type='bank',
134                           centroid=(-10.3001, 56.9),
135                           country_code='us')
136
137         results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')], ccodes=[cc, 'fr'])
138
139         assert [r.place_id for r in results] == [rid]
140
141
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),
146                           country_code='us')
147         apiobj.add_placex(place_id=122, class_='amenity', type='bank',
148                           centroid=(5.6001, 4.2994),
149                           country_code='us')
150
151
152         results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')],
153                              details=SearchDetails(excluded=[excluded]))
154
155         assert [r.place_id for r in results] == [rid]
156
157
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),
163                           country_code='us')
164
165         results = run_search(apiobj, frontend, 0.1, [('amenity', 'bank')],
166                              details=SearchDetails(layers=layer))
167
168         assert [r.place_id for r in results] == rids