2 Tests for SQL preprocessing.
4 from pathlib import Path
8 from nominatim.db.sql_preprocessor import SQLPreprocessor
11 def sql_factory(tmp_path):
12 def _mk_sql(sql_body):
13 (tmp_path / 'test.sql').write_text("""
14 CREATE OR REPLACE FUNCTION test() RETURNS TEXT
19 $$ LANGUAGE plpgsql IMMUTABLE;""".format(sql_body))
26 def sql_preprocessor(temp_db_conn, tmp_path, def_config, monkeypatch, table_factory):
27 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', '.')
28 table_factory('country_name', 'partition INT', (0, 1, 2))
29 return SQLPreprocessor(temp_db_conn, def_config, tmp_path)
31 @pytest.mark.parametrize("expr,ret", [
33 ("'{{db.partitions|join}}'", '012'),
34 ("{% if 'country_name' in db.tables %}'yes'{% else %}'no'{% endif %}", "yes"),
35 ("{% if 'xxx' in db.tables %}'yes'{% else %}'no'{% endif %}", "no"),
36 ("'{{config.DATABASE_MODULE_PATH}}'", '.')
38 def test_load_file_simple(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor, expr, ret):
39 sqlfile = sql_factory("RETURN {};".format(expr))
41 sql_preprocessor.run_sql_file(temp_db_conn, sqlfile)
43 assert temp_db_cursor.scalar('SELECT test()') == ret
46 def test_load_file_with_params(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor):
47 sqlfile = sql_factory("RETURN '{{ foo }} {{ bar }}';")
49 sql_preprocessor.run_sql_file(temp_db_conn, sqlfile, bar='XX', foo='ZZ')
51 assert temp_db_cursor.scalar('SELECT test()') == 'ZZ XX'