2 Tests for SQL preprocessing.
 
   6 from nominatim.db.sql_preprocessor import SQLPreprocessor
 
   9 def sql_factory(tmp_path):
 
  10     def _mk_sql(sql_body):
 
  11         (tmp_path / 'test.sql').write_text("""
 
  12           CREATE OR REPLACE FUNCTION test() RETURNS TEXT
 
  17           $$ LANGUAGE plpgsql IMMUTABLE;""".format(sql_body))
 
  22 @pytest.mark.parametrize("expr,ret", [
 
  24     ("'{{db.partitions|join}}'", '012'),
 
  25     ("{% if 'country_name' in db.tables %}'yes'{% else %}'no'{% endif %}", "yes"),
 
  26     ("{% if 'xxx' in db.tables %}'yes'{% else %}'no'{% endif %}", "no"),
 
  27     ("'{{db.tablespace.address_data}}'", ""),
 
  28     ("'{{db.tablespace.search_data}}'", 'TABLESPACE "dsearch"'),
 
  29     ("'{{db.tablespace.address_index}}'", 'TABLESPACE "iaddress"'),
 
  30     ("'{{db.tablespace.aux_data}}'", 'TABLESPACE "daux"')
 
  32 def test_load_file_simple(sql_preprocessor_cfg, sql_factory,
 
  33                           temp_db_conn, temp_db_cursor, monkeypatch,
 
  35     monkeypatch.setenv('NOMINATIM_TABLESPACE_SEARCH_DATA', 'dsearch')
 
  36     monkeypatch.setenv('NOMINATIM_TABLESPACE_ADDRESS_INDEX', 'iaddress')
 
  37     monkeypatch.setenv('NOMINATIM_TABLESPACE_AUX_DATA', 'daux')
 
  38     sqlfile = sql_factory("RETURN {};".format(expr))
 
  40     SQLPreprocessor(temp_db_conn, sql_preprocessor_cfg).run_sql_file(temp_db_conn, sqlfile)
 
  42     assert temp_db_cursor.scalar('SELECT test()') == ret
 
  45 def test_load_file_with_params(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor):
 
  46     sqlfile = sql_factory("RETURN '{{ foo }} {{ bar }}';")
 
  48     sql_preprocessor.run_sql_file(temp_db_conn, sqlfile, bar='XX', foo='ZZ')
 
  50     assert temp_db_cursor.scalar('SELECT test()') == 'ZZ XX'