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
 
  16 @pytest.fixture(autouse=True)
 
  17 def create_placex_table(project_env, tokenizer_mock, temp_db_cursor, placex_table):
 
  18     """ All tests in this module require the placex table to be set up.
 
  20     temp_db_cursor.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
 
  21     temp_db_cursor.execute("""CREATE TYPE prepare_update_info AS (
 
  24                              rank_address SMALLINT,
 
  28                              linked_place_id BIGINT
 
  30     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
 
  31                                                      OUT result prepare_update_info)
 
  34                              result.address := p.address;
 
  35                              result.name := p.name;
 
  36                              result.class := p.class;
 
  37                              result.type := p.type;
 
  38                              result.country_code := p.country_code;
 
  39                              result.rank_address := p.rank_address;
 
  41                            $$ LANGUAGE plpgsql STABLE;
 
  43     factory.create_tokenizer(project_env)
 
  46 def test_analyse_indexing_no_objects(project_env):
 
  47     with pytest.raises(UsageError):
 
  48         admin.analyse_indexing(project_env)
 
  51 @pytest.mark.parametrize("oid", ['1234', 'N123a', 'X123'])
 
  52 def test_analyse_indexing_bad_osmid(project_env, oid):
 
  53     with pytest.raises(UsageError):
 
  54         admin.analyse_indexing(project_env, osm_id=oid)
 
  57 def test_analyse_indexing_unknown_osmid(project_env):
 
  58     with pytest.raises(UsageError):
 
  59         admin.analyse_indexing(project_env, osm_id='W12345674')
 
  62 def test_analyse_indexing_with_place_id(project_env, temp_db_cursor):
 
  63     temp_db_cursor.execute("INSERT INTO placex (place_id) VALUES(12345)")
 
  65     admin.analyse_indexing(project_env, place_id=12345)
 
  68 def test_analyse_indexing_with_osm_id(project_env, temp_db_cursor):
 
  69     temp_db_cursor.execute("""INSERT INTO placex (place_id, osm_type, osm_id)
 
  70                               VALUES(9988, 'N', 10000)""")
 
  72     admin.analyse_indexing(project_env, osm_id='N10000')