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