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 def add_osmline(self, **kw):
81 self.add_data('osmline',
82 {'place_id': kw.get('place_id', 10000),
83 'osm_id': kw.get('osm_id', 4004),
84 'parent_place_id': kw.get('parent_place_id'),
85 'indexed_date': kw.get('indexed_date',
86 dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
87 'startnumber': kw.get('startnumber', 2),
88 'endnumber': kw.get('endnumber', 6),
89 'step': kw.get('step', 2),
90 'address': kw.get('address'),
91 'postcode': kw.get('postcode'),
92 'country_code': kw.get('country_code'),
93 'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
96 def add_tiger(self, **kw):
97 self.add_data('tiger',
98 {'place_id': kw.get('place_id', 30000),
99 'parent_place_id': kw.get('parent_place_id'),
100 'startnumber': kw.get('startnumber', 2),
101 'endnumber': kw.get('endnumber', 6),
102 'step': kw.get('step', 2),
103 'postcode': kw.get('postcode'),
104 'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
106 async def exec_async(self, sql, *args, **kwargs):
107 async with self.api._async_api.begin() as conn:
108 return await conn.execute(sql, *args, **kwargs)
111 async def create_tables(self):
112 async with self.api._async_api._engine.begin() as conn:
113 await conn.run_sync(self.api._async_api._tables.meta.create_all)
117 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
118 """ Create an asynchronous SQLAlchemy engine for the test DB.
120 monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
121 testapi = APITester()
122 testapi.async_to_sync(testapi.create_tables())
124 SQLPreprocessor(temp_db_conn, testapi.api.config)\
125 .run_sql_file(temp_db_conn, 'functions/address_lookup.sql')