]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_db_sql_preprocessor.py
move SearchDescription building into tokens
[nominatim.git] / test / python / test_db_sql_preprocessor.py
1 """
2 Tests for SQL preprocessing.
3 """
4 import pytest
5
6 @pytest.fixture
7 def sql_factory(tmp_path):
8     def _mk_sql(sql_body):
9         (tmp_path / 'test.sql').write_text("""
10           CREATE OR REPLACE FUNCTION test() RETURNS TEXT
11           AS $$
12           BEGIN
13             {}
14           END;
15           $$ LANGUAGE plpgsql IMMUTABLE;""".format(sql_body))
16         return 'test.sql'
17
18     return _mk_sql
19
20 @pytest.mark.parametrize("expr,ret", [
21     ("'a'", 'a'),
22     ("'{{db.partitions|join}}'", '012'),
23     ("{% if 'country_name' in db.tables %}'yes'{% else %}'no'{% endif %}", "yes"),
24     ("{% if 'xxx' in db.tables %}'yes'{% else %}'no'{% endif %}", "no"),
25     ])
26 def test_load_file_simple(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor, expr, ret):
27     sqlfile = sql_factory("RETURN {};".format(expr))
28
29     sql_preprocessor.run_sql_file(temp_db_conn, sqlfile)
30
31     assert temp_db_cursor.scalar('SELECT test()') == ret
32
33
34 def test_load_file_with_params(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor):
35     sqlfile = sql_factory("RETURN '{{ foo }} {{ bar }}';")
36
37     sql_preprocessor.run_sql_file(temp_db_conn, sqlfile, bar='XX', foo='ZZ')
38
39     assert temp_db_cursor.scalar('SELECT test()') == 'ZZ XX'