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, frontend, 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())
30 api = frontend(apiobj, options=['search'])
33 async with api._async_api.begin() as conn:
34 return await search.lookup(conn, details)
36 return api._loop.run_until_complete(run())
39 @pytest.mark.parametrize('coord,pid', [('34.3, 56.100021', 2),
41 def test_simple_near_search_in_placex(apiobj, frontend, coord, pid):
42 apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
44 apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
45 centroid=(34.3, 56.1))
47 details = SearchDetails.from_kwargs({'near': coord, 'near_radius': 0.001})
49 results = run_search(apiobj, frontend, 0.1, [('highway', 'bus_stop')], [0.5], details=details)
51 assert [r.place_id for r in results] == [pid]
54 @pytest.mark.parametrize('coord,pid', [('34.3, 56.100021', 2),
57 def test_simple_near_search_in_classtype(apiobj, frontend, coord, pid):
58 apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
60 apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
61 centroid=(34.3, 56.1))
62 apiobj.add_class_type_table('highway', 'bus_stop')
64 details = SearchDetails.from_kwargs({'near': coord, 'near_radius': 0.5})
66 results = run_search(apiobj, frontend, 0.1, [('highway', 'bus_stop')], [0.5], details=details)
68 assert [r.place_id for r in results] == [pid]
71 class TestPoiSearchWithRestrictions:
73 @pytest.fixture(autouse=True, params=["placex", "classtype"])
74 def fill_database(self, apiobj, request):
75 apiobj.add_placex(place_id=1, class_='highway', type='bus_stop',
77 centroid=(34.3, 56.10003))
78 apiobj.add_placex(place_id=2, class_='highway', type='bus_stop',
80 centroid=(34.3, 56.1))
81 if request.param == 'classtype':
82 apiobj.add_class_type_table('highway', 'bus_stop')
83 self.args = {'near': '34.3, 56.4', 'near_radius': 0.5}
85 self.args = {'near': '34.3, 56.100021', 'near_radius': 0.001}
88 def test_unrestricted(self, apiobj, frontend):
89 results = run_search(apiobj, frontend, 0.1, [('highway', 'bus_stop')], [0.5],
90 details=SearchDetails.from_kwargs(self.args))
92 assert [r.place_id for r in results] == [1, 2]
95 def test_restict_country(self, apiobj, frontend):
96 results = run_search(apiobj, frontend, 0.1, [('highway', 'bus_stop')], [0.5],
98 details=SearchDetails.from_kwargs(self.args))
100 assert [r.place_id for r in results] == [2]
103 def test_restrict_by_viewbox(self, apiobj, frontend):
104 args = {'bounded_viewbox': True, 'viewbox': '34.299,56.0,34.3001,56.10001'}
105 args.update(self.args)
106 results = run_search(apiobj, frontend, 0.1, [('highway', 'bus_stop')], [0.5],
108 details=SearchDetails.from_kwargs(args))
110 assert [r.place_id for r in results] == [2]