]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/conftest.py
add lookup of tiger data
[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
18 class APITester:
19
20     def __init__(self):
21         self.api = napi.NominatimAPI(Path('/invalid'))
22         self.async_to_sync(self.api._async_api.setup_database())
23
24
25     def async_to_sync(self, func):
26         """ Run an asynchronous function until completion using the
27             internal loop of the API.
28         """
29         return self.api._loop.run_until_complete(func)
30
31
32     def add_data(self, table, data):
33         """ Insert data into the given table.
34         """
35         sql = getattr(self.api._async_api._tables, table).insert()
36         self.async_to_sync(self.exec_async(sql, data))
37
38
39     def add_placex(self, **kw):
40         name = kw.get('name')
41         if isinstance(name, str):
42             name = {'name': name}
43
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'),
50                       'name': name,
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)')})
67
68
69     def add_address_placex(self, object_id, **kw):
70         self.add_placex(**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)})
78
79
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)')})
94
95
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)')})
105
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)
109
110
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)
114
115
116 @pytest.fixture
117 def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
118     """ Create an asynchronous SQLAlchemy engine for the test DB.
119     """
120     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
121     testapi = APITester()
122     testapi.async_to_sync(testapi.create_tables())
123
124     SQLPreprocessor(temp_db_conn, testapi.api.config)\
125         .run_sql_file(temp_db_conn, 'functions/address_lookup.sql')
126
127     yield testapi
128
129     testapi.api.close()