mirror of
https://github.com/osm-search/Nominatim.git
synced 2024-11-24 06:22:08 +03:00
fbbdd31399
Creating and populating the word table is now the responsibility of the tokenizer. The get_maxwordfreq() function has been replaced with a simple template parameter to the SQL during function installation. The number is taken from the parameter list in the database to ensure that it is not changed after installation.
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
"""
|
|
Tests for SQL preprocessing.
|
|
"""
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
@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"),
|
|
])
|
|
def test_load_file_simple(sql_preprocessor, sql_factory, temp_db_conn, temp_db_cursor, expr, ret):
|
|
sqlfile = sql_factory("RETURN {};".format(expr))
|
|
|
|
sql_preprocessor.run_sql_file(temp_db_conn, sqlfile)
|
|
|
|
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'
|