2 Tests for functions to import a new database.
8 from nominatim.tools import database_import
9 from nominatim.errors import UsageError
13 dbname = 'test_nominatim_python_unittest'
15 conn = psycopg2.connect(database='postgres')
17 conn.set_isolation_level(0)
18 with conn.cursor() as cur:
19 cur.execute('DROP DATABASE IF EXISTS {}'.format(dbname))
23 with conn.cursor() as cur:
24 cur.execute('DROP DATABASE IF EXISTS {}'.format(dbname))
27 def test_create_db_success(nonexistant_db):
28 database_import.create_db('dbname=' + nonexistant_db, rouser='www-data')
30 conn = psycopg2.connect(database=nonexistant_db)
34 def test_create_db_already_exists(temp_db):
35 with pytest.raises(UsageError):
36 database_import.create_db('dbname=' + temp_db)
39 def test_create_db_unsupported_version(nonexistant_db, monkeypatch):
40 monkeypatch.setattr(database_import, 'POSTGRESQL_REQUIRED_VERSION', (100, 4))
42 with pytest.raises(UsageError, match='PostgreSQL server is too old.'):
43 database_import.create_db('dbname=' + nonexistant_db)
46 def test_create_db_missing_ro_user(nonexistant_db):
47 with pytest.raises(UsageError, match='Missing read-only user.'):
48 database_import.create_db('dbname=' + nonexistant_db, rouser='sdfwkjkjgdugu2;jgsafkljas;')
51 def test_setup_extensions(temp_db_conn, temp_db_cursor):
52 database_import.setup_extensions(temp_db_conn)
54 temp_db_cursor.execute('CREATE TABLE t (h HSTORE, geom GEOMETRY(Geometry, 4326))')
57 def test_setup_extensions_old_postgis(temp_db_conn, monkeypatch):
58 monkeypatch.setattr(database_import, 'POSTGIS_REQUIRED_VERSION', (50, 50))
60 with pytest.raises(UsageError, match='PostGIS version is too old.'):
61 database_import.setup_extensions(temp_db_conn)
64 def test_install_module(tmp_path):
65 src_dir = tmp_path / 'source'
67 (src_dir / 'nominatim.so').write_text('TEST nomiantim.so')
69 project_dir = tmp_path / 'project'
72 database_import.install_module(src_dir, project_dir, '')
74 outfile = project_dir / 'module' / 'nominatim.so'
76 assert outfile.exists()
77 assert outfile.read_text() == 'TEST nomiantim.so'
78 assert outfile.stat().st_mode == 33261
81 def test_import_base_data(src_dir, temp_db, temp_db_cursor):
82 temp_db_cursor.execute('CREATE EXTENSION hstore')
83 temp_db_cursor.execute('CREATE EXTENSION postgis')
84 database_import.import_base_data('dbname=' + temp_db, src_dir / 'data')
86 assert temp_db_cursor.scalar('SELECT count(*) FROM country_name') > 0
89 def test_import_base_data_ignore_partitions(src_dir, temp_db, temp_db_cursor):
90 temp_db_cursor.execute('CREATE EXTENSION hstore')
91 temp_db_cursor.execute('CREATE EXTENSION postgis')
92 database_import.import_base_data('dbname=' + temp_db, src_dir / 'data',
93 ignore_partitions=True)
95 assert temp_db_cursor.scalar('SELECT count(*) FROM country_name') > 0
96 assert temp_db_cursor.scalar('SELECT count(*) FROM country_name WHERE partition != 0') == 0