]> git.openstreetmap.org Git - nominatim.git/blob - test/python/conftest.py
port freeze function to python
[nominatim.git] / test / python / conftest.py
1 import itertools
2 import sys
3 from pathlib import Path
4
5 import psycopg2
6 import psycopg2.extras
7 import pytest
8
9 SRC_DIR = Path(__file__) / '..' / '..' / '..'
10
11 # always test against the source
12 sys.path.insert(0, str(SRC_DIR.resolve()))
13
14 from nominatim.config import Configuration
15 from nominatim.db import connection
16
17 class _TestingCursor(psycopg2.extras.DictCursor):
18     """ Extension to the DictCursor class that provides execution
19         short-cuts that simplify writing assertions.
20     """
21
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.
25         """
26         self.execute(sql, params)
27         assert self.rowcount == 1
28         return self.fetchone()[0]
29
30     def row_set(self, sql, params=None):
31         """ Execute a query and return the result as a set of tuples.
32         """
33         self.execute(sql, params)
34         if self.rowcount == 1:
35             return set(tuple(self.fetchone()))
36
37         return set((tuple(row) for row in self))
38
39     def table_exists(self, table):
40         """ Check that a table with the given name exists in the database.
41         """
42         num = self.scalar("""SELECT count(*) FROM pg_tables
43                              WHERE tablename = %s""", (table, ))
44         return num == 1
45
46
47 @pytest.fixture
48 def temp_db(monkeypatch):
49     """ Create an empty database for the test. The database name is also
50         exported into NOMINATIM_DATABASE_DSN.
51     """
52     name = 'test_nominatim_python_unittest'
53     conn = psycopg2.connect(database='postgres')
54
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))
59
60     conn.close()
61
62     monkeypatch.setenv('NOMINATIM_DATABASE_DSN' , 'dbname=' + name)
63
64     yield name
65
66     conn = psycopg2.connect(database='postgres')
67
68     conn.set_isolation_level(0)
69     with conn.cursor() as cur:
70         cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
71
72     conn.close()
73
74 @pytest.fixture
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;')
79     conn.commit()
80     conn.close()
81
82     return temp_db
83
84 @pytest.fixture
85 def temp_db_conn(temp_db):
86     """ Connection to the test database.
87     """
88     conn = connection.connect('dbname=' + temp_db)
89     yield conn
90     conn.close()
91
92
93 @pytest.fixture
94 def temp_db_cursor(temp_db):
95     """ Connection and cursor towards the test database. The connection will
96         be in auto-commit mode.
97     """
98     conn = psycopg2.connect('dbname=' + temp_db)
99     conn.set_isolation_level(0)
100     with conn.cursor(cursor_factory=_TestingCursor) as cur:
101         yield cur
102     conn.close()
103
104
105 @pytest.fixture
106 def def_config():
107     return Configuration(None, SRC_DIR.resolve() / 'settings')
108
109
110 @pytest.fixture
111 def status_table(temp_db_conn):
112     """ Create an empty version of the status table and
113         the status logging table.
114     """
115     with temp_db_conn.cursor() as cur:
116         cur.execute("""CREATE TABLE import_status (
117                            lastimportdate timestamp with time zone NOT NULL,
118                            sequence_id integer,
119                            indexed boolean
120                        )""")
121         cur.execute("""CREATE TABLE import_osmosis_log (
122                            batchend timestamp,
123                            batchseq integer,
124                            batchsize bigint,
125                            starttime timestamp,
126                            endtime timestamp,
127                            event text
128                            )""")
129     temp_db_conn.commit()
130
131
132 @pytest.fixture
133 def place_table(temp_db_with_extensions, temp_db_conn):
134     """ Create an empty version of the place table.
135     """
136     with temp_db_conn.cursor() as cur:
137         cur.execute("""CREATE TABLE place (
138                            osm_id int8 NOT NULL,
139                            osm_type char(1) NOT NULL,
140                            class text NOT NULL,
141                            type text NOT NULL,
142                            name hstore,
143                            admin_level smallint,
144                            address hstore,
145                            extratags hstore,
146                            geometry Geometry(Geometry,4326) NOT NULL)""")
147     temp_db_conn.commit()
148
149
150 @pytest.fixture
151 def place_row(place_table, temp_db_cursor):
152     """ A factory for rows in the place table. The table is created as a
153         prerequisite to the fixture.
154     """
155     idseq = itertools.count(1001)
156     def _insert(osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
157                 admin_level=None, address=None, extratags=None, geom=None):
158         temp_db_cursor.execute("INSERT INTO place VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
159                                (osm_id or next(idseq), osm_type, cls, typ, names,
160                                 admin_level, address, extratags,
161                                 geom or 'SRID=4326;POINT(0 0 )'))
162
163     return _insert
164
165 @pytest.fixture
166 def placex_table(temp_db_with_extensions, temp_db_conn):
167     """ Create an empty version of the place table.
168     """
169     with temp_db_conn.cursor() as cur:
170         cur.execute("""CREATE TABLE placex (
171                            place_id BIGINT NOT NULL,
172                            parent_place_id BIGINT,
173                            linked_place_id BIGINT,
174                            importance FLOAT,
175                            indexed_date TIMESTAMP,
176                            geometry_sector INTEGER,
177                            rank_address SMALLINT,
178                            rank_search SMALLINT,
179                            partition SMALLINT,
180                            indexed_status SMALLINT,
181                            osm_id int8,
182                            osm_type char(1),
183                            class text,
184                            type text,
185                            name hstore,
186                            admin_level smallint,
187                            address hstore,
188                            extratags hstore,
189                            geometry Geometry(Geometry,4326),
190                            wikipedia TEXT,
191                            country_code varchar(2),
192                            housenumber TEXT,
193                            postcode TEXT,
194                            centroid GEOMETRY(Geometry, 4326))
195                            """)
196     temp_db_conn.commit()
197
198
199