]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_refresh_create_functions.py
ac2f221126f79c4da85249a8e5facb9c7bcd6f68
[nominatim.git] / test / python / test_tools_refresh_create_functions.py
1 """
2 Tests for creating PL/pgSQL functions for Nominatim.
3 """
4 from pathlib import Path
5 import pytest
6
7 from nominatim.db.connection import connect
8 from nominatim.tools.refresh import _get_standard_function_sql, _get_partition_function_sql
9
10 SQL_DIR = (Path(__file__) / '..' / '..' / '..' / 'lib-sql').resolve()
11
12 @pytest.fixture
13 def db(temp_db):
14     with connect('dbname=' + temp_db) as conn:
15         yield conn
16
17 @pytest.fixture
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))
22
23     return db
24
25
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)
29
30     assert sql
31     assert sql.find('{modulepath}') < 0
32     assert sql.find("'{}'".format(Path('module/nominatim.so').resolve())) >= 0
33
34
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)
38
39     assert sql
40     assert sql.find('{modulepath}') < 0
41     assert sql.find("'custom/nominatim.so'") >= 0
42
43
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)
48
49     assert sql
50     assert (sql.find('%DIFFUPDATES%') < 0) == enabled
51
52
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)
57
58     assert sql
59     assert (sql.find('--DEBUG') < 0) == enabled
60
61
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)
67
68     assert sql
69     assert (sql.find('--LIMIT INDEXING') < 0) == enabled
70
71
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)
77
78     assert sql
79     assert (sql.find('%NOTIGERDATA%') >= 0) == enabled
80
81
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)
87
88     assert sql
89     assert (sql.find('%NOAUXDATA%') >= 0) == enabled
90
91
92 def test_partition_function(temp_db_cursor, db, def_config):
93     temp_db_cursor.execute("CREATE TABLE country_name (partition SMALLINT)")
94
95     sql = _get_partition_function_sql(db, SQL_DIR)
96
97     assert sql
98     assert sql.find('-partition-') < 0