3 from pathlib import Path
8 SRC_DIR = Path(__file__) / '..' / '..' / '..'
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 from nominatim.db import properties
17 import nominatim.tokenizer.factory
20 import dummy_tokenizer
22 from cursor import CursorForTesting
26 def temp_db(monkeypatch):
27 """ Create an empty database for the test. The database name is also
28 exported into NOMINATIM_DATABASE_DSN.
30 name = 'test_nominatim_python_unittest'
31 conn = psycopg2.connect(database='postgres')
33 conn.set_isolation_level(0)
34 with conn.cursor() as cur:
35 cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
36 cur.execute('CREATE DATABASE {}'.format(name))
40 monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'dbname=' + name)
44 conn = psycopg2.connect(database='postgres')
46 conn.set_isolation_level(0)
47 with conn.cursor() as cur:
48 cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
55 return 'dbname=' + temp_db
59 def temp_db_with_extensions(temp_db):
60 conn = psycopg2.connect(database=temp_db)
61 with conn.cursor() as cur:
62 cur.execute('CREATE EXTENSION hstore; CREATE EXTENSION postgis;')
69 def temp_db_conn(temp_db):
70 """ Connection to the test database.
72 with connection.connect('dbname=' + temp_db) as conn:
77 def temp_db_cursor(temp_db):
78 """ Connection and cursor towards the test database. The connection will
79 be in auto-commit mode.
81 conn = psycopg2.connect('dbname=' + temp_db)
82 conn.set_isolation_level(0)
83 with conn.cursor(cursor_factory=CursorForTesting) as cur:
89 def table_factory(temp_db_cursor):
90 """ A fixture that creates new SQL tables, potentially filled with
93 def mk_table(name, definition='id INT', content=None):
94 temp_db_cursor.execute('CREATE TABLE {} ({})'.format(name, definition))
95 if content is not None:
96 temp_db_cursor.execute_values("INSERT INTO {} VALUES %s".format(name), content)
103 cfg = Configuration(None, SRC_DIR.resolve() / 'settings')
104 cfg.set_libdirs(module='.', osm2pgsql='.',
105 php=SRC_DIR / 'lib-php',
106 sql=SRC_DIR / 'lib-sql',
107 data=SRC_DIR / 'data')
113 return SRC_DIR.resolve()
118 def _call_nominatim(*args):
119 return nominatim.cli.nominatim(
120 module_dir='MODULE NOT AVAILABLE',
121 osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
122 phplib_dir=str(SRC_DIR / 'lib-php'),
123 data_dir=str(SRC_DIR / 'data'),
124 phpcgi_path='/usr/bin/php-cgi',
125 sqllib_dir=str(SRC_DIR / 'lib-sql'),
126 config_dir=str(SRC_DIR / 'settings'),
129 return _call_nominatim
133 def property_table(table_factory):
134 table_factory('nominatim_properties', 'property TEXT, value TEXT')
138 def status_table(temp_db_conn):
139 """ Create an empty version of the status table and
140 the status logging table.
142 with temp_db_conn.cursor() as cur:
143 cur.execute("""CREATE TABLE import_status (
144 lastimportdate timestamp with time zone NOT NULL,
148 cur.execute("""CREATE TABLE import_osmosis_log (
156 temp_db_conn.commit()
160 def place_table(temp_db_with_extensions, temp_db_conn):
161 """ Create an empty version of the place table.
163 with temp_db_conn.cursor() as cur:
164 cur.execute("""CREATE TABLE place (
165 osm_id int8 NOT NULL,
166 osm_type char(1) NOT NULL,
170 admin_level smallint,
173 geometry Geometry(Geometry,4326) NOT NULL)""")
174 temp_db_conn.commit()
178 def place_row(place_table, temp_db_cursor):
179 """ A factory for rows in the place table. The table is created as a
180 prerequisite to the fixture.
182 idseq = itertools.count(1001)
183 def _insert(osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
184 admin_level=None, address=None, extratags=None, geom=None):
185 temp_db_cursor.execute("INSERT INTO place VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
186 (osm_id or next(idseq), osm_type, cls, typ, names,
187 admin_level, address, extratags,
188 geom or 'SRID=4326;POINT(0 0)'))
193 def placex_table(temp_db_with_extensions, temp_db_conn):
194 """ Create an empty version of the place table.
196 return mocks.MockPlacexTable(temp_db_conn)
200 def osmline_table(temp_db_with_extensions, temp_db_conn):
201 with temp_db_conn.cursor() as cur:
202 cur.execute("""CREATE TABLE location_property_osmline (
205 parent_place_id BIGINT,
206 geometry_sector INTEGER,
207 indexed_date TIMESTAMP,
211 indexed_status SMALLINT,
213 interpolationtype TEXT,
216 country_code VARCHAR(2))""")
217 temp_db_conn.commit()
221 def word_table(temp_db_conn):
222 return mocks.MockWordTable(temp_db_conn)
226 def osm2pgsql_options(temp_db):
227 return dict(osm2pgsql='echo',
229 osm2pgsql_style='style.file',
231 dsn='dbname=' + temp_db,
233 tablespaces=dict(slim_data='', slim_index='',
234 main_data='', main_index=''))
237 def sql_preprocessor(temp_db_conn, tmp_path, monkeypatch, table_factory):
238 table_factory('country_name', 'partition INT', ((0, ), (1, ), (2, )))
239 cfg = Configuration(None, SRC_DIR.resolve() / 'settings')
240 cfg.set_libdirs(module='.', osm2pgsql='.', php=SRC_DIR / 'lib-php',
241 sql=tmp_path, data=SRC_DIR / 'data')
243 return SQLPreprocessor(temp_db_conn, cfg)
247 def tokenizer_mock(monkeypatch, property_table, temp_db_conn, tmp_path):
248 """ Sets up the configuration so that the test dummy tokenizer will be
249 loaded when the tokenizer factory is used. Also returns a factory
250 with which a new dummy tokenizer may be created.
252 monkeypatch.setenv('NOMINATIM_TOKENIZER', 'dummy')
254 def _import_dummy(module, *args, **kwargs):
255 return dummy_tokenizer
257 monkeypatch.setattr(nominatim.tokenizer.factory, "_import_tokenizer", _import_dummy)
258 properties.set_property(temp_db_conn, 'tokenizer', 'dummy')
260 def _create_tokenizer():
261 return dummy_tokenizer.DummyTokenizer(None, None)
263 return _create_tokenizer