1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for maintenance and analysis functions.
13 from nominatim_db.errors import UsageError
14 from nominatim_db.tools import admin
15 from nominatim_db.tokenizer import factory
16 from nominatim_db.db.sql_preprocessor import SQLPreprocessor
19 @pytest.fixture(autouse=True)
20 def create_placex_table(project_env, tokenizer_mock, temp_db_cursor, placex_table):
21 """ All tests in this module require the placex table to be set up.
23 temp_db_cursor.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
24 temp_db_cursor.execute("""CREATE TYPE prepare_update_info AS (
27 rank_address SMALLINT,
31 linked_place_id BIGINT
33 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
34 OUT result prepare_update_info)
37 result.address := p.address;
38 result.name := p.name;
39 result.class := p.class;
40 result.type := p.type;
41 result.country_code := p.country_code;
42 result.rank_address := p.rank_address;
44 $$ LANGUAGE plpgsql STABLE;
46 factory.create_tokenizer(project_env)
49 def test_analyse_indexing_no_objects(project_env):
50 with pytest.raises(UsageError):
51 admin.analyse_indexing(project_env)
54 @pytest.mark.parametrize("oid", ['1234', 'N123a', 'X123'])
55 def test_analyse_indexing_bad_osmid(project_env, oid):
56 with pytest.raises(UsageError):
57 admin.analyse_indexing(project_env, osm_id=oid)
60 def test_analyse_indexing_unknown_osmid(project_env):
61 with pytest.raises(UsageError):
62 admin.analyse_indexing(project_env, osm_id='W12345674')
65 def test_analyse_indexing_with_place_id(project_env, placex_row):
66 place_id = placex_row()
68 admin.analyse_indexing(project_env, place_id=place_id)
71 def test_analyse_indexing_with_osm_id(project_env, placex_row):
72 placex_row(osm_type='N', osm_id=10000)
74 admin.analyse_indexing(project_env, osm_id='N10000')
77 class TestAdminCleanDeleted:
79 @pytest.fixture(autouse=True)
80 def setup_polygon_delete(self, project_env, table_factory, place_table, placex_row,
81 osmline_table, temp_db_cursor, temp_db_conn, def_config, src_dir):
82 """ Set up place_force_delete function and related tables
84 self.project_env = project_env
85 self.temp_db_cursor = temp_db_cursor
86 table_factory('import_polygon_delete',
90 type TEXT NOT NULL""",
91 ((100, 'N', 'boundary', 'administrative'),
92 (145, 'N', 'boundary', 'administrative'),
93 (175, 'R', 'landcover', 'grass')))
95 now = dt.datetime.now()
96 placex_row(osm_type='N', osm_id=100, cls='boundary', typ='administrative',
97 indexed_status=1, indexed_date=now - dt.timedelta(days=30))
98 placex_row(osm_type='N', osm_id=145, cls='boundary', typ='administrative',
99 indexed_status=1, indexed_date=now - dt.timedelta(days=90))
100 placex_row(osm_type='R', osm_id=175, cls='landcover', typ='grass',
101 indexed_status=1, indexed_date=now - dt.timedelta(days=90))
103 # set up tables and triggers for utils function
104 table_factory('place_to_be_deleted',
110 table_factory('import_polygon_error', """osm_id BIGINT,
113 type TEXT NOT NULL""")
114 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION place_delete()
115 RETURNS TRIGGER AS $$
116 BEGIN RETURN NULL; END;
117 $$ LANGUAGE plpgsql;""")
118 temp_db_cursor.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
119 FOR EACH ROW EXECUTE PROCEDURE place_delete();""")
120 orig_sql = def_config.lib_dir.sql
121 def_config.lib_dir.sql = src_dir / 'lib-sql'
122 sqlproc = SQLPreprocessor(temp_db_conn, def_config)
123 sqlproc.run_sql_file(temp_db_conn, 'functions/utils.sql')
124 def_config.lib_dir.sql = orig_sql
126 def test_admin_clean_deleted_no_records(self):
127 admin.clean_deleted_relations(self.project_env, age='1 year')
129 rowset = self.temp_db_cursor.row_set(
130 'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
132 assert rowset == {(100, 'N', 'boundary', 'administrative', 1),
133 (145, 'N', 'boundary', 'administrative', 1),
134 (175, 'R', 'landcover', 'grass', 1)}
135 assert self.temp_db_cursor.table_rows('import_polygon_delete') == 3
137 @pytest.mark.parametrize('test_age', ['T week', '1 welk', 'P1E'])
138 def test_admin_clean_deleted_bad_age(self, test_age):
139 with pytest.raises(UsageError):
140 admin.clean_deleted_relations(self.project_env, age=test_age)
142 def test_admin_clean_deleted_partial(self):
143 admin.clean_deleted_relations(self.project_env, age='2 months')
145 rowset = self.temp_db_cursor.row_set(
146 'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
148 assert rowset == {(100, 'N', 'boundary', 'administrative', 1),
149 (145, 'N', 'boundary', 'administrative', 100),
150 (175, 'R', 'landcover', 'grass', 100)}
151 assert self.temp_db_cursor.table_rows('import_polygon_delete') == 1
153 @pytest.mark.parametrize('test_age', ['1 week', 'P3D', '5 hours'])
154 def test_admin_clean_deleted(self, test_age):
155 admin.clean_deleted_relations(self.project_env, age=test_age)
157 rowset = self.temp_db_cursor.row_set(
158 'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
160 assert rowset == {(100, 'N', 'boundary', 'administrative', 100),
161 (145, 'N', 'boundary', 'administrative', 100),
162 (175, 'R', 'landcover', 'grass', 100)}
163 assert self.temp_db_cursor.table_rows('import_polygon_delete') == 0