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 POI searcher.
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
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)
24 penalty = global_penalty
25 qualifiers = WeightedCategories(poitypes, poi_penalties)
26 countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
28 search = PoiSearch(MySearchData())
31 async with apiobj.api._async_api.begin() as conn:
32 return await search.lookup(conn, details)
34 return apiobj.async_to_sync(run())
37 @pytest.mark.parametrize('coord,pid', [('34.3, 56.100021', 2),
39 def test_simple_near_search_in_placex(apiobj, coord, pid):
40 apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
42 apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
43 centroid=(34.3, 56.1))
45 details = SearchDetails.from_kwargs({'near': coord, 'near_radius': 0.001})
47 results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5], details=details)
49 assert [r.place_id for r in results] == [pid]
52 @pytest.mark.parametrize('coord,pid', [('34.3, 56.100021', 2),
55 def test_simple_near_search_in_classtype(apiobj, coord, pid):
56 apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
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')
62 details = SearchDetails.from_kwargs({'near': coord, 'near_radius': 0.5})
64 results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5], details=details)
66 assert [r.place_id for r in results] == [pid]
69 class TestPoiSearchWithRestrictions:
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',
75 centroid=(34.3, 56.10003))
76 apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
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}
83 self.args = {'near': '34.3, 56.100021', 'near_radius': 0.001}
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))
90 assert [r.place_id for r in results] == [1, 2]
93 def test_restict_country(self, apiobj):
94 results = run_search(apiobj, 0.1, [('highway', 'bus_stop')], [0.5],
96 details=SearchDetails.from_kwargs(self.args))
98 assert [r.place_id for r in results] == [2]
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],
106 details=SearchDetails.from_kwargs(args))
108 assert [r.place_id for r in results] == [2]