]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_admin.py
moved sql function flush_deleted_places() to utils
[nominatim.git] / test / python / tools / test_admin.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 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
12 from nominatim.errors import UsageError
13 from nominatim.tools import admin
14 from nominatim.tokenizer import factory
15 from nominatim.db.sql_preprocessor import SQLPreprocessor
16
17 @pytest.fixture(autouse=True)
18 def create_placex_table(project_env, tokenizer_mock, temp_db_cursor, placex_table):
19     """ All tests in this module require the placex table to be set up.
20     """
21     temp_db_cursor.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
22     temp_db_cursor.execute("""CREATE TYPE prepare_update_info AS (
23                              name HSTORE,
24                              address HSTORE,
25                              rank_address SMALLINT,
26                              country_code TEXT,
27                              class TEXT,
28                              type TEXT,
29                              linked_place_id BIGINT
30                            )""")
31     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
32                                                      OUT result prepare_update_info)
33                            AS $$
34                            BEGIN
35                              result.address := p.address;
36                              result.name := p.name;
37                              result.class := p.class;
38                              result.type := p.type;
39                              result.country_code := p.country_code;
40                              result.rank_address := p.rank_address;
41                            END;
42                            $$ LANGUAGE plpgsql STABLE;
43                         """)
44     factory.create_tokenizer(project_env)
45
46
47 def test_analyse_indexing_no_objects(project_env):
48     with pytest.raises(UsageError):
49         admin.analyse_indexing(project_env)
50
51
52 @pytest.mark.parametrize("oid", ['1234', 'N123a', 'X123'])
53 def test_analyse_indexing_bad_osmid(project_env, oid):
54     with pytest.raises(UsageError):
55         admin.analyse_indexing(project_env, osm_id=oid)
56
57
58 def test_analyse_indexing_unknown_osmid(project_env):
59     with pytest.raises(UsageError):
60         admin.analyse_indexing(project_env, osm_id='W12345674')
61
62
63 def test_analyse_indexing_with_place_id(project_env, temp_db_cursor):
64     temp_db_cursor.execute("INSERT INTO placex (place_id) VALUES(12345)")
65
66     admin.analyse_indexing(project_env, place_id=12345)
67
68
69 def test_analyse_indexing_with_osm_id(project_env, temp_db_cursor):
70     temp_db_cursor.execute("""INSERT INTO placex (place_id, osm_type, osm_id)
71                               VALUES(9988, 'N', 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_table, osmline_table, temp_db_cursor, temp_db_conn, def_config, src_dir):
80         """ Set up place_force_delete function and related tables
81         """
82         self.project_env = project_env
83         self.temp_db_cursor = temp_db_cursor
84         table_factory('import_polygon_delete',
85                       """osm_id BIGINT,
86                       osm_type CHAR(1),
87                       class TEXT NOT NULL,
88                       type TEXT NOT NULL""",
89                       ((100, 'N', 'boundary', 'administrative'),
90                       (145, 'N', 'boundary', 'administrative'),
91                       (175, 'R', 'landcover', 'grass')))
92         temp_db_cursor.execute("""INSERT INTO placex (place_id, osm_id, osm_type, class, type, indexed_date, indexed_status)
93                               VALUES(1, 100, 'N', 'boundary', 'administrative', current_date - INTERVAL '1 month', 1),
94                                (2, 145, 'N', 'boundary', 'administrative', current_date - INTERVAL '1 month', 1),
95                                (3, 175, 'R', 'landcover', 'grass', current_date - INTERVAL '1 month', 1)""")
96         # set up tables and triggers for utils function
97         table_factory('place_to_be_deleted',
98                       """osm_id BIGINT,
99                       osm_type CHAR(1),
100                       class TEXT NOT NULL,
101                       type TEXT NOT NULL,
102                       deferred BOOLEAN""")
103         table_factory('country_name', 'partition INT')
104         table_factory('import_polygon_error', """osm_id BIGINT,
105                       osm_type CHAR(1),
106                       class TEXT NOT NULL,
107                       type TEXT NOT NULL""")
108         temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION place_delete()
109                                RETURNS TRIGGER AS $$
110                                BEGIN RETURN NULL; END;
111                                $$ LANGUAGE plpgsql;""")
112         temp_db_cursor.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
113                                FOR EACH ROW EXECUTE PROCEDURE place_delete();""")
114         orig_sql = def_config.lib_dir.sql
115         def_config.lib_dir.sql = src_dir / 'lib-sql'
116         sqlproc = SQLPreprocessor(temp_db_conn, def_config)
117         sqlproc.run_sql_file(temp_db_conn, 'functions/utils.sql')
118         def_config.lib_dir.sql = orig_sql
119         
120
121     def test_admin_clean_deleted_no_records(self):
122         admin.clean_deleted_relations(self.project_env, age='1 year')
123         assert self.temp_db_cursor.row_set('SELECT osm_id, osm_type, class, type, indexed_status FROM placex') == {(100, 'N', 'boundary', 'administrative', 1),
124                                                                                                                    (145, 'N', 'boundary', 'administrative', 1),
125                                                                                                                    (175, 'R', 'landcover', 'grass', 1)}
126         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 3
127
128
129     def test_admin_clean_deleted_no_age(self):
130         with pytest.raises(UsageError):
131             admin.clean_deleted_relations(self.project_env)
132
133
134     @pytest.mark.parametrize('test_age', ['T week', '1 welk', 'P1E'])
135     def test_admin_clean_deleted_bad_age(self, test_age):
136         with pytest.raises(UsageError):
137             admin.clean_deleted_relations(self.project_env, age = test_age)
138
139
140     @pytest.mark.parametrize('test_age', ['1 week', 'P3D', '5 hours'])
141     def test_admin_clean_deleted(self, test_age):
142         admin.clean_deleted_relations(self.project_env, age = test_age)
143         assert self.temp_db_cursor.row_set('SELECT osm_id, osm_type, class, type, indexed_status FROM placex') == {(100, 'N', 'boundary', 'administrative', 100),
144                                                                                                                    (145, 'N', 'boundary', 'administrative', 100),
145                                                                                                                    (175, 'R', 'landcover', 'grass', 100)}
146         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 0