]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_db_utils.py
add function to set up libpq environment
[nominatim.git] / test / python / test_db_utils.py
1 """
2 Tests for DB utility functions in db.utils
3 """
4 import psycopg2
5 import pytest
6
7 import nominatim.db.utils as db_utils
8
9 def test_execute_file_success(temp_db_conn, 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(temp_db_conn, tmpfile)
14
15     with temp_db_conn.cursor() as cur:
16         cur.execute('SELECT * FROM test')
17
18         assert cur.rowcount == 1
19         assert cur.fetchone()[0] == 56
20
21 def test_execute_file_bad_file(temp_db_conn, tmp_path):
22     with pytest.raises(FileNotFoundError):
23         db_utils.execute_file(temp_db_conn, tmp_path / 'test2.sql')
24
25 def test_execute_file_bad_sql(temp_db_conn, tmp_path):
26     tmpfile = tmp_path / 'test.sql'
27     tmpfile.write_text('CREATE STABLE test (id INT)')
28
29     with pytest.raises(psycopg2.ProgrammingError):
30         db_utils.execute_file(temp_db_conn, tmpfile)