]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_refresh_create_functions.py
move index creation for word table to tokenizer
[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 sql_tmp_path(tmp_path, def_config):
10     def_config.lib_dir.sql = tmp_path
11     return tmp_path
12
13 @pytest.fixture
14 def conn(sql_preprocessor, temp_db_conn):
15     return temp_db_conn
16
17
18 def test_create_functions(temp_db_cursor, conn, def_config, sql_tmp_path):
19     sqlfile = sql_tmp_path / 'functions.sql'
20     sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
21                           AS $$
22                           BEGIN
23                             RETURN 43;
24                           END;
25                           $$ LANGUAGE plpgsql IMMUTABLE;
26                        """)
27
28     create_functions(conn, def_config)
29
30     assert temp_db_cursor.scalar('SELECT test()') == 43
31
32
33 @pytest.mark.parametrize("dbg,ret", ((True, 43), (False, 22)))
34 def test_create_functions_with_template(temp_db_cursor, conn, def_config, sql_tmp_path, dbg, ret):
35     sqlfile = sql_tmp_path / 'functions.sql'
36     sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
37                           AS $$
38                           BEGIN
39                             {% if debug %}
40                             RETURN 43;
41                             {% else %}
42                             RETURN 22;
43                             {% endif %}
44                           END;
45                           $$ LANGUAGE plpgsql IMMUTABLE;
46                        """)
47
48     create_functions(conn, def_config, enable_debug=dbg)
49
50     assert temp_db_cursor.scalar('SELECT test()') == ret