]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_create_functions.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / tools / test_refresh_create_functions.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for creating PL/pgSQL functions for Nominatim.
9 """
10 import pytest
11
12 from nominatim.tools.refresh import create_functions
13
14 class TestCreateFunctions:
15     @pytest.fixture(autouse=True)
16     def init_env(self, sql_preprocessor, temp_db_conn, def_config, tmp_path):
17         self.conn = temp_db_conn
18         self.config = def_config
19         def_config.lib_dir.sql = tmp_path
20
21
22     def write_functions(self, content):
23         sqlfile = self.config.lib_dir.sql / 'functions.sql'
24         sqlfile.write_text(content)
25
26
27     def test_create_functions(self, temp_db_cursor):
28         self.write_functions("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
29                               AS $$
30                               BEGIN
31                                 RETURN 43;
32                               END;
33                               $$ LANGUAGE plpgsql IMMUTABLE;
34                            """)
35
36         create_functions(self.conn, self.config)
37
38         assert temp_db_cursor.scalar('SELECT test()') == 43
39
40
41     @pytest.mark.parametrize("dbg,ret", ((True, 43), (False, 22)))
42     def test_create_functions_with_template(self, temp_db_cursor, dbg, ret):
43         self.write_functions("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
44                               AS $$
45                               BEGIN
46                                 {% if debug %}
47                                 RETURN 43;
48                                 {% else %}
49                                 RETURN 22;
50                                 {% endif %}
51                               END;
52                               $$ LANGUAGE plpgsql IMMUTABLE;
53                            """)
54
55         create_functions(self.conn, self.config, enable_debug=dbg)
56
57         assert temp_db_cursor.scalar('SELECT test()') == ret