1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for maintenance and analysis functions.
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
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.
21 temp_db_cursor.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
22 temp_db_cursor.execute("""CREATE TYPE prepare_update_info AS (
25 rank_address SMALLINT,
29 linked_place_id BIGINT
31 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
32 OUT result prepare_update_info)
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;
42 $$ LANGUAGE plpgsql STABLE;
44 factory.create_tokenizer(project_env)
47 def test_analyse_indexing_no_objects(project_env):
48 with pytest.raises(UsageError):
49 admin.analyse_indexing(project_env)
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)
58 def test_analyse_indexing_unknown_osmid(project_env):
59 with pytest.raises(UsageError):
60 admin.analyse_indexing(project_env, osm_id='W12345674')
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)")
66 admin.analyse_indexing(project_env, place_id=12345)
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)""")
73 admin.analyse_indexing(project_env, osm_id='N10000')
76 class TestAdminCleanDeleted:
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
82 self.project_env = project_env
83 self.temp_db_cursor = temp_db_cursor
84 table_factory('import_polygon_delete',
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 '3 month', 1),
95 (3, 175, 'R', 'landcover', 'grass', current_date - INTERVAL '3 months', 1)""")
96 # set up tables and triggers for utils function
97 table_factory('place_to_be_deleted',
103 table_factory('country_name', 'partition INT')
104 table_factory('import_polygon_error', """osm_id BIGINT,
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
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
129 @pytest.mark.parametrize('test_age', ['T week', '1 welk', 'P1E'])
130 def test_admin_clean_deleted_bad_age(self, test_age):
131 with pytest.raises(UsageError):
132 admin.clean_deleted_relations(self.project_env, age = test_age)
135 def test_admin_clean_deleted_partial(self):
136 admin.clean_deleted_relations(self.project_env, age = '2 months')
137 assert self.temp_db_cursor.row_set('SELECT osm_id, osm_type, class, type, indexed_status FROM placex') == {(100, 'N', 'boundary', 'administrative', 1),
138 (145, 'N', 'boundary', 'administrative', 100),
139 (175, 'R', 'landcover', 'grass', 100)}
140 assert self.temp_db_cursor.table_rows('import_polygon_delete') == 1
142 @pytest.mark.parametrize('test_age', ['1 week', 'P3D', '5 hours'])
143 def test_admin_clean_deleted(self, test_age):
144 admin.clean_deleted_relations(self.project_env, age = test_age)
145 assert self.temp_db_cursor.row_set('SELECT osm_id, osm_type, class, type, indexed_status FROM placex') == {(100, 'N', 'boundary', 'administrative', 100),
146 (145, 'N', 'boundary', 'administrative', 100),
147 (175, 'R', 'landcover', 'grass', 100)}
148 assert self.temp_db_cursor.table_rows('import_polygon_delete') == 0