]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_refresh_create_functions.py
Merge pull request #2197 from lonvia/use-jinja-for-sql-preprocessing
[nominatim.git] / test / python / test_tools_refresh_create_functions.py
1 """
2 Tests for creating PL/pgSQL functions for Nominatim.
3 """
4 import pytest
5
6 from nominatim.tools.refresh import create_functions
7
8 @pytest.fixture
9 def conn(temp_db_conn, table_factory, monkeypatch):
10     monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', '.')
11     table_factory('country_name', 'partition INT', (0, 1, 2))
12     return temp_db_conn
13
14
15 def test_create_functions(temp_db_cursor, conn, def_config, tmp_path):
16     sqlfile = tmp_path / 'functions.sql'
17     sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
18                           AS $$
19                           BEGIN
20                             RETURN 43;
21                           END;
22                           $$ LANGUAGE plpgsql IMMUTABLE;
23                        """)
24
25     create_functions(conn, def_config, tmp_path)
26
27     assert temp_db_cursor.scalar('SELECT test()') == 43
28
29
30 @pytest.mark.parametrize("dbg,ret", ((True, 43), (False, 22)))
31 def test_create_functions_with_template(temp_db_cursor, conn, def_config, tmp_path, dbg, ret):
32     sqlfile = tmp_path / 'functions.sql'
33     sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
34                           AS $$
35                           BEGIN
36                             {% if debug %}
37                             RETURN 43;
38                             {% else %}
39                             RETURN 22;
40                             {% endif %}
41                           END;
42                           $$ LANGUAGE plpgsql IMMUTABLE;
43                        """)
44
45     create_functions(conn, def_config, tmp_path, enable_debug=dbg)
46
47     assert temp_db_cursor.scalar('SELECT test()') == ret