]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_near.py
enable connection pools for sqlite
[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) 2023 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
18
19 def run_search(apiobj, global_penalty, cat, cat_penalty=None, ccodes=[],
20                details=SearchDetails()):
21
22     class PlaceSearchData:
23         penalty = 0.0
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')]
29         rankings = []
30
31     if ccodes is not None:
32         details.countries = ccodes
33
34     place_search = PlaceSearch(0.0, PlaceSearchData(), 2)
35
36     if cat_penalty is None:
37         cat_penalty = [0.0] * len(cat)
38
39     near_search = NearSearch(0.1, WeightedCategories(cat, cat_penalty), place_search)
40
41     async def run():
42         async with apiobj.api._async_api.begin() as conn:
43             return await near_search.lookup(conn, details)
44
45     results = apiobj.async_to_sync(run())
46     results.sort(key=lambda r: r.accuracy)
47
48     return results
49
50
51 def test_no_results_inner_query(apiobj):
52     assert not run_search(apiobj, 0.4, [('this', 'that')])
53
54
55 def test_no_appropriate_results_inner_query(apiobj):
56     apiobj.add_placex(place_id=100, country_code='us',
57                       centroid=(5.6, 4.3),
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',
60                            centroid=(5.6, 4.3))
61     apiobj.add_placex(place_id=22, class_='amenity', type='bank',
62                       centroid=(5.6001, 4.2994))
63
64     assert not run_search(apiobj, 0.4, [('amenity', 'bank')])
65
66
67 class TestNearSearch:
68
69     @pytest.fixture(autouse=True)
70     def fill_database(self, apiobj):
71         apiobj.add_placex(place_id=100, country_code='us',
72                           centroid=(5.6, 4.3))
73         apiobj.add_search_name(100, names=[56], country_code='us',
74                                centroid=(5.6, 4.3))
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))
79
80
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))
86
87         results = run_search(apiobj, 0.1, [('amenity', 'bank')])
88
89         assert [r.place_id for r in results] == [22]
90
91
92     def test_multiple_types_near_in_placex(self, apiobj):
93         apiobj.add_placex(place_id=22, class_='amenity', type='bank',
94                           importance=0.002,
95                           centroid=(5.6001, 4.2994))
96         apiobj.add_placex(place_id=23, class_='amenity', type='bench',
97                           importance=0.001,
98                           centroid=(5.6001, 4.2994))
99
100         results = run_search(apiobj, 0.1, [('amenity', 'bank'),
101                                            ('amenity', 'bench')])
102
103         assert [r.place_id for r in results] == [22, 23]
104
105
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')
113
114         results = run_search(apiobj, 0.1, [('amenity', 'bank')])
115
116         assert [r.place_id for r in results] == [22]
117
118
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),
123                           country_code='us')
124         apiobj.add_placex(place_id=122, class_='amenity', type='bank',
125                           centroid=(5.6001, 4.2994),
126                           country_code='mx')
127         apiobj.add_placex(place_id=23, class_='amenity', type='bank',
128                           centroid=(-10.3001, 56.9),
129                           country_code='mx')
130         apiobj.add_placex(place_id=123, class_='amenity', type='bank',
131                           centroid=(-10.3001, 56.9),
132                           country_code='us')
133
134         results = run_search(apiobj, 0.1, [('amenity', 'bank')], ccodes=[cc, 'fr'])
135
136         assert [r.place_id for r in results] == [rid]
137
138
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),
143                           country_code='us')
144         apiobj.add_placex(place_id=122, class_='amenity', type='bank',
145                           centroid=(5.6001, 4.2994),
146                           country_code='us')
147
148
149         results = run_search(apiobj, 0.1, [('amenity', 'bank')],
150                              details=SearchDetails(excluded=[excluded]))
151
152         assert [r.place_id for r in results] == [rid]
153
154
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),
160                           country_code='us')
161
162         results = run_search(apiobj, 0.1, [('amenity', 'bank')],
163                              details=SearchDetails(layers=layer))
164
165         assert [r.place_id for r in results] == rids