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