]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_admin.py
update existing search tests to pass PlaceID objects instead of integers
[nominatim.git] / test / python / tools / test_admin.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for maintenance and analysis functions.
9 """
10 import pytest
11 import datetime as dt
12
13 from nominatim_db.errors import UsageError
14 from nominatim_db.tools import admin
15 from nominatim_db.tokenizer import factory
16
17
18 @pytest.fixture(autouse=True)
19 def create_placex_table(project_env, tokenizer_mock, temp_db_cursor, placex_table):
20     """ All tests in this module require the placex table to be set up.
21     """
22     temp_db_cursor.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
23     temp_db_cursor.execute("""CREATE TYPE prepare_update_info AS (
24                              name HSTORE,
25                              address HSTORE,
26                              rank_address SMALLINT,
27                              country_code TEXT,
28                              class TEXT,
29                              type TEXT,
30                              linked_place_id BIGINT
31                            )""")
32     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
33                                                      OUT result prepare_update_info)
34                            AS $$
35                            BEGIN
36                              result.address := p.address;
37                              result.name := p.name;
38                              result.class := p.class;
39                              result.type := p.type;
40                              result.country_code := p.country_code;
41                              result.rank_address := p.rank_address;
42                            END;
43                            $$ LANGUAGE plpgsql STABLE;
44                         """)
45     factory.create_tokenizer(project_env)
46
47
48 def test_analyse_indexing_no_objects(project_env):
49     with pytest.raises(UsageError):
50         admin.analyse_indexing(project_env)
51
52
53 @pytest.mark.parametrize("oid", ['1234', 'N123a', 'X123'])
54 def test_analyse_indexing_bad_osmid(project_env, oid):
55     with pytest.raises(UsageError):
56         admin.analyse_indexing(project_env, osm_id=oid)
57
58
59 def test_analyse_indexing_unknown_osmid(project_env):
60     with pytest.raises(UsageError):
61         admin.analyse_indexing(project_env, osm_id='W12345674')
62
63
64 def test_analyse_indexing_with_place_id(project_env, placex_row):
65     place_id = placex_row()
66
67     admin.analyse_indexing(project_env, place_id=place_id)
68
69
70 def test_analyse_indexing_with_osm_id(project_env, placex_row):
71     placex_row(osm_type='N', osm_id=10000)
72
73     admin.analyse_indexing(project_env, osm_id='N10000')
74
75
76 class TestAdminCleanDeleted:
77
78     @pytest.fixture(autouse=True)
79     def setup_polygon_delete(self, project_env, table_factory, place_interpolation_table,
80                              placex_row, osmline_table, temp_db_cursor, load_sql):
81         """ Set up place_force_delete function and related tables
82         """
83         self.project_env = project_env
84         self.temp_db_cursor = temp_db_cursor
85         table_factory('import_polygon_delete',
86                       """osm_id BIGINT,
87                       osm_type CHAR(1),
88                       class TEXT NOT NULL,
89                       type TEXT NOT NULL""",
90                       ((100, 'N', 'boundary', 'administrative'),
91                        (145, 'N', 'boundary', 'administrative'),
92                        (175, 'R', 'landcover', 'grass')))
93
94         now = dt.datetime.now()
95         placex_row(osm_type='N', osm_id=100, cls='boundary', typ='administrative',
96                    indexed_status=1, indexed_date=now - dt.timedelta(days=30))
97         placex_row(osm_type='N', osm_id=145, cls='boundary', typ='administrative',
98                    indexed_status=1, indexed_date=now - dt.timedelta(days=90))
99         placex_row(osm_type='R', osm_id=175, cls='landcover', typ='grass',
100                    indexed_status=1, indexed_date=now - dt.timedelta(days=90))
101
102         # set up tables and triggers for utils function
103         table_factory('place_to_be_deleted',
104                       """osm_id BIGINT,
105                       osm_type CHAR(1),
106                       class TEXT NOT NULL,
107                       type TEXT NOT NULL,
108                       deferred BOOLEAN""")
109         table_factory('place_interpolation_to_be_deleted',
110                       """osm_id BIGINT,
111                       osm_type CHAR(1)""")
112         table_factory('import_polygon_error', """osm_id BIGINT,
113                       osm_type CHAR(1),
114                       class TEXT NOT NULL,
115                       type TEXT NOT NULL""")
116         temp_db_cursor.execute("""
117             CREATE OR REPLACE FUNCTION place_delete() RETURNS TRIGGER AS $$
118                 BEGIN RETURN NULL; END;
119             $$ LANGUAGE plpgsql;
120
121             CREATE TRIGGER place_before_delete BEFORE DELETE ON place
122             FOR EACH ROW EXECUTE PROCEDURE place_delete();
123
124             CREATE TRIGGER place_interpolation_before_delete BEFORE DELETE ON place_interpolation
125             FOR EACH ROW EXECUTE PROCEDURE place_delete();
126         """)
127         load_sql('functions/utils.sql')
128
129     def test_admin_clean_deleted_no_records(self):
130         admin.clean_deleted_relations(self.project_env, age='1 year')
131
132         rowset = self.temp_db_cursor.row_set(
133             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
134
135         assert rowset == {(100, 'N', 'boundary', 'administrative', 1),
136                           (145, 'N', 'boundary', 'administrative', 1),
137                           (175, 'R', 'landcover', 'grass', 1)}
138         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 3
139
140     @pytest.mark.parametrize('test_age', ['T week', '1 welk', 'P1E'])
141     def test_admin_clean_deleted_bad_age(self, test_age):
142         with pytest.raises(UsageError):
143             admin.clean_deleted_relations(self.project_env, age=test_age)
144
145     def test_admin_clean_deleted_partial(self):
146         admin.clean_deleted_relations(self.project_env, age='2 months')
147
148         rowset = self.temp_db_cursor.row_set(
149             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
150
151         assert rowset == {(100, 'N', 'boundary', 'administrative', 1),
152                           (145, 'N', 'boundary', 'administrative', 100),
153                           (175, 'R', 'landcover', 'grass', 100)}
154         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 1
155
156     @pytest.mark.parametrize('test_age', ['1 week', 'P3D', '5 hours'])
157     def test_admin_clean_deleted(self, test_age):
158         admin.clean_deleted_relations(self.project_env, age=test_age)
159
160         rowset = self.temp_db_cursor.row_set(
161             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
162
163         assert rowset == {(100, 'N', 'boundary', 'administrative', 100),
164                           (145, 'N', 'boundary', 'administrative', 100),
165                           (175, 'R', 'landcover', 'grass', 100)}
166         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 0