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):
 
  82     table_factory('place', content=((1, ), ))
 
  84     database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
 
  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
 
  91     files = [tmp_path / 'file1.osm', tmp_path / 'file2.osm']
 
  95     database_import.import_osm_data(files, osm2pgsql_options)
 
  98 def test_import_osm_data_simple_no_data(table_factory, osm2pgsql_options):
 
  99     table_factory('place')
 
 101     with pytest.raises(UsageError, match='No data.*'):
 
 102         database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
 
 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')
 
 109     flatfile = tmp_path / 'flatfile'
 
 110     flatfile.write_text('touch')
 
 112     osm2pgsql_options['flatnode_file'] = str(flatfile.resolve())
 
 114     database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options, drop=True)
 
 116     assert not flatfile.exists()
 
 117     assert not temp_db_conn.table_exists('planet_osm_nodes')
 
 120 def test_import_osm_data_default_cache(table_factory, osm2pgsql_options):
 
 121     table_factory('place', content=((1, ), ))
 
 123     osm2pgsql_options['osm2pgsql_cache'] = 0
 
 125     database_import.import_osm_data(Path(__file__), osm2pgsql_options)
 
 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')
 
 134         table_factory(table, content=((1, ), (2, ), (3, )))
 
 135         assert temp_db_cursor.table_rows(table) == 3
 
 137     database_import.truncate_data_tables(temp_db_conn)
 
 140         assert temp_db_cursor.table_rows(table) == 0
 
 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
 
 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)')
 
 155     database_import.load_data(dsn, threads)
 
 157     assert temp_db_cursor.table_rows('placex') == 30
 
 158     assert temp_db_cursor.table_rows('location_property_osmline') == 1