2 Tests for functions to import a new database.
4 from pathlib import Path
5 from contextlib import closing
10 from nominatim.tools import database_import
11 from nominatim.errors import UsageError
13 class TestDatabaseSetup:
14 DBNAME = 'test_nominatim_python_unittest'
16 @pytest.fixture(autouse=True)
17 def setup_nonexistant_db(self):
18 conn = psycopg2.connect(database='postgres')
21 conn.set_isolation_level(0)
22 with conn.cursor() as cur:
23 cur.execute(f'DROP DATABASE IF EXISTS {self.DBNAME}')
27 with conn.cursor() as cur:
28 cur.execute(f'DROP DATABASE IF EXISTS {self.DBNAME}')
34 conn = psycopg2.connect(database=self.DBNAME)
37 with conn.cursor() as cur:
44 return closing(psycopg2.connect(database=self.DBNAME))
47 def test_setup_skeleton(self):
48 database_import.setup_database_skeleton(f'dbname={self.DBNAME}')
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))')
56 def test_unsupported_pg_version(self, monkeypatch):
57 monkeypatch.setattr(database_import, 'POSTGRESQL_REQUIRED_VERSION', (100, 4))
59 with pytest.raises(UsageError, match='PostgreSQL server is too old.'):
60 database_import.setup_database_skeleton(f'dbname={self.DBNAME}')
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;')
69 def test_setup_extensions_old_postgis(self, monkeypatch):
70 monkeypatch.setattr(database_import, 'POSTGIS_REQUIRED_VERSION', (50, 50))
72 with pytest.raises(UsageError, match='PostGIS is too old.'):
73 database_import.setup_database_skeleton(f'dbname={self.DBNAME}')
76 def test_setup_skeleton_already_exists(temp_db):
77 with pytest.raises(UsageError):
78 database_import.setup_database_skeleton(f'dbname={temp_db}')
81 def test_import_osm_data_simple(table_factory, osm2pgsql_options, capfd):
82 table_factory('place', content=((1, ), ))
84 database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
85 captured = capfd.readouterr()
87 assert '--create' in captured.out
88 assert '--output gazetteer' in captured.out
89 assert f'--style {osm2pgsql_options["osm2pgsql_style"]}' in captured.out
90 assert f'--number-processes {osm2pgsql_options["threads"]}' in captured.out
91 assert f'--cache {osm2pgsql_options["osm2pgsql_cache"]}' in captured.out
92 assert 'file.pbf' in captured.out
95 def test_import_osm_data_multifile(table_factory, tmp_path, osm2pgsql_options, capfd):
96 table_factory('place', content=((1, ), ))
97 osm2pgsql_options['osm2pgsql_cache'] = 0
99 files = [tmp_path / 'file1.osm', tmp_path / 'file2.osm']
103 database_import.import_osm_data(files, osm2pgsql_options)
104 captured = capfd.readouterr()
106 assert 'file1.osm' in captured.out
107 assert 'file2.osm' in captured.out
110 def test_import_osm_data_simple_no_data(table_factory, osm2pgsql_options):
111 table_factory('place')
113 with pytest.raises(UsageError, match='No data.*'):
114 database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
117 def test_import_osm_data_drop(table_factory, temp_db_conn, tmp_path, osm2pgsql_options):
118 table_factory('place', content=((1, ), ))
119 table_factory('planet_osm_nodes')
121 flatfile = tmp_path / 'flatfile'
122 flatfile.write_text('touch')
124 osm2pgsql_options['flatnode_file'] = str(flatfile.resolve())
126 database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options, drop=True)
128 assert not flatfile.exists()
129 assert not temp_db_conn.table_exists('planet_osm_nodes')
132 def test_import_osm_data_default_cache(table_factory, osm2pgsql_options, capfd):
133 table_factory('place', content=((1, ), ))
135 osm2pgsql_options['osm2pgsql_cache'] = 0
137 database_import.import_osm_data(Path(__file__), osm2pgsql_options)
138 captured = capfd.readouterr()
140 assert f'--cache {osm2pgsql_options["osm2pgsql_cache"]}' in captured.out
143 def test_truncate_database_tables(temp_db_conn, temp_db_cursor, table_factory):
144 tables = ('placex', 'place_addressline', 'location_area',
145 'location_area_country',
146 'location_property_tiger', 'location_property_osmline',
147 'location_postcode', 'search_name', 'location_road_23')
149 table_factory(table, content=((1, ), (2, ), (3, )))
150 assert temp_db_cursor.table_rows(table) == 3
152 database_import.truncate_data_tables(temp_db_conn)
155 assert temp_db_cursor.table_rows(table) == 0
158 @pytest.mark.parametrize("threads", (1, 5))
159 def test_load_data(dsn, place_row, placex_table, osmline_table,
160 word_table, temp_db_cursor, threads):
161 for func in ('precompute_words', 'getorcreate_housenumber_id', 'make_standard_name'):
162 temp_db_cursor.execute("""CREATE FUNCTION {} (src TEXT)
163 RETURNS TEXT AS $$ SELECT 'a'::TEXT $$ LANGUAGE SQL
165 for oid in range(100, 130):
166 place_row(osm_id=oid)
167 place_row(osm_type='W', osm_id=342, cls='place', typ='houses',
168 geom='SRID=4326;LINESTRING(0 0, 10 10)')
170 database_import.load_data(dsn, threads)
172 assert temp_db_cursor.table_rows('placex') == 30
173 assert temp_db_cursor.table_rows('location_property_osmline') == 1