]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/conftest.py
lift restrictions on search with frequent terms slightly
[nominatim.git] / test / python / api / conftest.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) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Helper fixtures for API call tests.
9 """
10 from pathlib import Path
11 import pytest
12 import pytest_asyncio
13 import time
14 import datetime as dt
15
16 import sqlalchemy as sa
17
18 import nominatim_api as napi
19 from nominatim_db.db.sql_preprocessor import SQLPreprocessor
20 from nominatim_api.search.query_analyzer_factory import make_query_analyzer
21 from nominatim_db.tools import convert_sqlite
22 import nominatim_api.logging as loglib
23
24 class APITester:
25
26     def __init__(self):
27         self.api = napi.NominatimAPI(Path('/invalid'))
28         self.async_to_sync(self.api._async_api.setup_database())
29
30
31     def async_to_sync(self, func):
32         """ Run an asynchronous function until completion using the
33             internal loop of the API.
34         """
35         return self.api._loop.run_until_complete(func)
36
37
38     def add_data(self, table, data):
39         """ Insert data into the given table.
40         """
41         sql = getattr(self.api._async_api._tables, table).insert()
42         self.async_to_sync(self.exec_async(sql, data))
43
44
45     def add_placex(self, **kw):
46         name = kw.get('name')
47         if isinstance(name, str):
48             name = {'name': name}
49
50         centroid = kw.get('centroid', (23.0, 34.0))
51         geometry = kw.get('geometry', 'POINT(%f %f)' % centroid)
52
53         self.add_data('placex',
54                      {'place_id': kw.get('place_id', 1000),
55                       'osm_type': kw.get('osm_type', 'W'),
56                       'osm_id': kw.get('osm_id', 4),
57                       'class_': kw.get('class_', 'highway'),
58                       'type': kw.get('type', 'residential'),
59                       'name': name,
60                       'address': kw.get('address'),
61                       'extratags': kw.get('extratags'),
62                       'parent_place_id': kw.get('parent_place_id'),
63                       'linked_place_id': kw.get('linked_place_id'),
64                       'admin_level': kw.get('admin_level', 15),
65                       'country_code': kw.get('country_code'),
66                       'housenumber': kw.get('housenumber'),
67                       'postcode': kw.get('postcode'),
68                       'wikipedia': kw.get('wikipedia'),
69                       'rank_search': kw.get('rank_search', 30),
70                       'rank_address': kw.get('rank_address', 30),
71                       'importance': kw.get('importance'),
72                       'centroid': 'POINT(%f %f)' % centroid,
73                       'indexed_status': kw.get('indexed_status', 0),
74                       'indexed_date': kw.get('indexed_date',
75                                              dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
76                       'geometry': geometry})
77
78
79     def add_address_placex(self, object_id, **kw):
80         self.add_placex(**kw)
81         self.add_data('addressline',
82                       {'place_id': object_id,
83                        'address_place_id': kw.get('place_id', 1000),
84                        'distance': kw.get('distance', 0.0),
85                        'cached_rank_address': kw.get('rank_address', 30),
86                        'fromarea': kw.get('fromarea', False),
87                        'isaddress': kw.get('isaddress', True)})
88
89
90     def add_osmline(self, **kw):
91         self.add_data('osmline',
92                      {'place_id': kw.get('place_id', 10000),
93                       'osm_id': kw.get('osm_id', 4004),
94                       'parent_place_id': kw.get('parent_place_id'),
95                       'indexed_date': kw.get('indexed_date',
96                                              dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
97                       'startnumber': kw.get('startnumber', 2),
98                       'endnumber': kw.get('endnumber', 6),
99                       'step': kw.get('step', 2),
100                       'address': kw.get('address'),
101                       'postcode': kw.get('postcode'),
102                       'country_code': kw.get('country_code'),
103                       'linegeo': kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
104
105
106     def add_tiger(self, **kw):
107         self.add_data('tiger',
108                      {'place_id': kw.get('place_id', 30000),
109                       'parent_place_id': kw.get('parent_place_id'),
110                       'startnumber': kw.get('startnumber', 2),
111                       'endnumber': kw.get('endnumber', 6),
112                       'step': kw.get('step', 2),
113                       'postcode': kw.get('postcode'),
114                       'linegeo': kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
115
116
117     def add_postcode(self, **kw):
118         self.add_data('postcode',
119                      {'place_id': kw.get('place_id', 1000),
120                       'parent_place_id': kw.get('parent_place_id'),
121                       'country_code': kw.get('country_code'),
122                       'postcode': kw.get('postcode'),
123                       'rank_search': kw.get('rank_search', 20),
124                       'rank_address': kw.get('rank_address', 22),
125                       'indexed_date': kw.get('indexed_date',
126                                              dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
127                       'geometry': kw.get('geometry', 'POINT(23 34)')})
128
129
130     def add_country(self, country_code, geometry):
131         self.add_data('country_grid',
132                       {'country_code': country_code,
133                        'area': 0.1,
134                        'geometry': geometry})
135
136
137     def add_country_name(self, country_code, names, partition=0):
138         self.add_data('country_name',
139                       {'country_code': country_code,
140                        'name': names,
141                        'partition': partition})
142
143
144     def add_search_name(self, place_id, **kw):
145         centroid = kw.get('centroid', (23.0, 34.0))
146         self.add_data('search_name',
147                       {'place_id': place_id,
148                        'importance': kw.get('importance', 0.00001),
149                        'search_rank': kw.get('search_rank', 30),
150                        'address_rank': kw.get('address_rank', 30),
151                        'name_vector': kw.get('names', []),
152                        'nameaddress_vector': kw.get('address', []),
153                        'country_code': kw.get('country_code', 'xx'),
154                        'centroid': 'POINT(%f %f)' % centroid})
155
156
157     def add_class_type_table(self, cls, typ):
158         self.async_to_sync(
159             self.exec_async(sa.text(f"""CREATE TABLE place_classtype_{cls}_{typ}
160                                          AS (SELECT place_id, centroid FROM placex
161                                              WHERE class = '{cls}' AND type = '{typ}')
162                                      """)))
163
164
165     def add_word_table(self, content):
166         data = [dict(zip(['word_id', 'word_token', 'type', 'word', 'info'], c))
167                 for c in content]
168
169         async def _do_sql():
170             async with self.api._async_api.begin() as conn:
171                 if 'word' not in conn.t.meta.tables:
172                     await make_query_analyzer(conn)
173                     word_table = conn.t.meta.tables['word']
174                     await conn.connection.run_sync(word_table.create)
175                 if data:
176                     await conn.execute(conn.t.meta.tables['word'].insert(), data)
177
178         self.async_to_sync(_do_sql())
179
180
181     async def exec_async(self, sql, *args, **kwargs):
182         async with self.api._async_api.begin() as conn:
183             return await conn.execute(sql, *args, **kwargs)
184
185
186     async def create_tables(self):
187         async with self.api._async_api._engine.begin() as conn:
188             await conn.run_sync(self.api._async_api._tables.meta.create_all)
189
190
191 @pytest.fixture
192 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
193     """ Create an asynchronous SQLAlchemy engine for the test DB.
194     """
195     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
196     testapi = APITester()
197     testapi.async_to_sync(testapi.create_tables())
198
199     proc = SQLPreprocessor(temp_db_conn, testapi.api.config)
200     proc.run_sql_file(temp_db_conn, 'functions/ranking.sql')
201
202     loglib.set_log_output('text')
203     yield testapi
204     print(loglib.get_and_disable())
205
206     testapi.api.close()
207
208
209 @pytest.fixture(params=['postgres_db', 'sqlite_db'])
210 def frontend(request, event_loop, tmp_path):
211     testapis = []
212     if request.param == 'sqlite_db':
213         db = str(tmp_path / 'test_nominatim_python_unittest.sqlite')
214
215         def mkapi(apiobj, options={'reverse'}):
216             apiobj.add_data('properties',
217                         [{'property': 'tokenizer', 'value': 'icu'},
218                          {'property': 'tokenizer_import_normalisation', 'value': ':: lower();'},
219                          {'property': 'tokenizer_import_transliteration', 'value': "'1' > '/1/'; 'ä' > 'ä '"},
220                         ])
221
222             async def _do_sql():
223                 async with apiobj.api._async_api.begin() as conn:
224                     if 'word' in conn.t.meta.tables:
225                         return
226                     await make_query_analyzer(conn)
227                     word_table = conn.t.meta.tables['word']
228                     await conn.connection.run_sync(word_table.create)
229
230             apiobj.async_to_sync(_do_sql())
231
232             event_loop.run_until_complete(convert_sqlite.convert(Path('/invalid'),
233                                                                  db, options))
234             outapi = napi.NominatimAPI(Path('/invalid'),
235                                        {'NOMINATIM_DATABASE_DSN': f"sqlite:dbname={db}",
236                                         'NOMINATIM_USE_US_TIGER_DATA': 'yes'})
237             testapis.append(outapi)
238
239             return outapi
240     elif request.param == 'postgres_db':
241         def mkapi(apiobj, options=None):
242             return apiobj.api
243
244     yield mkapi
245
246     for api in testapis:
247         api.close()
248
249
250 @pytest_asyncio.fixture
251 async def api(temp_db):
252     async with napi.NominatimAPIAsync(Path('/invalid')) as api:
253         yield api