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
17 import nominatim.api.logging as loglib
22 self.api = napi.NominatimAPI(Path('/invalid'))
23 self.async_to_sync(self.api._async_api.setup_database())
26 def async_to_sync(self, func):
27 """ Run an asynchronous function until completion using the
28 internal loop of the API.
30 return self.api._loop.run_until_complete(func)
33 def add_data(self, table, data):
34 """ Insert data into the given table.
36 sql = getattr(self.api._async_api._tables, table).insert()
37 self.async_to_sync(self.exec_async(sql, data))
40 def add_placex(self, **kw):
42 if isinstance(name, str):
45 centroid = kw.get('centroid', (23.0, 34.0))
46 geometry = kw.get('geometry', 'POINT(%f %f)' % centroid)
48 self.add_data('placex',
49 {'place_id': kw.get('place_id', 1000),
50 'osm_type': kw.get('osm_type', 'W'),
51 'osm_id': kw.get('osm_id', 4),
52 'class_': kw.get('class_', 'highway'),
53 'type': kw.get('type', 'residential'),
55 'address': kw.get('address'),
56 'extratags': kw.get('extratags'),
57 'parent_place_id': kw.get('parent_place_id'),
58 'linked_place_id': kw.get('linked_place_id'),
59 'admin_level': kw.get('admin_level', 15),
60 'country_code': kw.get('country_code'),
61 'housenumber': kw.get('housenumber'),
62 'postcode': kw.get('postcode'),
63 'wikipedia': kw.get('wikipedia'),
64 'rank_search': kw.get('rank_search', 30),
65 'rank_address': kw.get('rank_address', 30),
66 'importance': kw.get('importance'),
67 'centroid': 'SRID=4326;POINT(%f %f)' % centroid,
68 'indexed_status': kw.get('indexed_status', 0),
69 'indexed_date': kw.get('indexed_date',
70 dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
71 'geometry': 'SRID=4326;' + geometry})
74 def add_address_placex(self, object_id, **kw):
76 self.add_data('addressline',
77 {'place_id': object_id,
78 'address_place_id': kw.get('place_id', 1000),
79 'distance': kw.get('distance', 0.0),
80 'cached_rank_address': kw.get('rank_address', 30),
81 'fromarea': kw.get('fromarea', False),
82 'isaddress': kw.get('isaddress', True)})
85 def add_osmline(self, **kw):
86 self.add_data('osmline',
87 {'place_id': kw.get('place_id', 10000),
88 'osm_id': kw.get('osm_id', 4004),
89 'parent_place_id': kw.get('parent_place_id'),
90 'indexed_date': kw.get('indexed_date',
91 dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
92 'startnumber': kw.get('startnumber', 2),
93 'endnumber': kw.get('endnumber', 6),
94 'step': kw.get('step', 2),
95 'address': kw.get('address'),
96 'postcode': kw.get('postcode'),
97 'country_code': kw.get('country_code'),
98 'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
101 def add_tiger(self, **kw):
102 self.add_data('tiger',
103 {'place_id': kw.get('place_id', 30000),
104 'parent_place_id': kw.get('parent_place_id'),
105 'startnumber': kw.get('startnumber', 2),
106 'endnumber': kw.get('endnumber', 6),
107 'step': kw.get('step', 2),
108 'postcode': kw.get('postcode'),
109 'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
112 def add_postcode(self, **kw):
113 self.add_data('postcode',
114 {'place_id': kw.get('place_id', 1000),
115 'parent_place_id': kw.get('parent_place_id'),
116 'country_code': kw.get('country_code'),
117 'postcode': kw.get('postcode'),
118 'rank_search': kw.get('rank_search', 20),
119 'rank_address': kw.get('rank_address', 22),
120 'indexed_date': kw.get('indexed_date',
121 dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
122 'geometry': 'SRID=4326;' + kw.get('geometry', 'POINT(23 34)')})
125 def add_country(self, country_code, geometry):
126 self.add_data('country_grid',
127 {'country_code': country_code,
129 'geometry': 'SRID=4326;' + geometry})
132 async def exec_async(self, sql, *args, **kwargs):
133 async with self.api._async_api.begin() as conn:
134 return await conn.execute(sql, *args, **kwargs)
137 async def create_tables(self):
138 async with self.api._async_api._engine.begin() as conn:
139 await conn.run_sync(self.api._async_api._tables.meta.create_all)
143 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
144 """ Create an asynchronous SQLAlchemy engine for the test DB.
146 monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
147 testapi = APITester()
148 testapi.async_to_sync(testapi.create_tables())
150 proc = SQLPreprocessor(temp_db_conn, testapi.api.config)
151 proc.run_sql_file(temp_db_conn, 'functions/address_lookup.sql')
152 proc.run_sql_file(temp_db_conn, 'functions/ranking.sql')
154 loglib.set_log_output('text')
156 print(loglib.get_and_disable())