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 database integrity checks.
 
  12 from nominatim_db.tools import check_database as chkdb
 
  13 import nominatim_db.version
 
  16 def test_check_database_unknown_db(def_config, monkeypatch):
 
  17     monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'pgsql:dbname=fjgkhughwgh2423gsags')
 
  18     assert chkdb.check_database(def_config) == 1
 
  21 def test_check_database_fatal_test(def_config, temp_db):
 
  22     assert chkdb.check_database(def_config) == 1
 
  25 def test_check_connection_good(temp_db_conn, def_config):
 
  26     assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  29 def test_check_connection_bad(def_config):
 
  30     badconn = chkdb._BadConnection('Error')
 
  31     assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL
 
  34 def test_check_database_version_not_found(property_table, temp_db_conn, def_config):
 
  35     assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.FATAL
 
  38 def test_check_database_version_good(property_table, temp_db_conn, def_config):
 
  39     property_table.set('database_version',
 
  40                        str(nominatim_db.version.NOMINATIM_VERSION))
 
  41     assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  44 def test_check_database_version_bad(property_table, temp_db_conn, def_config):
 
  45     property_table.set('database_version', '3.9.9-9')
 
  46     assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.FATAL
 
  49 def test_check_placex_table_good(table_factory, temp_db_conn, def_config):
 
  50     table_factory('placex')
 
  51     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  54 def test_check_placex_table_bad(temp_db_conn, def_config):
 
  55     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.FATAL
 
  58 def test_check_placex_table_size_good(table_factory, temp_db_conn, def_config):
 
  59     table_factory('placex', content=((1, ), (2, )))
 
  60     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  63 def test_check_placex_table_size_bad(table_factory, temp_db_conn, def_config):
 
  64     table_factory('placex')
 
  65     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.FATAL
 
  68 def test_check_tokenizer_missing(temp_db_conn, def_config, tmp_path):
 
  69     def_config.project_dir = tmp_path
 
  70     assert chkdb.check_tokenizer(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
  73 @pytest.mark.parametrize("check_result,state", [(None, chkdb.CheckState.OK),
 
  74                                                 ("Something wrong", chkdb.CheckState.FAIL)])
 
  75 def test_check_tokenizer(temp_db_conn, def_config, monkeypatch,
 
  79         def check_database(_):
 
  82     monkeypatch.setattr(chkdb.tokenizer_factory, 'get_tokenizer_for_db',
 
  83                         lambda *a, **k: _TestTokenizer())
 
  84     assert chkdb.check_tokenizer(temp_db_conn, def_config) == state
 
  87 def test_check_indexing_good(table_factory, temp_db_conn, def_config):
 
  88     table_factory('placex', 'place_id int, indexed_status smallint',
 
  89                   content=((1, 0), (2, 0)))
 
  90     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  93 def test_check_indexing_bad(table_factory, temp_db_conn, def_config):
 
  94     table_factory('placex', 'place_id int, indexed_status smallint',
 
  95                   content=((1, 0), (2, 2)))
 
  96     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.WARN
 
  99 def test_check_database_indexes_bad(temp_db_conn, def_config):
 
 100     assert chkdb.check_database_indexes(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
 103 def test_check_database_indexes_valid(temp_db_conn, def_config):
 
 104     assert chkdb.check_database_index_valid(temp_db_conn, def_config) == chkdb.CheckState.OK
 
 107 def test_check_tiger_table_disabled(temp_db_conn, def_config, monkeypatch):
 
 108     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'no')
 
 109     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.NOT_APPLICABLE
 
 112 def test_check_tiger_table_enabled(temp_db_cursor, temp_db_conn, def_config, monkeypatch):
 
 113     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
 
 114     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
 116     temp_db_cursor.execute('CREATE TABLE location_property_tiger (place_id int)')
 
 117     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
 119     temp_db_cursor.execute('INSERT INTO location_property_tiger VALUES (1), (2)')
 
 120     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.OK