2 Tests for functions to import a new database.
4 from pathlib import Path
9 from nominatim.tools import database_import
10 from nominatim.errors import UsageError
14 dbname = 'test_nominatim_python_unittest'
16 conn = psycopg2.connect(database='postgres')
18 conn.set_isolation_level(0)
19 with conn.cursor() as cur:
20 cur.execute('DROP DATABASE IF EXISTS {}'.format(dbname))
24 with conn.cursor() as cur:
25 cur.execute('DROP DATABASE IF EXISTS {}'.format(dbname))
27 @pytest.mark.parametrize("no_partitions", (True, False))
28 def test_setup_skeleton(src_dir, nonexistant_db, no_partitions):
29 database_import.setup_database_skeleton('dbname=' + nonexistant_db,
30 src_dir / 'data', no_partitions)
32 conn = psycopg2.connect(database=nonexistant_db)
35 with conn.cursor() as cur:
36 cur.execute("SELECT distinct partition FROM country_name")
37 partitions = set((r[0] for r in list(cur)))
39 assert partitions == set((0, ))
41 assert len(partitions) > 10
46 def test_create_db_success(nonexistant_db):
47 database_import.create_db('dbname=' + nonexistant_db, rouser='www-data')
49 conn = psycopg2.connect(database=nonexistant_db)
53 def test_create_db_already_exists(temp_db):
54 with pytest.raises(UsageError):
55 database_import.create_db('dbname=' + temp_db)
58 def test_create_db_unsupported_version(nonexistant_db, monkeypatch):
59 monkeypatch.setattr(database_import, 'POSTGRESQL_REQUIRED_VERSION', (100, 4))
61 with pytest.raises(UsageError, match='PostgreSQL server is too old.'):
62 database_import.create_db('dbname=' + nonexistant_db)
65 def test_create_db_missing_ro_user(nonexistant_db):
66 with pytest.raises(UsageError, match='Missing read-only user.'):
67 database_import.create_db('dbname=' + nonexistant_db, rouser='sdfwkjkjgdugu2;jgsafkljas;')
70 def test_setup_extensions(temp_db_conn, table_factory):
71 database_import.setup_extensions(temp_db_conn)
73 # Use table creation to check that hstore and geometry types are available.
74 table_factory('t', 'h HSTORE, geom GEOMETRY(Geometry, 4326)')
77 def test_setup_extensions_old_postgis(temp_db_conn, monkeypatch):
78 monkeypatch.setattr(database_import, 'POSTGIS_REQUIRED_VERSION', (50, 50))
80 with pytest.raises(UsageError, match='PostGIS version is too old.'):
81 database_import.setup_extensions(temp_db_conn)
84 def test_import_base_data(dsn, src_dir, temp_db_with_extensions, temp_db_cursor):
85 database_import.import_base_data(dsn, src_dir / 'data')
87 assert temp_db_cursor.table_rows('country_name') > 0
90 def test_import_base_data_ignore_partitions(dsn, src_dir, temp_db_with_extensions,
92 database_import.import_base_data(dsn, src_dir / 'data', ignore_partitions=True)
94 assert temp_db_cursor.table_rows('country_name') > 0
95 assert temp_db_cursor.table_rows('country_name', where='partition != 0') == 0
98 def test_import_osm_data_simple(table_factory, osm2pgsql_options):
99 table_factory('place', content=((1, ), ))
101 database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
104 def test_import_osm_data_multifile(table_factory, tmp_path, osm2pgsql_options):
105 table_factory('place', content=((1, ), ))
106 osm2pgsql_options['osm2pgsql_cache'] = 0
108 files = [tmp_path / 'file1.osm', tmp_path / 'file2.osm']
112 database_import.import_osm_data(files, osm2pgsql_options)
115 def test_import_osm_data_simple_no_data(table_factory, osm2pgsql_options):
116 table_factory('place')
118 with pytest.raises(UsageError, match='No data.*'):
119 database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options)
122 def test_import_osm_data_drop(table_factory, temp_db_conn, tmp_path, osm2pgsql_options):
123 table_factory('place', content=((1, ), ))
124 table_factory('planet_osm_nodes')
126 flatfile = tmp_path / 'flatfile'
127 flatfile.write_text('touch')
129 osm2pgsql_options['flatnode_file'] = str(flatfile.resolve())
131 database_import.import_osm_data(Path('file.pbf'), osm2pgsql_options, drop=True)
133 assert not flatfile.exists()
134 assert not temp_db_conn.table_exists('planet_osm_nodes')
137 def test_import_osm_data_default_cache(table_factory, osm2pgsql_options):
138 table_factory('place', content=((1, ), ))
140 osm2pgsql_options['osm2pgsql_cache'] = 0
142 database_import.import_osm_data(Path(__file__), osm2pgsql_options)
145 def test_truncate_database_tables(temp_db_conn, temp_db_cursor, table_factory):
146 tables = ('placex', 'place_addressline', 'location_area',
147 'location_area_country',
148 'location_property_tiger', 'location_property_osmline',
149 'location_postcode', 'search_name', 'location_road_23')
151 table_factory(table, content=((1, ), (2, ), (3, )))
152 assert temp_db_cursor.table_rows(table) == 3
154 database_import.truncate_data_tables(temp_db_conn)
157 assert temp_db_cursor.table_rows(table) == 0
160 @pytest.mark.parametrize("threads", (1, 5))
161 def test_load_data(dsn, place_row, placex_table, osmline_table,
162 word_table, temp_db_cursor, threads):
163 for func in ('precompute_words', 'getorcreate_housenumber_id', 'make_standard_name'):
164 temp_db_cursor.execute("""CREATE FUNCTION {} (src TEXT)
165 RETURNS TEXT AS $$ SELECT 'a'::TEXT $$ LANGUAGE SQL
167 for oid in range(100, 130):
168 place_row(osm_id=oid)
169 place_row(osm_type='W', osm_id=342, cls='place', typ='houses',
170 geom='SRID=4326;LINESTRING(0 0, 10 10)')
172 database_import.load_data(dsn, threads)
174 assert temp_db_cursor.table_rows('placex') == 30
175 assert temp_db_cursor.table_rows('location_property_osmline') == 1
178 @pytest.mark.parametrize("languages", (None, ' fr,en'))
179 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
180 table_factory, tokenizer_mock, languages):
182 table_factory('country_name', 'country_code varchar(2), name hstore',
183 content=(('us', '"name"=>"us1","name:af"=>"us2"'),
184 ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
186 assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
188 tokenizer = tokenizer_mock()
190 database_import.create_country_names(temp_db_conn, tokenizer, languages)
192 assert len(tokenizer.analyser_cache['countries']) == 2
194 result_set = {k: set(v.values()) for k, v in tokenizer.analyser_cache['countries']}
197 assert result_set == {'us' : set(('us', 'us1', 'United States')),
198 'fr' : set(('fr', 'Fra', 'Fren'))}
200 assert result_set == {'us' : set(('us', 'us1', 'us2', 'United States')),
201 'fr' : set(('fr', 'Fra', 'Fren'))}