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.
15 import sqlalchemy as sa
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
26 self.api = napi.NominatimAPI()
27 self.async_to_sync(self.api._async_api.setup_database())
30 def async_to_sync(self, func):
31 """ Run an asynchronous function until completion using the
32 internal loop of the API.
34 return self.api._loop.run_until_complete(func)
37 def add_data(self, table, data):
38 """ Insert data into the given table.
40 sql = getattr(self.api._async_api._tables, table).insert()
41 self.async_to_sync(self.exec_async(sql, data))
44 def add_placex(self, **kw):
46 if isinstance(name, str):
49 centroid = kw.get('centroid', (23.0, 34.0))
50 geometry = kw.get('geometry', 'POINT(%f %f)' % centroid)
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'),
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})
78 def add_address_placex(self, object_id, **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)})
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)')})
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)')})
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)')})
129 def add_country(self, country_code, geometry):
130 self.add_data('country_grid',
131 {'country_code': country_code,
133 'geometry': geometry})
136 def add_country_name(self, country_code, names, partition=0):
137 self.add_data('country_name',
138 {'country_code': country_code,
140 'partition': partition})
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})
156 def add_class_type_table(self, cls, typ):
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}')
164 def add_word_table(self, content):
165 data = [dict(zip(['word_id', 'word_token', 'type', 'word', 'info'], c))
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)
175 await conn.execute(conn.t.meta.tables['word'].insert(), data)
177 self.async_to_sync(_do_sql())
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)
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)
191 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
192 """ Create an asynchronous SQLAlchemy engine for the test DB.
194 monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
195 testapi = APITester()
196 testapi.async_to_sync(testapi.create_tables())
198 proc = SQLPreprocessor(temp_db_conn, testapi.api.config)
199 proc.run_sql_file(temp_db_conn, 'functions/ranking.sql')
201 loglib.set_log_output('text')
203 print(loglib.get_and_disable())
208 @pytest.fixture(params=['postgres_db', 'sqlite_db'])
209 def frontend(request, event_loop, tmp_path):
211 if request.param == 'sqlite_db':
212 db = str(tmp_path / 'test_nominatim_python_unittest.sqlite')
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/'; 'ä' > 'ä '"},
222 async with apiobj.api._async_api.begin() as conn:
223 if 'word' in conn.t.meta.tables:
225 await make_query_analyzer(conn)
226 word_table = conn.t.meta.tables['word']
227 await conn.connection.run_sync(word_table.create)
229 apiobj.async_to_sync(_do_sql())
231 event_loop.run_until_complete(convert_sqlite.convert(None, db, options))
232 outapi = napi.NominatimAPI(environ={'NOMINATIM_DATABASE_DSN': f"sqlite:dbname={db}",
233 'NOMINATIM_USE_US_TIGER_DATA': 'yes'})
234 testapis.append(outapi)
237 elif request.param == 'postgres_db':
238 def mkapi(apiobj, options=None):
247 @pytest_asyncio.fixture
248 async def api(temp_db):
249 async with napi.NominatimAPIAsync() as api: