2 Tests for creating PL/pgSQL functions for Nominatim.
4 from pathlib import Path
7 from nominatim.db.connection import connect
8 from nominatim.tools.refresh import _get_standard_function_sql, _get_partition_function_sql
10 SQL_DIR = (Path(__file__) / '..' / '..' / '..' / 'lib-sql').resolve()
14 with connect('dbname=' + temp_db) as conn:
18 def db_with_tables(db):
19 with db.cursor() as cur:
20 for table in ('place', 'placex', 'location_postcode'):
21 cur.execute('CREATE TABLE {} (place_id BIGINT)'.format(table))
26 def test_standard_functions_replace_module_default(db, def_config):
27 def_config.project_dir = Path('.')
28 sql = _get_standard_function_sql(db, def_config, SQL_DIR, False, False)
31 assert sql.find('{modulepath}') < 0
32 assert sql.find("'{}'".format(Path('module/nominatim.so').resolve())) >= 0
35 def test_standard_functions_replace_module_custom(monkeypatch, db, def_config):
36 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'custom')
37 sql = _get_standard_function_sql(db, def_config, SQL_DIR, False, False)
40 assert sql.find('{modulepath}') < 0
41 assert sql.find("'custom/nominatim.so'") >= 0
44 @pytest.mark.parametrize("enabled", (True, False))
45 def test_standard_functions_enable_diff(db_with_tables, def_config, enabled):
46 def_config.project_dir = Path('.')
47 sql = _get_standard_function_sql(db_with_tables, def_config, SQL_DIR, enabled, False)
50 assert (sql.find('%DIFFUPDATES%') < 0) == enabled
53 @pytest.mark.parametrize("enabled", (True, False))
54 def test_standard_functions_enable_debug(db_with_tables, def_config, enabled):
55 def_config.project_dir = Path('.')
56 sql = _get_standard_function_sql(db_with_tables, def_config, SQL_DIR, False, enabled)
59 assert (sql.find('--DEBUG') < 0) == enabled
62 @pytest.mark.parametrize("enabled", (True, False))
63 def test_standard_functions_enable_limit_reindexing(monkeypatch, db_with_tables, def_config, enabled):
64 def_config.project_dir = Path('.')
65 monkeypatch.setenv('NOMINATIM_LIMIT_REINDEXING', 'yes' if enabled else 'no')
66 sql = _get_standard_function_sql(db_with_tables, def_config, SQL_DIR, False, False)
69 assert (sql.find('--LIMIT INDEXING') < 0) == enabled
72 @pytest.mark.parametrize("enabled", (True, False))
73 def test_standard_functions_enable_tiger(monkeypatch, db_with_tables, def_config, enabled):
74 def_config.project_dir = Path('.')
75 monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes' if enabled else 'no')
76 sql = _get_standard_function_sql(db_with_tables, def_config, SQL_DIR, False, False)
79 assert (sql.find('%NOTIGERDATA%') >= 0) == enabled
82 @pytest.mark.parametrize("enabled", (True, False))
83 def test_standard_functions_enable_aux(monkeypatch, db_with_tables, def_config, enabled):
84 def_config.project_dir = Path('.')
85 monkeypatch.setenv('NOMINATIM_USE_AUX_LOCATION_DATA', 'yes' if enabled else 'no')
86 sql = _get_standard_function_sql(db_with_tables, def_config, SQL_DIR, False, False)
89 assert (sql.find('%NOAUXDATA%') >= 0) == enabled
92 def test_partition_function(temp_db_cursor, db, def_config):
93 temp_db_cursor.execute("CREATE TABLE country_name (partition SMALLINT)")
95 sql = _get_partition_function_sql(db, SQL_DIR)
98 assert sql.find('-partition-') < 0