]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_tiger_data.py
1366fe3ec06327997ef90598c86ef5ecda051450
[nominatim.git] / test / python / test_tools_tiger_data.py
1 """
2 Test for tiger data function
3 """
4 from pathlib import Path
5
6 import pytest
7 import tarfile
8
9 from nominatim.tools import tiger_data, database_import
10
11
12 @pytest.mark.parametrize("threads", (1, 5))
13 def test_add_tiger_data(dsn, src_dir, def_config, tmp_path, sql_preprocessor,
14                         temp_db_cursor, threads, temp_db):
15     temp_db_cursor.execute('CREATE EXTENSION hstore')
16     temp_db_cursor.execute('CREATE EXTENSION postgis')
17     temp_db_cursor.execute('CREATE TABLE place (id INT)')
18     sqlfile = tmp_path / '1010.sql'
19     sqlfile.write_text("""INSERT INTO place values (1);
20                           INSERT INTO non_existant_table values (1);""")
21     tiger_data.add_tiger_data(dsn, str(tmp_path), threads, def_config, src_dir / 'lib-sql')
22
23     assert temp_db_cursor.table_rows('place') == 1
24
25 @pytest.mark.parametrize("threads", (1, 5))
26 def test_add_tiger_data_bad_file(dsn, src_dir, def_config, tmp_path, sql_preprocessor,
27                         temp_db_cursor, threads, temp_db):
28     temp_db_cursor.execute('CREATE EXTENSION hstore')
29     temp_db_cursor.execute('CREATE EXTENSION postgis')
30     temp_db_cursor.execute('CREATE TABLE place (id INT)')
31     sqlfile = tmp_path / '1010.txt'
32     sqlfile.write_text("""Random text""")
33     tiger_data.add_tiger_data(dsn, str(tmp_path), threads, def_config, src_dir / 'lib-sql')
34
35     assert temp_db_cursor.table_rows('place') == 0
36
37 @pytest.mark.parametrize("threads", (1, 5))
38 def test_add_tiger_data_tarfile(dsn, src_dir, def_config, tmp_path,
39                         temp_db_cursor, threads, temp_db, sql_preprocessor):
40     temp_db_cursor.execute('CREATE EXTENSION hstore')
41     temp_db_cursor.execute('CREATE EXTENSION postgis')
42     temp_db_cursor.execute('CREATE TABLE place (id INT)')
43     sqlfile = tmp_path / '1010.sql'
44     sqlfile.write_text("""INSERT INTO place values (1);
45                           INSERT INTO non_existant_table values (1);""")
46     tar = tarfile.open("sample.tar.gz", "w:gz")
47     tar.add(sqlfile)
48     tar.close()
49     tiger_data.add_tiger_data(dsn, str(src_dir / 'sample.tar.gz'), threads, def_config, src_dir / 'lib-sql')
50     
51     assert temp_db_cursor.table_rows('place') == 1
52
53 @pytest.mark.parametrize("threads", (1, 5))
54 def test_add_tiger_data_bad_tarfile(dsn, src_dir, def_config, tmp_path,
55                         temp_db_cursor, threads, temp_db, sql_preprocessor):
56     temp_db_cursor.execute('CREATE EXTENSION hstore')
57     temp_db_cursor.execute('CREATE EXTENSION postgis')
58     temp_db_cursor.execute('CREATE TABLE place (id INT)')
59     sqlfile = tmp_path / '1010.txt'
60     sqlfile.write_text("""Random text""")
61     tar = tarfile.open("sample.tar.gz", "w:gz")
62     tar.add(sqlfile)
63     tar.close()
64     tiger_data.add_tiger_data(dsn, str(src_dir / 'sample.tar.gz'), threads, def_config, src_dir / 'lib-sql')
65     
66     assert temp_db_cursor.table_rows('place') == 0