]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_db_utils.py
Merge pull request #2341 from lonvia/cleanup-python-tests
[nominatim.git] / test / python / test_db_utils.py
1 """
2 Tests for DB utility functions in db.utils
3 """
4 import pytest
5
6 import nominatim.db.utils as db_utils
7 from nominatim.errors import UsageError
8
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);')
12
13     db_utils.execute_file(dsn, tmpfile)
14
15     assert temp_db_cursor.row_set('SELECT * FROM test') == {(56, )}
16
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')
20
21
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)')
25
26     with pytest.raises(UsageError):
27         db_utils.execute_file(dsn, tmpfile)
28
29
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)')
33
34     db_utils.execute_file(dsn, tmpfile, ignore_errors=True)
35
36
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)')
40
41     db_utils.execute_file(dsn, tmpfile, pre_code='CREATE TABLE test (id INT)')
42
43     assert temp_db_cursor.row_set('SELECT * FROM test') == {(4, )}
44
45
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)')
49
50     db_utils.execute_file(dsn, tmpfile, post_code='INSERT INTO test VALUES(23)')
51
52     assert temp_db_cursor.row_set('SELECT * FROM test') == {(23, )}