3 from pathlib import Path
9 SRC_DIR = Path(__file__) / '..' / '..' / '..'
11 # always test against the source
12 sys.path.insert(0, str(SRC_DIR.resolve()))
14 from nominatim.config import Configuration
15 from nominatim.db import connection
17 class _TestingCursor(psycopg2.extras.DictCursor):
18 """ Extension to the DictCursor class that provides execution
19 short-cuts that simplify writing assertions.
22 def scalar(self, sql, params=None):
23 """ Execute a query with a single return value and return this value.
24 Raises an assertion when not exactly one row is returned.
26 self.execute(sql, params)
27 assert self.rowcount == 1
28 return self.fetchone()[0]
30 def row_set(self, sql, params=None):
31 """ Execute a query and return the result as a set of tuples.
33 self.execute(sql, params)
34 if self.rowcount == 1:
35 return set(tuple(self.fetchone()))
37 return set((tuple(row) for row in self))
39 def table_exists(self, table):
40 """ Check that a table with the given name exists in the database.
42 num = self.scalar("""SELECT count(*) FROM pg_tables
43 WHERE tablename = %s""", (table, ))
48 def temp_db(monkeypatch):
49 """ Create an empty database for the test. The database name is also
50 exported into NOMINATIM_DATABASE_DSN.
52 name = 'test_nominatim_python_unittest'
53 conn = psycopg2.connect(database='postgres')
55 conn.set_isolation_level(0)
56 with conn.cursor() as cur:
57 cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
58 cur.execute('CREATE DATABASE {}'.format(name))
62 monkeypatch.setenv('NOMINATIM_DATABASE_DSN' , 'dbname=' + name)
66 conn = psycopg2.connect(database='postgres')
68 conn.set_isolation_level(0)
69 with conn.cursor() as cur:
70 cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
75 def temp_db_with_extensions(temp_db):
76 conn = psycopg2.connect(database=temp_db)
77 with conn.cursor() as cur:
78 cur.execute('CREATE EXTENSION hstore; CREATE EXTENSION postgis;')
85 def temp_db_conn(temp_db):
86 """ Connection to the test database.
88 with connection.connect('dbname=' + temp_db) as conn:
93 def temp_db_cursor(temp_db):
94 """ Connection and cursor towards the test database. The connection will
95 be in auto-commit mode.
97 conn = psycopg2.connect('dbname=' + temp_db)
98 conn.set_isolation_level(0)
99 with conn.cursor(cursor_factory=_TestingCursor) as cur:
106 return Configuration(None, SRC_DIR.resolve() / 'settings')
110 def status_table(temp_db_conn):
111 """ Create an empty version of the status table and
112 the status logging table.
114 with temp_db_conn.cursor() as cur:
115 cur.execute("""CREATE TABLE import_status (
116 lastimportdate timestamp with time zone NOT NULL,
120 cur.execute("""CREATE TABLE import_osmosis_log (
128 temp_db_conn.commit()
132 def place_table(temp_db_with_extensions, temp_db_conn):
133 """ Create an empty version of the place table.
135 with temp_db_conn.cursor() as cur:
136 cur.execute("""CREATE TABLE place (
137 osm_id int8 NOT NULL,
138 osm_type char(1) NOT NULL,
142 admin_level smallint,
145 geometry Geometry(Geometry,4326) NOT NULL)""")
146 temp_db_conn.commit()
150 def place_row(place_table, temp_db_cursor):
151 """ A factory for rows in the place table. The table is created as a
152 prerequisite to the fixture.
154 idseq = itertools.count(1001)
155 def _insert(osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
156 admin_level=None, address=None, extratags=None, geom=None):
157 temp_db_cursor.execute("INSERT INTO place VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
158 (osm_id or next(idseq), osm_type, cls, typ, names,
159 admin_level, address, extratags,
160 geom or 'SRID=4326;POINT(0 0 )'))
165 def placex_table(temp_db_with_extensions, temp_db_conn):
166 """ Create an empty version of the place table.
168 with temp_db_conn.cursor() as cur:
169 cur.execute("""CREATE TABLE placex (
170 place_id BIGINT NOT NULL,
171 parent_place_id BIGINT,
172 linked_place_id BIGINT,
174 indexed_date TIMESTAMP,
175 geometry_sector INTEGER,
176 rank_address SMALLINT,
177 rank_search SMALLINT,
179 indexed_status SMALLINT,
185 admin_level smallint,
188 geometry Geometry(Geometry,4326),
190 country_code varchar(2),
193 centroid GEOMETRY(Geometry, 4326))
195 temp_db_conn.commit()