]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_near.py
always run function update on migrations
[nominatim.git] / test / python / api / search / test_search_near.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 near 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 NearSearch, PlaceSearch
15 from nominatim.api.search.db_search_fields import WeightedStrings, WeightedCategories,\
16                                                   FieldLookup, FieldRanking, RankedTokens
17
18
19 def run_search(apiobj, global_penalty, cat, cat_penalty=None,
20                details=SearchDetails()):
21
22     class PlaceSearchData:
23         penalty = 0.0
24         postcodes = WeightedStrings([], [])
25         countries = WeightedStrings([], [])
26         housenumbers = WeightedStrings([], [])
27         qualifiers = WeightedStrings([], [])
28         lookups = [FieldLookup('name_vector', [56], 'lookup_all')]
29         rankings = []
30
31     place_search = PlaceSearch(0.0, PlaceSearchData(), 2)
32
33     if cat_penalty is None:
34         cat_penalty = [0.0] * len(cat)
35
36     near_search = NearSearch(0.1, WeightedCategories(cat, cat_penalty), place_search)
37
38     async def run():
39         async with apiobj.api._async_api.begin() as conn:
40             return await near_search.lookup(conn, details)
41
42     results = apiobj.async_to_sync(run())
43     results.sort(key=lambda r: r.accuracy)
44
45     return results
46
47
48 def test_no_results_inner_query(apiobj):
49     assert not run_search(apiobj, 0.4, [('this', 'that')])
50
51
52 class TestNearSearch:
53
54     @pytest.fixture(autouse=True)
55     def fill_database(self, apiobj):
56         apiobj.add_placex(place_id=100, country_code='us',
57                           centroid=(5.6, 4.3))
58         apiobj.add_search_name(100, names=[56], country_code='us',
59                                centroid=(5.6, 4.3))
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))
64
65
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))
71
72         results = run_search(apiobj, 0.1, [('amenity', 'bank')])
73
74         assert [r.place_id for r in results] == [22]
75
76
77     def test_multiple_types_near_in_placex(self, apiobj):
78         apiobj.add_placex(place_id=22, class_='amenity', type='bank',
79                           importance=0.002,
80                           centroid=(5.6001, 4.2994))
81         apiobj.add_placex(place_id=23, class_='amenity', type='bench',
82                           importance=0.001,
83                           centroid=(5.6001, 4.2994))
84
85         results = run_search(apiobj, 0.1, [('amenity', 'bank'),
86                                            ('amenity', 'bench')])
87
88         assert [r.place_id for r in results] == [22, 23]
89
90
91     def test_near_in_classtype(self, apiobj):
92         apiobj.add_placex(place_id=22, class_='amenity', type='bank',
93                           centroid=(5.6, 4.34))
94         apiobj.add_placex(place_id=23, class_='amenity', type='bench',
95                           centroid=(5.6, 4.34))
96         apiobj.add_class_type_table('amenity', 'bank')
97         apiobj.add_class_type_table('amenity', 'bench')
98
99         results = run_search(apiobj, 0.1, [('amenity', 'bank')])
100
101         assert [r.place_id for r in results] == [22]
102