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 near searcher.
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
19 def run_search(apiobj, global_penalty, cat, cat_penalty=None,
20 details=SearchDetails()):
22 class PlaceSearchData:
24 postcodes = WeightedStrings([], [])
25 countries = WeightedStrings([], [])
26 housenumbers = WeightedStrings([], [])
27 qualifiers = WeightedStrings([], [])
28 lookups = [FieldLookup('name_vector', [56], 'lookup_all')]
31 place_search = PlaceSearch(0.0, PlaceSearchData(), 2)
33 if cat_penalty is None:
34 cat_penalty = [0.0] * len(cat)
36 near_search = NearSearch(0.1, WeightedCategories(cat, cat_penalty), place_search)
39 async with apiobj.api._async_api.begin() as conn:
40 return await near_search.lookup(conn, details)
42 results = apiobj.async_to_sync(run())
43 results.sort(key=lambda r: r.accuracy)
48 def test_no_results_inner_query(apiobj):
49 assert not run_search(apiobj, 0.4, [('this', 'that')])
54 @pytest.fixture(autouse=True)
55 def fill_database(self, apiobj):
56 apiobj.add_placex(place_id=100, country_code='us',
58 apiobj.add_search_name(100, names=[56], country_code='us',
60 apiobj.add_placex(place_id=101, country_code='mx',
61 centroid=(-10.3, 56.9))
62 apiobj.add_search_name(101, names=[56], country_code='mx',
63 centroid=(-10.3, 56.9))
66 def test_near_in_placex(self, apiobj):
67 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
68 centroid=(5.6001, 4.2994))
69 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
70 centroid=(5.6001, 4.2994))
72 results = run_search(apiobj, 0.1, [('amenity', 'bank')])
74 assert [r.place_id for r in results] == [22]
77 def test_multiple_types_near_in_placex(self, apiobj):
78 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
80 centroid=(5.6001, 4.2994))
81 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
83 centroid=(5.6001, 4.2994))
85 results = run_search(apiobj, 0.1, [('amenity', 'bank'),
86 ('amenity', 'bench')])
88 assert [r.place_id for r in results] == [22, 23]
91 def test_near_in_classtype(self, apiobj):
92 apiobj.add_placex(place_id=22, class_='amenity', type='bank',
94 apiobj.add_placex(place_id=23, class_='amenity', type='bench',
96 apiobj.add_class_type_table('amenity', 'bank')
97 apiobj.add_class_type_table('amenity', 'bench')
99 results = run_search(apiobj, 0.1, [('amenity', 'bank')])
101 assert [r.place_id for r in results] == [22]