2 Tests for SQL preprocessing.
4 from pathlib import Path
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 ("'{{config.DATABASE_MODULE_PATH}}'", '.')
29 def test_load_file_simple(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor, expr, ret):
30 sqlfile = sql_factory("RETURN {};".format(expr))
32 sql_preprocessor.run_sql_file(temp_db_conn, sqlfile)
34 assert temp_db_cursor.scalar('SELECT test()') == ret
37 def test_load_file_with_params(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor):
38 sqlfile = sql_factory("RETURN '{{ foo }} {{ bar }}';")
40 sql_preprocessor.run_sql_file(temp_db_conn, sqlfile, bar='XX', foo='ZZ')
42 assert temp_db_cursor.scalar('SELECT test()') == 'ZZ XX'