]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/conftest.py
add tests for parameter converter
[nominatim.git] / test / python / api / conftest.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Helper fixtures for API call tests.
9 """
10 from pathlib import Path
11 import pytest
12 import time
13 import datetime as dt
14
15 import nominatim.api as napi
16 from nominatim.db.sql_preprocessor import SQLPreprocessor
17 import nominatim.api.logging as loglib
18
19 class APITester:
20
21     def __init__(self):
22         self.api = napi.NominatimAPI(Path('/invalid'))
23         self.async_to_sync(self.api._async_api.setup_database())
24
25
26     def async_to_sync(self, func):
27         """ Run an asynchronous function until completion using the
28             internal loop of the API.
29         """
30         return self.api._loop.run_until_complete(func)
31
32
33     def add_data(self, table, data):
34         """ Insert data into the given table.
35         """
36         sql = getattr(self.api._async_api._tables, table).insert()
37         self.async_to_sync(self.exec_async(sql, data))
38
39
40     def add_placex(self, **kw):
41         name = kw.get('name')
42         if isinstance(name, str):
43             name = {'name': name}
44
45         centroid = kw.get('centroid', (23.0, 34.0))
46         geometry = kw.get('geometry', 'POINT(%f %f)' % centroid)
47
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'),
54                       'name': name,
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})
72
73
74     def add_address_placex(self, object_id, **kw):
75         self.add_placex(**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)})
83
84
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)')})
99
100
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)')})
110
111
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)')})
123
124
125     def add_country(self, country_code, geometry):
126         self.add_data('country_grid',
127                       {'country_code': country_code,
128                        'area': 0.1,
129                        'geometry': 'SRID=4326;' + geometry})
130
131
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)
135
136
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)
140
141
142 @pytest.fixture
143 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
144     """ Create an asynchronous SQLAlchemy engine for the test DB.
145     """
146     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
147     testapi = APITester()
148     testapi.async_to_sync(testapi.create_tables())
149
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')
153
154     loglib.set_log_output('text')
155     yield testapi
156     print(loglib.get_and_disable())
157
158     testapi.api.close()