]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_create_functions.py
enable flake for Python tests
[nominatim.git] / test / python / tools / test_refresh_create_functions.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 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_db.tools.refresh import create_functions
13
14
15 class TestCreateFunctions:
16     @pytest.fixture(autouse=True)
17     def init_env(self, sql_preprocessor, temp_db_conn, def_config, tmp_path):
18         self.conn = temp_db_conn
19         self.config = def_config
20         def_config.lib_dir.sql = tmp_path
21
22     def write_functions(self, content):
23         sqlfile = self.config.lib_dir.sql / 'functions.sql'
24         sqlfile.write_text(content)
25
26     def test_create_functions(self, temp_db_cursor):
27         self.write_functions("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
28                               AS $$
29                               BEGIN
30                                 RETURN 43;
31                               END;
32                               $$ LANGUAGE plpgsql IMMUTABLE;
33                            """)
34
35         create_functions(self.conn, self.config)
36
37         assert temp_db_cursor.scalar('SELECT test()') == 43
38
39     @pytest.mark.parametrize("dbg,ret", ((True, 43), (False, 22)))
40     def test_create_functions_with_template(self, temp_db_cursor, dbg, ret):
41         self.write_functions("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
42                               AS $$
43                               BEGIN
44                                 {% if debug %}
45                                 RETURN 43;
46                                 {% else %}
47                                 RETURN 22;
48                                 {% endif %}
49                               END;
50                               $$ LANGUAGE plpgsql IMMUTABLE;
51                            """)
52
53         create_functions(self.conn, self.config, enable_debug=dbg)
54
55         assert temp_db_cursor.scalar('SELECT test()') == ret