2022-01-03 18:23:58 +03:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#
|
|
|
|
# This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2022 by the Nominatim developer community.
|
|
|
|
# For a full list of authors see the git log.
|
2021-03-03 19:37:22 +03:00
|
|
|
"""
|
|
|
|
Tests for SQL preprocessing.
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
2021-10-15 16:07:43 +03:00
|
|
|
from nominatim.db.sql_preprocessor import SQLPreprocessor
|
|
|
|
|
2021-03-03 19:37:22 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def sql_factory(tmp_path):
|
|
|
|
def _mk_sql(sql_body):
|
|
|
|
(tmp_path / 'test.sql').write_text("""
|
|
|
|
CREATE OR REPLACE FUNCTION test() RETURNS TEXT
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
{}
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql IMMUTABLE;""".format(sql_body))
|
|
|
|
return 'test.sql'
|
|
|
|
|
|
|
|
return _mk_sql
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("expr,ret", [
|
|
|
|
("'a'", 'a'),
|
|
|
|
("'{{db.partitions|join}}'", '012'),
|
|
|
|
("{% if 'country_name' in db.tables %}'yes'{% else %}'no'{% endif %}", "yes"),
|
|
|
|
("{% if 'xxx' in db.tables %}'yes'{% else %}'no'{% endif %}", "no"),
|
2021-10-15 16:07:43 +03:00
|
|
|
("'{{db.tablespace.address_data}}'", ""),
|
|
|
|
("'{{db.tablespace.search_data}}'", 'TABLESPACE "dsearch"'),
|
|
|
|
("'{{db.tablespace.address_index}}'", 'TABLESPACE "iaddress"'),
|
|
|
|
("'{{db.tablespace.aux_data}}'", 'TABLESPACE "daux"')
|
2021-03-03 19:37:22 +03:00
|
|
|
])
|
2021-10-15 16:07:43 +03:00
|
|
|
def test_load_file_simple(sql_preprocessor_cfg, sql_factory,
|
|
|
|
temp_db_conn, temp_db_cursor, monkeypatch,
|
|
|
|
expr, ret):
|
|
|
|
monkeypatch.setenv('NOMINATIM_TABLESPACE_SEARCH_DATA', 'dsearch')
|
|
|
|
monkeypatch.setenv('NOMINATIM_TABLESPACE_ADDRESS_INDEX', 'iaddress')
|
|
|
|
monkeypatch.setenv('NOMINATIM_TABLESPACE_AUX_DATA', 'daux')
|
2021-03-03 19:37:22 +03:00
|
|
|
sqlfile = sql_factory("RETURN {};".format(expr))
|
|
|
|
|
2021-10-15 16:07:43 +03:00
|
|
|
SQLPreprocessor(temp_db_conn, sql_preprocessor_cfg).run_sql_file(temp_db_conn, sqlfile)
|
2021-03-03 19:37:22 +03:00
|
|
|
|
|
|
|
assert temp_db_cursor.scalar('SELECT test()') == ret
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_file_with_params(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor):
|
|
|
|
sqlfile = sql_factory("RETURN '{{ foo }} {{ bar }}';")
|
|
|
|
|
|
|
|
sql_preprocessor.run_sql_file(temp_db_conn, sqlfile, bar='XX', foo='ZZ')
|
|
|
|
|
|
|
|
assert temp_db_cursor.scalar('SELECT test()') == 'ZZ XX'
|