]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_database_import.py
organise python tests in subdirectories
[nominatim.git] / test / python / tools / test_database_import.py
1 """
2 Tests for functions to import a new database.
3 """
4 from pathlib import Path
5 from contextlib import closing
6
7 import pytest
8 import psycopg2
9
10 from nominatim.tools import database_import
11 from nominatim.errors import UsageError
12
13 class TestDatabaseSetup:
14     DBNAME = 'test_nominatim_python_unittest'
15
16     @pytest.fixture(autouse=True)
17     def setup_nonexistant_db(self):
18         conn = psycopg2.connect(database='postgres')
19
20         try:
21             conn.set_isolation_level(0)
22             with conn.cursor() as cur:
23                 cur.execute(f'DROP DATABASE IF EXISTS {self.DBNAME}')
24
25             yield True
26
27             with conn.cursor() as cur:
28                 cur.execute(f'DROP DATABASE IF EXISTS {self.DBNAME}')
29         finally:
30             conn.close()
31
32     @pytest.fixture
33     def cursor(self):
34         conn = psycopg2.connect(database=self.DBNAME)
35
36         try:
37             with conn.cursor() as cur:
38                 yield cur
39         finally:
40             conn.close()
41
42
43     def conn(self):
44         return closing(psycopg2.connect(database=self.DBNAME))
45
46
47     def test_setup_skeleton(self):
48         database_import.setup_database_skeleton(f'dbname={self.DBNAME}')
49
50         # Check that all extensions are set up.
51         with self.conn() as conn:
52             with conn.cursor() as cur:
53                 cur.execute('CREATE TABLE t (h HSTORE, geom GEOMETRY(Geometry, 4326))')
54
55
56     def test_unsupported_pg_version(self, monkeypatch):
57         monkeypatch.setattr(database_import, 'POSTGRESQL_REQUIRED_VERSION', (100, 4))
58
59         with pytest.raises(UsageError, match='PostgreSQL server is too old.'):
60             database_import.setup_database_skeleton(f'dbname={self.DBNAME}')
61
62
63     def test_create_db_missing_ro_user(self):
64         with pytest.raises(UsageError, match='Missing read-only user.'):
65             database_import.setup_database_skeleton(f'dbname={self.DBNAME}',
66                                                     rouser='sdfwkjkjgdugu2;jgsafkljas;')
67
68
69     def test_setup_extensions_old_postgis(self, monkeypatch):
70         monkeypatch.setattr(database_import, 'POSTGIS_REQUIRED_VERSION', (50, 50))
71
72         with pytest.raises(UsageError, match='PostGIS is too old.'):
73             database_import.setup_database_skeleton(f'dbname={self.DBNAME}')
74
75
76 def test_setup_skeleton_already_exists(temp_db):
77     with pytest.raises(UsageError):
78         database_import.setup_database_skeleton(f'dbname={temp_db}')
79
80
81 def test_import_osm_data_simple(table_factory, osm2pgsql_options):
82     table_factory('place', content=((1, ), ))
83
84     database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
85
86
87 def test_import_osm_data_multifile(table_factory, tmp_path, osm2pgsql_options):
88     table_factory('place', content=((1, ), ))
89     osm2pgsql_options['osm2pgsql_cache'] = 0
90
91     files = [tmp_path / 'file1.osm', tmp_path / 'file2.osm']
92     for f in files:
93         f.write_text('test')
94
95     database_import.import_osm_data(files, osm2pgsql_options)
96
97
98 def test_import_osm_data_simple_no_data(table_factory, osm2pgsql_options):
99     table_factory('place')
100
101     with pytest.raises(UsageError, match='No data.*'):
102         database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
103
104
105 def test_import_osm_data_drop(table_factory, temp_db_conn, tmp_path, osm2pgsql_options):
106     table_factory('place', content=((1, ), ))
107     table_factory('planet_osm_nodes')
108
109     flatfile = tmp_path / 'flatfile'
110     flatfile.write_text('touch')
111
112     osm2pgsql_options['flatnode_file'] = str(flatfile.resolve())
113
114     database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options, drop=True)
115
116     assert not flatfile.exists()
117     assert not temp_db_conn.table_exists('planet_osm_nodes')
118
119
120 def test_import_osm_data_default_cache(table_factory, osm2pgsql_options):
121     table_factory('place', content=((1, ), ))
122
123     osm2pgsql_options['osm2pgsql_cache'] = 0
124
125     database_import.import_osm_data(Path(__file__), osm2pgsql_options)
126
127
128 def test_truncate_database_tables(temp_db_conn, temp_db_cursor, table_factory):
129     tables = ('placex', 'place_addressline', 'location_area',
130               'location_area_country',
131               'location_property_tiger', 'location_property_osmline',
132               'location_postcode', 'search_name', 'location_road_23')
133     for table in tables:
134         table_factory(table, content=((1, ), (2, ), (3, )))
135         assert temp_db_cursor.table_rows(table) == 3
136
137     database_import.truncate_data_tables(temp_db_conn)
138
139     for table in tables:
140         assert temp_db_cursor.table_rows(table) == 0
141
142
143 @pytest.mark.parametrize("threads", (1, 5))
144 def test_load_data(dsn, place_row, placex_table, osmline_table,
145                    word_table, temp_db_cursor, threads):
146     for func in ('precompute_words', 'getorcreate_housenumber_id', 'make_standard_name'):
147         temp_db_cursor.execute("""CREATE FUNCTION {} (src TEXT)
148                                   RETURNS TEXT AS $$ SELECT 'a'::TEXT $$ LANGUAGE SQL
149                                """.format(func))
150     for oid in range(100, 130):
151         place_row(osm_id=oid)
152     place_row(osm_type='W', osm_id=342, cls='place', typ='houses',
153               geom='SRID=4326;LINESTRING(0 0, 10 10)')
154
155     database_import.load_data(dsn, threads)
156
157     assert temp_db_cursor.table_rows('placex') == 30
158     assert temp_db_cursor.table_rows('location_property_osmline') == 1