1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2025 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_db.errors import UsageError
 
  13 from nominatim_db.tools import admin
 
  14 from nominatim_db.tokenizer import factory
 
  15 from nominatim_db.db.sql_preprocessor import SQLPreprocessor
 
  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.
 
  22     temp_db_cursor.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
 
  23     temp_db_cursor.execute("""CREATE TYPE prepare_update_info AS (
 
  26                              rank_address SMALLINT,
 
  30                              linked_place_id BIGINT
 
  32     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
 
  33                                                      OUT result prepare_update_info)
 
  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;
 
  43                            $$ LANGUAGE plpgsql STABLE;
 
  45     factory.create_tokenizer(project_env)
 
  48 def test_analyse_indexing_no_objects(project_env):
 
  49     with pytest.raises(UsageError):
 
  50         admin.analyse_indexing(project_env)
 
  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)
 
  59 def test_analyse_indexing_unknown_osmid(project_env):
 
  60     with pytest.raises(UsageError):
 
  61         admin.analyse_indexing(project_env, osm_id='W12345674')
 
  64 def test_analyse_indexing_with_place_id(project_env, temp_db_cursor):
 
  65     temp_db_cursor.execute("INSERT INTO placex (place_id) VALUES(12345)")
 
  67     admin.analyse_indexing(project_env, place_id=12345)
 
  70 def test_analyse_indexing_with_osm_id(project_env, temp_db_cursor):
 
  71     temp_db_cursor.execute("""INSERT INTO placex (place_id, osm_type, osm_id)
 
  72                               VALUES(9988, 'N', 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,
 
  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')))
 
  94         temp_db_cursor.execute("""
 
  95             INSERT INTO placex (place_id, osm_id, osm_type, class, type,
 
  96                                 indexed_date, indexed_status)
 
  97             VALUES(1, 100, 'N', 'boundary', 'administrative', current_date - INTERVAL '1 month', 1),
 
  98                   (2, 145, 'N', 'boundary', 'administrative', current_date - INTERVAL '3 month', 1),
 
  99                   (3, 175, 'R', 'landcover', 'grass', current_date - INTERVAL '3 months', 1)""")
 
 100         # set up tables and triggers for utils function
 
 101         table_factory('place_to_be_deleted',
 
 107         table_factory('country_name', 'partition INT')
 
 108         table_factory('import_polygon_error', """osm_id BIGINT,
 
 111                       type TEXT NOT NULL""")
 
 112         temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION place_delete()
 
 113                                RETURNS TRIGGER AS $$
 
 114                                BEGIN RETURN NULL; END;
 
 115                                $$ LANGUAGE plpgsql;""")
 
 116         temp_db_cursor.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
 
 117                                FOR EACH ROW EXECUTE PROCEDURE place_delete();""")
 
 118         orig_sql = def_config.lib_dir.sql
 
 119         def_config.lib_dir.sql = src_dir / 'lib-sql'
 
 120         sqlproc = SQLPreprocessor(temp_db_conn, def_config)
 
 121         sqlproc.run_sql_file(temp_db_conn, 'functions/utils.sql')
 
 122         def_config.lib_dir.sql = orig_sql
 
 124     def test_admin_clean_deleted_no_records(self):
 
 125         admin.clean_deleted_relations(self.project_env, age='1 year')
 
 127         rowset = self.temp_db_cursor.row_set(
 
 128             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
 
 130         assert rowset == {(100, 'N', 'boundary', 'administrative', 1),
 
 131                           (145, 'N', 'boundary', 'administrative', 1),
 
 132                           (175, 'R', 'landcover', 'grass', 1)}
 
 133         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 3
 
 135     @pytest.mark.parametrize('test_age', ['T week', '1 welk', 'P1E'])
 
 136     def test_admin_clean_deleted_bad_age(self, test_age):
 
 137         with pytest.raises(UsageError):
 
 138             admin.clean_deleted_relations(self.project_env, age=test_age)
 
 140     def test_admin_clean_deleted_partial(self):
 
 141         admin.clean_deleted_relations(self.project_env, age='2 months')
 
 143         rowset = self.temp_db_cursor.row_set(
 
 144             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
 
 146         assert rowset == {(100, 'N', 'boundary', 'administrative', 1),
 
 147                           (145, 'N', 'boundary', 'administrative', 100),
 
 148                           (175, 'R', 'landcover', 'grass', 100)}
 
 149         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 1
 
 151     @pytest.mark.parametrize('test_age', ['1 week', 'P3D', '5 hours'])
 
 152     def test_admin_clean_deleted(self, test_age):
 
 153         admin.clean_deleted_relations(self.project_env, age=test_age)
 
 155         rowset = self.temp_db_cursor.row_set(
 
 156             'SELECT osm_id, osm_type, class, type, indexed_status FROM placex')
 
 158         assert rowset == {(100, 'N', 'boundary', 'administrative', 100),
 
 159                           (145, 'N', 'boundary', 'administrative', 100),
 
 160                           (175, 'R', 'landcover', 'grass', 100)}
 
 161         assert self.temp_db_cursor.table_rows('import_polygon_delete') == 0