2 Tests for DB utility functions in db.utils
 
   8 import nominatim.db.utils as db_utils
 
   9 from nominatim.errors import UsageError
 
  11 def test_execute_file_success(dsn, temp_db_cursor, tmp_path):
 
  12     tmpfile = tmp_path / 'test.sql'
 
  13     tmpfile.write_text('CREATE TABLE test (id INT);\nINSERT INTO test VALUES(56);')
 
  15     db_utils.execute_file(dsn, tmpfile)
 
  17     assert temp_db_cursor.row_set('SELECT * FROM test') == {(56, )}
 
  19 def test_execute_file_bad_file(dsn, tmp_path):
 
  20     with pytest.raises(FileNotFoundError):
 
  21         db_utils.execute_file(dsn, tmp_path / 'test2.sql')
 
  24 def test_execute_file_bad_sql(dsn, tmp_path):
 
  25     tmpfile = tmp_path / 'test.sql'
 
  26     tmpfile.write_text('CREATE STABLE test (id INT)')
 
  28     with pytest.raises(UsageError):
 
  29         db_utils.execute_file(dsn, tmpfile)
 
  32 def test_execute_file_bad_sql_ignore_errors(dsn, tmp_path):
 
  33     tmpfile = tmp_path / 'test.sql'
 
  34     tmpfile.write_text('CREATE STABLE test (id INT)')
 
  36     db_utils.execute_file(dsn, tmpfile, ignore_errors=True)
 
  39 def test_execute_file_with_pre_code(dsn, tmp_path, temp_db_cursor):
 
  40     tmpfile = tmp_path / 'test.sql'
 
  41     tmpfile.write_text('INSERT INTO test VALUES(4)')
 
  43     db_utils.execute_file(dsn, tmpfile, pre_code='CREATE TABLE test (id INT)')
 
  45     assert temp_db_cursor.row_set('SELECT * FROM test') == {(4, )}
 
  48 def test_execute_file_with_post_code(dsn, tmp_path, temp_db_cursor):
 
  49     tmpfile = tmp_path / 'test.sql'
 
  50     tmpfile.write_text('CREATE TABLE test (id INT)')
 
  52     db_utils.execute_file(dsn, tmpfile, post_code='INSERT INTO test VALUES(23)')
 
  54     assert temp_db_cursor.row_set('SELECT * FROM test') == {(23, )}
 
  58     TABLE_NAME = 'copytable'
 
  60     @pytest.fixture(autouse=True)
 
  61     def setup_test_table(self, table_factory):
 
  62         table_factory(self.TABLE_NAME, 'colA INT, colB TEXT')
 
  65     def table_rows(self, cursor):
 
  66         return cursor.row_set('SELECT * FROM ' + self.TABLE_NAME)
 
  69     def test_copybuffer_empty(self):
 
  70         with db_utils.CopyBuffer() as buf:
 
  71             buf.copy_out(None, "dummy")
 
  74     def test_all_columns(self, temp_db_cursor):
 
  75         with db_utils.CopyBuffer() as buf:
 
  79             buf.copy_out(temp_db_cursor, self.TABLE_NAME)
 
  81         assert self.table_rows(temp_db_cursor) == {(3, 'hum'), (None, 'f\\t')}
 
  84     def test_selected_columns(self, temp_db_cursor):
 
  85         with db_utils.CopyBuffer() as buf:
 
  88             buf.copy_out(temp_db_cursor, self.TABLE_NAME,
 
  91         assert self.table_rows(temp_db_cursor) == {(None, 'foo')}
 
  94     def test_reordered_columns(self, temp_db_cursor):
 
  95         with db_utils.CopyBuffer() as buf:
 
  99             buf.copy_out(temp_db_cursor, self.TABLE_NAME,
 
 100                          columns=['colB', 'colA'])
 
 102         assert self.table_rows(temp_db_cursor) == {(1, 'one'), (2, ' two ')}
 
 105     def test_special_characters(self, temp_db_cursor):
 
 106         with db_utils.CopyBuffer() as buf:
 
 111             buf.copy_out(temp_db_cursor, self.TABLE_NAME,
 
 114         assert self.table_rows(temp_db_cursor) == {(None, 'foo\tbar'),
 
 120 class TestCopyBufferJson:
 
 121     TABLE_NAME = 'copytable'
 
 123     @pytest.fixture(autouse=True)
 
 124     def setup_test_table(self, table_factory):
 
 125         table_factory(self.TABLE_NAME, 'colA INT, colB JSONB')
 
 128     def table_rows(self, cursor):
 
 129         cursor.execute('SELECT * FROM ' + self.TABLE_NAME)
 
 130         results = {k: v for k,v in cursor}
 
 132         assert len(results) == cursor.rowcount
 
 137     def test_json_object(self, temp_db_cursor):
 
 138         with db_utils.CopyBuffer() as buf:
 
 139             buf.add(1, json.dumps({'test': 'value', 'number': 1}))
 
 141             buf.copy_out(temp_db_cursor, self.TABLE_NAME)
 
 143         assert self.table_rows(temp_db_cursor) == \
 
 144                    {1: {'test': 'value', 'number': 1}}
 
 147     def test_json_object_special_chras(self, temp_db_cursor):
 
 148         with db_utils.CopyBuffer() as buf:
 
 149             buf.add(1, json.dumps({'te\tst': 'va\nlue', 'nu"mber': None}))
 
 151             buf.copy_out(temp_db_cursor, self.TABLE_NAME)
 
 153         assert self.table_rows(temp_db_cursor) == \
 
 154                    {1: {'te\tst': 'va\nlue', 'nu"mber': None}}