]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_poi.py
These days the OSM wikipedia tab no longer contains URLs
[nominatim.git] / test / python / api / search / test_search_poi.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 POI 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 PoiSearch
15 from nominatim.api.search.db_search_fields import WeightedStrings, WeightedCategories
16
17
18 def run_search(apiobj, global_penalty, poitypes, poi_penalties=None,
19                ccodes=[], details=SearchDetails()):
20     if poi_penalties is None:
21         poi_penalties = [0.0] * len(poitypes)
22
23     class MySearchData:
24         penalty = global_penalty
25         qualifiers = WeightedCategories(poitypes, poi_penalties)
26         countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
27
28     search = PoiSearch(MySearchData())
29
30     async def run():
31         async with apiobj.api._async_api.begin() as conn:
32             return await search.lookup(conn, details)
33
34     return apiobj.async_to_sync(run())
35
36
37 @pytest.mark.parametrize('coord,pid', [('34.3, 56.100021', 2),
38                                        ('5.0, 4.59933', 1)])
39 def test_simple_near_search_in_placex(apiobj, coord, pid):
40     apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
41                       centroid=(5.0, 4.6))
42     apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
43                       centroid=(34.3, 56.1))
44
45     details = SearchDetails.from_kwargs({'near': coord, 'near_radius': 0.001})
46
47     results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5], details=details)
48
49     assert [r.place_id for r in results] == [pid]
50
51
52 @pytest.mark.parametrize('coord,pid', [('34.3, 56.100021', 2),
53                                        ('34.3, 56.4', 2),
54                                        ('5.0, 4.59933', 1)])
55 def test_simple_near_search_in_classtype(apiobj, coord, pid):
56     apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
57                       centroid=(5.0, 4.6))
58     apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
59                       centroid=(34.3, 56.1))
60     apiobj.add_class_type_table('highway', 'bus_stop')
61
62     details = SearchDetails.from_kwargs({'near': coord, 'near_radius': 0.5})
63
64     results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5], details=details)
65
66     assert [r.place_id for r in results] == [pid]
67
68
69 class TestPoiSearchWithRestrictions:
70
71     @pytest.fixture(autouse=True, params=["placex", "classtype"])
72     def fill_database(self, apiobj, request):
73         apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
74                           country_code='au',
75                           centroid=(34.3, 56.10003))
76         apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
77                           country_code='nz',
78                           centroid=(34.3, 56.1))
79         if request.param == 'classtype':
80             apiobj.add_class_type_table('highway', 'bus_stop')
81             self.args = {'near': '34.3, 56.4', 'near_radius': 0.5}
82         else:
83             self.args = {'near': '34.3, 56.100021', 'near_radius': 0.001}
84
85
86     def test_unrestricted(self, apiobj):
87         results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5],
88                              details=SearchDetails.from_kwargs(self.args))
89
90         assert [r.place_id for r in results] == [1, 2]
91
92
93     def test_restict_country(self, apiobj):
94         results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5],
95                              ccodes=['de', 'nz'],
96                              details=SearchDetails.from_kwargs(self.args))
97
98         assert [r.place_id for r in results] == [2]
99
100
101     def test_restrict_by_viewbox(self, apiobj):
102         args = {'bounded_viewbox': True, 'viewbox': '34.299,56.0,34.3001,56.10001'}
103         args.update(self.args)
104         results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5],
105                              ccodes=['de', 'nz'],
106                              details=SearchDetails.from_kwargs(args))
107
108         assert [r.place_id for r in results] == [2]