]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_create_functions.py
Merge pull request #2539 from lonvia/clean-up-python-tests
[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 class TestCreateFunctions:
9     @pytest.fixture(autouse=True)
10     def init_env(self, sql_preprocessor, temp_db_conn, def_config, tmp_path):
11         self.conn = temp_db_conn
12         self.config = def_config
13         def_config.lib_dir.sql = tmp_path
14
15
16     def write_functions(self, content):
17         sqlfile = self.config.lib_dir.sql / 'functions.sql'
18         sqlfile.write_text(content)
19
20
21     def test_create_functions(self, temp_db_cursor):
22         self.write_functions("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
23                               AS $$
24                               BEGIN
25                                 RETURN 43;
26                               END;
27                               $$ LANGUAGE plpgsql IMMUTABLE;
28                            """)
29
30         create_functions(self.conn, self.config)
31
32         assert temp_db_cursor.scalar('SELECT test()') == 43
33
34
35     @pytest.mark.parametrize("dbg,ret", ((True, 43), (False, 22)))
36     def test_create_functions_with_template(self, temp_db_cursor, dbg, ret):
37         self.write_functions("""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(self.conn, self.config, enable_debug=dbg)
50
51         assert temp_db_cursor.scalar('SELECT test()') == ret