1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for creating PL/pgSQL functions for Nominatim.
12 from nominatim_db.tools.refresh import create_functions
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
22 def write_functions(self, content):
23 sqlfile = self.config.lib_dir.sql / 'functions.sql'
24 sqlfile.write_text(content)
26 def test_create_functions(self, temp_db_cursor):
27 self.write_functions("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
32 $$ LANGUAGE plpgsql IMMUTABLE;
35 create_functions(self.conn, self.config)
37 assert temp_db_cursor.scalar('SELECT test()') == 43
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
50 $$ LANGUAGE plpgsql IMMUTABLE;
53 create_functions(self.conn, self.config, enable_debug=dbg)
55 assert temp_db_cursor.scalar('SELECT test()') == ret