1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 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
14 import nominatim.api as napi
19 self.api = napi.NominatimAPI(Path('/invalid'), {})
20 self.async_to_sync(self.api._async_api.setup_database())
23 def async_to_sync(self, func):
24 """ Run an asynchronous function until completion using the
25 internal loop of the API.
27 return self.api._loop.run_until_complete(func)
30 def add_data(self, table, data):
31 """ Insert data into the given table.
33 sql = getattr(self.api._async_api._tables, table).insert()
34 self.async_to_sync(self.exec_async(sql, data))
37 async def exec_async(self, sql, *args, **kwargs):
38 async with self.api._async_api.begin() as conn:
39 return await conn.execute(sql, *args, **kwargs)
42 async def create_tables(self):
43 async with self.api._async_api._engine.begin() as conn:
44 await conn.run_sync(self.api._async_api._tables.meta.create_all)
48 def apiobj(temp_db_with_extensions):
49 """ Create an asynchronous SQLAlchemy engine for the test DB.
52 testapi.async_to_sync(testapi.create_tables())