1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Helper fixtures for API call tests.
10 from pathlib import Path
16 import sqlalchemy as sa
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
27 self.api = napi.NominatimAPI(Path('/invalid'))
28 self.async_to_sync(self.api._async_api.setup_database())
31 def async_to_sync(self, func):
32 """ Run an asynchronous function until completion using the
33 internal loop of the API.
35 return self.api._loop.run_until_complete(func)
38 def add_data(self, table, data):
39 """ Insert data into the given table.
41 sql = getattr(self.api._async_api._tables, table).insert()
42 self.async_to_sync(self.exec_async(sql, data))
45 def add_placex(self, **kw):
47 if isinstance(name, str):
50 centroid = kw.get('centroid', (23.0, 34.0))
51 geometry = kw.get('geometry', 'POINT(%f %f)' % centroid)
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'),
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})
79 def add_address_placex(self, object_id, **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)})
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)')})
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)')})
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)')})
130 def add_country(self, country_code, geometry):
131 self.add_data('country_grid',
132 {'country_code': country_code,
134 'geometry': geometry})
137 def add_country_name(self, country_code, names, partition=0):
138 self.add_data('country_name',
139 {'country_code': country_code,
141 'partition': partition})
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})
157 def add_class_type_table(self, cls, typ):
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}')
165 def add_word_table(self, content):
166 data = [dict(zip(['word_id', 'word_token', 'type', 'word', 'info'], c))
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)
176 await conn.execute(conn.t.meta.tables['word'].insert(), data)
178 self.async_to_sync(_do_sql())
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)
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)
192 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
193 """ Create an asynchronous SQLAlchemy engine for the test DB.
195 monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
196 testapi = APITester()
197 testapi.async_to_sync(testapi.create_tables())
199 proc = SQLPreprocessor(temp_db_conn, testapi.api.config)
200 proc.run_sql_file(temp_db_conn, 'functions/ranking.sql')
202 loglib.set_log_output('text')
204 print(loglib.get_and_disable())
209 @pytest.fixture(params=['postgres_db', 'sqlite_db'])
210 def frontend(request, event_loop, tmp_path):
212 if request.param == 'sqlite_db':
213 db = str(tmp_path / 'test_nominatim_python_unittest.sqlite')
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/'; 'ä' > 'ä '"},
223 async with apiobj.api._async_api.begin() as conn:
224 if 'word' in conn.t.meta.tables:
226 await make_query_analyzer(conn)
227 word_table = conn.t.meta.tables['word']
228 await conn.connection.run_sync(word_table.create)
230 apiobj.async_to_sync(_do_sql())
232 event_loop.run_until_complete(convert_sqlite.convert(Path('/invalid'),
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)
240 elif request.param == 'postgres_db':
241 def mkapi(apiobj, options=None):
250 @pytest_asyncio.fixture
251 async def api(temp_db):
252 async with napi.NominatimAPIAsync(Path('/invalid')) as api: