2 Tests for DB utility functions in db.utils
6 import nominatim.db.utils as db_utils
7 from nominatim.errors import UsageError
9 def test_execute_file_success(dsn, temp_db_cursor, tmp_path):
10 tmpfile = tmp_path / 'test.sql'
11 tmpfile.write_text('CREATE TABLE test (id INT);\nINSERT INTO test VALUES(56);')
13 db_utils.execute_file(dsn, tmpfile)
15 assert temp_db_cursor.row_set('SELECT * FROM test') == {(56, )}
17 def test_execute_file_bad_file(dsn, tmp_path):
18 with pytest.raises(FileNotFoundError):
19 db_utils.execute_file(dsn, tmp_path / 'test2.sql')
22 def test_execute_file_bad_sql(dsn, tmp_path):
23 tmpfile = tmp_path / 'test.sql'
24 tmpfile.write_text('CREATE STABLE test (id INT)')
26 with pytest.raises(UsageError):
27 db_utils.execute_file(dsn, tmpfile)
30 def test_execute_file_bad_sql_ignore_errors(dsn, tmp_path):
31 tmpfile = tmp_path / 'test.sql'
32 tmpfile.write_text('CREATE STABLE test (id INT)')
34 db_utils.execute_file(dsn, tmpfile, ignore_errors=True)
37 def test_execute_file_with_pre_code(dsn, tmp_path, temp_db_cursor):
38 tmpfile = tmp_path / 'test.sql'
39 tmpfile.write_text('INSERT INTO test VALUES(4)')
41 db_utils.execute_file(dsn, tmpfile, pre_code='CREATE TABLE test (id INT)')
43 assert temp_db_cursor.row_set('SELECT * FROM test') == {(4, )}
46 def test_execute_file_with_post_code(dsn, tmp_path, temp_db_cursor):
47 tmpfile = tmp_path / 'test.sql'
48 tmpfile.write_text('CREATE TABLE test (id INT)')
50 db_utils.execute_file(dsn, tmpfile, post_code='INSERT INTO test VALUES(23)')
52 assert temp_db_cursor.row_set('SELECT * FROM test') == {(23, )}