]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_api_search.py
enable flake for Python tests
[nominatim.git] / test / python / api / test_api_search.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 search API calls.
9
10 These tests make sure that all Python code is correct and executable.
11 Functional tests can be found in the BDD test suite.
12 """
13 import pytest
14
15 import nominatim_api.logging as loglib
16
17 API_OPTIONS = {'search'}
18
19
20 @pytest.fixture(autouse=True)
21 def setup_icu_tokenizer(apiobj):
22     """ Setup the properties needed for using the ICU tokenizer.
23     """
24     apiobj.add_data('properties',
25                     [{'property': 'tokenizer', 'value': 'icu'},
26                      {'property': 'tokenizer_import_normalisation', 'value': ':: lower();'},
27                      {'property': 'tokenizer_import_transliteration',
28                       'value': "'1' > '/1/'; 'ä' > 'ä '"},
29                      ])
30
31
32 def test_search_no_content(apiobj, frontend):
33     apiobj.add_word_table([])
34
35     api = frontend(apiobj, options=API_OPTIONS)
36     assert api.search('foo') == []
37
38
39 def test_search_simple_word(apiobj, frontend):
40     apiobj.add_word_table([(55, 'test', 'W', 'test', None),
41                            (2, 'test', 'w', 'test', None)])
42
43     apiobj.add_placex(place_id=444, class_='place', type='village',
44                       centroid=(1.3, 0.7))
45     apiobj.add_search_name(444, names=[2, 55])
46
47     api = frontend(apiobj, options=API_OPTIONS)
48     results = api.search('TEST')
49
50     assert [r.place_id for r in results] == [444]
51
52
53 @pytest.mark.parametrize('logtype', ['text', 'html'])
54 def test_search_with_debug(apiobj, frontend, logtype):
55     apiobj.add_word_table([(55, 'test', 'W', 'test', None),
56                            (2, 'test', 'w', 'test', None)])
57
58     apiobj.add_placex(place_id=444, class_='place', type='village',
59                       centroid=(1.3, 0.7))
60     apiobj.add_search_name(444, names=[2, 55])
61
62     api = frontend(apiobj, options=API_OPTIONS)
63     loglib.set_log_output(logtype)
64     api.search('TEST')
65
66     assert loglib.get_and_disable()
67
68
69 def test_address_no_content(apiobj, frontend):
70     apiobj.add_word_table([])
71
72     api = frontend(apiobj, options=API_OPTIONS)
73     assert api.search_address(amenity='hotel',
74                               street='Main St 34',
75                               city='Happyville',
76                               county='Wideland',
77                               state='Praerie',
78                               postalcode='55648',
79                               country='xx') == []
80
81
82 @pytest.mark.parametrize('atype,address,search', [('street', 26, 26),
83                                                   ('city', 16, 18),
84                                                   ('county', 12, 12),
85                                                   ('state', 8, 8)])
86 def test_address_simple_places(apiobj, frontend, atype, address, search):
87     apiobj.add_word_table([(55, 'test', 'W', 'test', None),
88                            (2, 'test', 'w', 'test', None)])
89
90     apiobj.add_placex(place_id=444,
91                       rank_address=address, rank_search=search,
92                       centroid=(1.3, 0.7))
93     apiobj.add_search_name(444, names=[2, 55], address_rank=address, search_rank=search)
94
95     api = frontend(apiobj, options=API_OPTIONS)
96     results = api.search_address(**{atype: 'TEST'})
97
98     assert [r.place_id for r in results] == [444]
99
100
101 def test_address_country(apiobj, frontend):
102     apiobj.add_word_table([(None, 'ro', 'C', 'ro', None)])
103     apiobj.add_country('ro', 'POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')
104     apiobj.add_country_name('ro', {'name': 'România'})
105
106     api = frontend(apiobj, options=API_OPTIONS)
107     assert len(api.search_address(country='ro')) == 1
108
109
110 def test_category_no_categories(apiobj, frontend):
111     apiobj.add_word_table([])
112
113     api = frontend(apiobj, options=API_OPTIONS)
114     assert api.search_category([], near_query='Berlin') == []
115
116
117 def test_category_no_content(apiobj, frontend):
118     apiobj.add_word_table([])
119
120     api = frontend(apiobj, options=API_OPTIONS)
121     assert api.search_category([('amenity', 'restaurant')]) == []
122
123
124 def test_category_simple_restaurant(apiobj, frontend):
125     apiobj.add_word_table([])
126
127     apiobj.add_placex(place_id=444, class_='amenity', type='restaurant',
128                       centroid=(1.3, 0.7))
129     apiobj.add_search_name(444, names=[2, 55], address_rank=16, search_rank=18)
130
131     api = frontend(apiobj, options=API_OPTIONS)
132     results = api.search_category([('amenity', 'restaurant')],
133                                   near=(1.3, 0.701), near_radius=0.015)
134
135     assert [r.place_id for r in results] == [444]
136
137
138 def test_category_with_search_phrase(apiobj, frontend):
139     apiobj.add_word_table([(55, 'test', 'W', 'test', None),
140                            (2, 'test', 'w', 'test', None)])
141
142     apiobj.add_placex(place_id=444, class_='place', type='village',
143                       rank_address=16, rank_search=18,
144                       centroid=(1.3, 0.7))
145     apiobj.add_search_name(444, names=[2, 55], address_rank=16, search_rank=18)
146     apiobj.add_placex(place_id=95, class_='amenity', type='restaurant',
147                       centroid=(1.3, 0.7003))
148
149     api = frontend(apiobj, options=API_OPTIONS)
150     results = api.search_category([('amenity', 'restaurant')], near_query='TEST')
151
152     assert [r.place_id for r in results] == [95]