]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/conftest.py
bdd: fully check correctness of geojson and geocodejson
[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         self.add_data('placex',
46                      {'place_id': kw.get('place_id', 1000),
47                       'osm_type': kw.get('osm_type', 'W'),
48                       'osm_id': kw.get('osm_id', 4),
49                       'class_': kw.get('class_', 'highway'),
50                       'type': kw.get('type', 'residential'),
51                       'name': name,
52                       'address': kw.get('address'),
53                       'extratags': kw.get('extratags'),
54                       'parent_place_id': kw.get('parent_place_id'),
55                       'linked_place_id': kw.get('linked_place_id'),
56                       'admin_level': kw.get('admin_level', 15),
57                       'country_code': kw.get('country_code'),
58                       'housenumber': kw.get('housenumber'),
59                       'postcode': kw.get('postcode'),
60                       'wikipedia': kw.get('wikipedia'),
61                       'rank_search': kw.get('rank_search', 30),
62                       'rank_address': kw.get('rank_address', 30),
63                       'importance': kw.get('importance'),
64                       'centroid': 'SRID=4326;POINT(%f %f)' % kw.get('centroid', (23.0, 34.0)),
65                       'indexed_date': kw.get('indexed_date',
66                                              dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
67                       'geometry': 'SRID=4326;' + kw.get('geometry', 'POINT(23 34)')})
68
69
70     def add_address_placex(self, object_id, **kw):
71         self.add_placex(**kw)
72         self.add_data('addressline',
73                       {'place_id': object_id,
74                        'address_place_id': kw.get('place_id', 1000),
75                        'distance': kw.get('distance', 0.0),
76                        'cached_rank_address': kw.get('rank_address', 30),
77                        'fromarea': kw.get('fromarea', False),
78                        'isaddress': kw.get('isaddress', True)})
79
80
81     def add_osmline(self, **kw):
82         self.add_data('osmline',
83                      {'place_id': kw.get('place_id', 10000),
84                       'osm_id': kw.get('osm_id', 4004),
85                       'parent_place_id': kw.get('parent_place_id'),
86                       'indexed_date': kw.get('indexed_date',
87                                              dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
88                       'startnumber': kw.get('startnumber', 2),
89                       'endnumber': kw.get('endnumber', 6),
90                       'step': kw.get('step', 2),
91                       'address': kw.get('address'),
92                       'postcode': kw.get('postcode'),
93                       'country_code': kw.get('country_code'),
94                       'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
95
96
97     def add_tiger(self, **kw):
98         self.add_data('tiger',
99                      {'place_id': kw.get('place_id', 30000),
100                       'parent_place_id': kw.get('parent_place_id'),
101                       'startnumber': kw.get('startnumber', 2),
102                       'endnumber': kw.get('endnumber', 6),
103                       'step': kw.get('step', 2),
104                       'postcode': kw.get('postcode'),
105                       'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
106
107
108     def add_postcode(self, **kw):
109         self.add_data('postcode',
110                      {'place_id': kw.get('place_id', 1000),
111                       'parent_place_id': kw.get('parent_place_id'),
112                       'country_code': kw.get('country_code'),
113                       'postcode': kw.get('postcode'),
114                       'rank_search': kw.get('rank_search', 20),
115                       'rank_address': kw.get('rank_address', 22),
116                       'indexed_date': kw.get('indexed_date',
117                                              dt.datetime(2022, 12, 7, 14, 14, 46, 0)),
118                       'geometry': 'SRID=4326;' + kw.get('geometry', 'POINT(23 34)')})
119
120
121     async def exec_async(self, sql, *args, **kwargs):
122         async with self.api._async_api.begin() as conn:
123             return await conn.execute(sql, *args, **kwargs)
124
125
126     async def create_tables(self):
127         async with self.api._async_api._engine.begin() as conn:
128             await conn.run_sync(self.api._async_api._tables.meta.create_all)
129
130
131 @pytest.fixture
132 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
133     """ Create an asynchronous SQLAlchemy engine for the test DB.
134     """
135     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
136     testapi = APITester()
137     testapi.async_to_sync(testapi.create_tables())
138
139     SQLPreprocessor(temp_db_conn, testapi.api.config)\
140         .run_sql_file(temp_db_conn, 'functions/address_lookup.sql')
141
142     loglib.set_log_output('text')
143     yield testapi
144     print(loglib.get_and_disable())
145
146     testapi.api.close()