]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_create_functions.py
python test: move single-use fixtures to subdirectories
[nominatim.git] / test / python / tools / test_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,
35                                         dbg, ret):
36     sqlfile = sql_tmp_path / 'functions.sql'
37     sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
38                           AS $$
39                           BEGIN
40                             {% if debug %}
41                             RETURN 43;
42                             {% else %}
43                             RETURN 22;
44                             {% endif %}
45                           END;
46                           $$ LANGUAGE plpgsql IMMUTABLE;
47                        """)
48
49     create_functions(conn, def_config, enable_debug=dbg)
50
51     assert temp_db_cursor.scalar('SELECT test()') == ret