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
15 import nominatim.api as napi
16 from nominatim.db.sql_preprocessor import SQLPreprocessor
21 self.api = napi.NominatimAPI(Path('/invalid'), {})
22 self.async_to_sync(self.api._async_api.setup_database())
25 def async_to_sync(self, func):
26 """ Run an asynchronous function until completion using the
27 internal loop of the API.
29 return self.api._loop.run_until_complete(func)
32 def add_data(self, table, data):
33 """ Insert data into the given table.
35 sql = getattr(self.api._async_api._tables, table).insert()
36 self.async_to_sync(self.exec_async(sql, data))
39 def add_placex(self, **kw):
41 if isinstance(name, str):
44 self.add_data('placex',
45 {'place_id': kw.get('place_id', 1000),
46 'osm_type': kw.get('osm_type', 'W'),
47 'osm_id': kw.get('osm_id', 4),
48 'class_': kw.get('class_', 'highway'),
49 'type': kw.get('type', 'residential'),
51 'address': kw.get('address'),
52 'extratags': kw.get('extratags'),
53 'parent_place_id': kw.get('parent_place_id'),
54 'linked_place_id': kw.get('linked_place_id'),
55 'admin_level': kw.get('admin_level', 15),
56 'country_code': kw.get('country_code'),
57 'housenumber': kw.get('housenumber'),
58 'postcode': kw.get('postcode'),
59 'wikipedia': kw.get('wikipedia'),
60 'rank_search': kw.get('rank_search', 30),
61 'rank_address': kw.get('rank_address', 30),
62 'importance': kw.get('importance'),
63 'centroid': 'SRID=4326;POINT(%f %f)' % kw.get('centroid', (23.0, 34.0)),
64 'indexed_date': kw.get('indexed_date',
65 dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
66 'geometry': 'SRID=4326;' + kw.get('geometry', 'POINT(23 34)')})
69 def add_address_placex(self, object_id, **kw):
71 self.add_data('addressline',
72 {'place_id': object_id,
73 'address_place_id': kw.get('place_id', 1000),
74 'distance': kw.get('distance', 0.0),
75 'cached_rank_address': kw.get('rank_address', 30),
76 'fromarea': kw.get('fromarea', False),
77 'isaddress': kw.get('isaddress', True)})
80 async def exec_async(self, sql, *args, **kwargs):
81 async with self.api._async_api.begin() as conn:
82 return await conn.execute(sql, *args, **kwargs)
85 async def create_tables(self):
86 async with self.api._async_api._engine.begin() as conn:
87 await conn.run_sync(self.api._async_api._tables.meta.create_all)
91 def apiobj(temp_db_with_extensions, temp_db_conn):
92 """ Create an asynchronous SQLAlchemy engine for the test DB.
95 testapi.async_to_sync(testapi.create_tables())
97 SQLPreprocessor(temp_db_conn, testapi.api.config)\
98 .run_sql_file(temp_db_conn, 'functions/address_lookup.sql')