3 from pathlib import Path
8 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
10 # always test against the source
11 sys.path.insert(0, str(SRC_DIR.resolve()))
13 from nominatim.config import Configuration
14 from nominatim.db import connection
15 from nominatim.db.sql_preprocessor import SQLPreprocessor
16 import nominatim.tokenizer.factory
18 import dummy_tokenizer
20 from cursor import CursorForTesting
24 def temp_db(monkeypatch):
25 """ Create an empty database for the test. The database name is also
26 exported into NOMINATIM_DATABASE_DSN.
28 name = 'test_nominatim_python_unittest'
29 conn = psycopg2.connect(database='postgres')
31 conn.set_isolation_level(0)
32 with conn.cursor() as cur:
33 cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
34 cur.execute('CREATE DATABASE {}'.format(name))
38 monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'dbname=' + name)
42 conn = psycopg2.connect(database='postgres')
44 conn.set_isolation_level(0)
45 with conn.cursor() as cur:
46 cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
53 return 'dbname=' + temp_db
57 def temp_db_with_extensions(temp_db):
58 conn = psycopg2.connect(database=temp_db)
59 with conn.cursor() as cur:
60 cur.execute('CREATE EXTENSION hstore; CREATE EXTENSION postgis;')
67 def temp_db_conn(temp_db):
68 """ Connection to the test database.
70 with connection.connect('dbname=' + temp_db) as conn:
75 def temp_db_cursor(temp_db):
76 """ Connection and cursor towards the test database. The connection will
77 be in auto-commit mode.
79 conn = psycopg2.connect('dbname=' + temp_db)
80 conn.set_isolation_level(0)
81 with conn.cursor(cursor_factory=CursorForTesting) as cur:
87 def table_factory(temp_db_cursor):
88 """ A fixture that creates new SQL tables, potentially filled with
91 def mk_table(name, definition='id INT', content=None):
92 temp_db_cursor.execute('CREATE TABLE {} ({})'.format(name, definition))
93 if content is not None:
94 temp_db_cursor.execute_values("INSERT INTO {} VALUES %s".format(name), content)
101 cfg = Configuration(None, SRC_DIR.resolve() / 'settings')
102 cfg.set_libdirs(module='.', osm2pgsql='.',
103 php=SRC_DIR / 'lib-php',
104 sql=SRC_DIR / 'lib-sql',
105 data=SRC_DIR / 'data')
111 return SRC_DIR.resolve()
115 def property_table(table_factory, temp_db_conn):
116 table_factory('nominatim_properties', 'property TEXT, value TEXT')
118 return mocks.MockPropertyTable(temp_db_conn)
122 def status_table(table_factory):
123 """ Create an empty version of the status table and
124 the status logging table.
126 table_factory('import_status',
127 """lastimportdate timestamp with time zone NOT NULL,
130 table_factory('import_osmosis_log',
131 """batchend timestamp,
140 def place_table(temp_db_with_extensions, table_factory):
141 """ Create an empty version of the place table.
143 table_factory('place',
144 """osm_id int8 NOT NULL,
145 osm_type char(1) NOT NULL,
149 admin_level smallint,
152 geometry Geometry(Geometry,4326) NOT NULL""")
156 def place_row(place_table, temp_db_cursor):
157 """ A factory for rows in the place table. The table is created as a
158 prerequisite to the fixture.
160 psycopg2.extras.register_hstore(temp_db_cursor)
161 idseq = itertools.count(1001)
162 def _insert(osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
163 admin_level=None, address=None, extratags=None, geom=None):
164 temp_db_cursor.execute("INSERT INTO place VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
165 (osm_id or next(idseq), osm_type, cls, typ, names,
166 admin_level, address, extratags,
167 geom or 'SRID=4326;POINT(0 0)'))
172 def placex_table(temp_db_with_extensions, temp_db_conn):
173 """ Create an empty version of the place table.
175 return mocks.MockPlacexTable(temp_db_conn)
179 def osmline_table(temp_db_with_extensions, table_factory):
180 table_factory('location_property_osmline',
183 parent_place_id BIGINT,
184 geometry_sector INTEGER,
185 indexed_date TIMESTAMP,
189 indexed_status SMALLINT,
191 interpolationtype TEXT,
194 country_code VARCHAR(2)""")
198 def word_table(temp_db_conn):
199 return mocks.MockWordTable(temp_db_conn)
203 def sql_preprocessor_cfg(tmp_path, table_factory, temp_db_with_extensions):
204 table_factory('country_name', 'partition INT', ((0, ), (1, ), (2, )))
205 cfg = Configuration(None, SRC_DIR.resolve() / 'settings')
206 cfg.set_libdirs(module='.', osm2pgsql='.', php=SRC_DIR / 'lib-php',
207 sql=tmp_path, data=SRC_DIR / 'data')
212 def sql_preprocessor(sql_preprocessor_cfg, temp_db_conn):
213 return SQLPreprocessor(temp_db_conn, sql_preprocessor_cfg)
217 def tokenizer_mock(monkeypatch, property_table):
218 """ Sets up the configuration so that the test dummy tokenizer will be
219 loaded when the tokenizer factory is used. Also returns a factory
220 with which a new dummy tokenizer may be created.
222 monkeypatch.setenv('NOMINATIM_TOKENIZER', 'dummy')
224 def _import_dummy(*args, **kwargs):
225 return dummy_tokenizer
227 monkeypatch.setattr(nominatim.tokenizer.factory, "_import_tokenizer", _import_dummy)
228 property_table.set('tokenizer', 'dummy')
230 def _create_tokenizer():
231 return dummy_tokenizer.DummyTokenizer(None, None)
233 return _create_tokenizer