]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_admin.py
reorganise fixtures for placex table
[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 from nominatim_db.db.sql_preprocessor import SQLPreprocessor
17
18
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.
22     """
23     temp_db_cursor.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
24     temp_db_cursor.execute("""CREATE TYPE prepare_update_info AS (
25                              name HSTORE,
26                              address HSTORE,
27                              rank_address SMALLINT,
28                              country_code TEXT,
29                              class TEXT,
30                              type TEXT,
31                              linked_place_id BIGINT
32                            )""")
33     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
34                                                      OUT result prepare_update_info)
35                            AS $$
36                            BEGIN
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;
43                            END;
44                            $$ LANGUAGE plpgsql STABLE;
45                         """)
46     factory.create_tokenizer(project_env)
47
48
49 def test_analyse_indexing_no_objects(project_env):
50     with pytest.raises(UsageError):
51         admin.analyse_indexing(project_env)
52
53
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)
58
59
60 def test_analyse_indexing_unknown_osmid(project_env):
61     with pytest.raises(UsageError):
62         admin.analyse_indexing(project_env, osm_id='W12345674')
63
64
65 def test_analyse_indexing_with_place_id(project_env, placex_row):
66     place_id = placex_row()
67
68     admin.analyse_indexing(project_env, place_id=place_id)
69
70
71 def test_analyse_indexing_with_osm_id(project_env, placex_row):
72     placex_row(osm_type='N', osm_id=10000)
73
74     admin.analyse_indexing(project_env, osm_id='N10000')
75
76
77 class TestAdminCleanDeleted:
78
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
83         """
84         self.project_env = project_env
85         self.temp_db_cursor = temp_db_cursor
86         table_factory('import_polygon_delete',
87                       """osm_id BIGINT,
88                       osm_type CHAR(1),
89                       class TEXT NOT NULL,
90                       type TEXT NOT NULL""",
91                       ((100, 'N', 'boundary', 'administrative'),
92                        (145, 'N', 'boundary', 'administrative'),
93                        (175, 'R', 'landcover', 'grass')))
94
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))
102
103         # set up tables and triggers for utils function
104         table_factory('place_to_be_deleted',
105                       """osm_id BIGINT,
106                       osm_type CHAR(1),
107                       class TEXT NOT NULL,
108                       type TEXT NOT NULL,
109                       deferred BOOLEAN""")
110         table_factory('import_polygon_error', """osm_id BIGINT,
111                       osm_type CHAR(1),
112                       class TEXT NOT NULL,
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
125
126     def test_admin_clean_deleted_no_records(self):
127         admin.clean_deleted_relations(self.project_env, age='1 year')
128
129         rowset = self.temp_db_cursor.row_set(
130             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
131
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
136
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)
141
142     def test_admin_clean_deleted_partial(self):
143         admin.clean_deleted_relations(self.project_env, age='2 months')
144
145         rowset = self.temp_db_cursor.row_set(
146             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
147
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
152
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)
156
157         rowset = self.temp_db_cursor.row_set(
158             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
159
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