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 database integrity checks.
 
  12 from nominatim.tools import check_database as chkdb
 
  14 def test_check_database_unknown_db(def_config, monkeypatch):
 
  15     monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'pgsql:dbname=fjgkhughwgh2423gsags')
 
  16     assert chkdb.check_database(def_config) == 1
 
  19 def test_check_database_fatal_test(def_config, temp_db):
 
  20     assert chkdb.check_database(def_config) == 1
 
  23 def test_check_conection_good(temp_db_conn, def_config):
 
  24     assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  27 def test_check_conection_bad(def_config):
 
  28     badconn = chkdb._BadConnection('Error')
 
  29     assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL
 
  32 def test_check_placex_table_good(table_factory, temp_db_conn, def_config):
 
  33     table_factory('placex')
 
  34     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  37 def test_check_placex_table_bad(temp_db_conn, def_config):
 
  38     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.FATAL
 
  41 def test_check_placex_table_size_good(table_factory, temp_db_conn, def_config):
 
  42     table_factory('placex', content=((1, ), (2, )))
 
  43     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  46 def test_check_placex_table_size_bad(table_factory, temp_db_conn, def_config):
 
  47     table_factory('placex')
 
  48     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.FATAL
 
  51 def test_check_tokenizer_missing(temp_db_conn, def_config, tmp_path):
 
  52     def_config.project_dir = tmp_path
 
  53     assert chkdb.check_tokenizer(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
  56 @pytest.mark.parametrize("check_result,state", [(None, chkdb.CheckState.OK),
 
  57                                                 ("Something wrong", chkdb.CheckState.FAIL)])
 
  58 def test_check_tokenizer(temp_db_conn, def_config, monkeypatch,
 
  62         def check_database(_):
 
  65     monkeypatch.setattr(chkdb.tokenizer_factory, 'get_tokenizer_for_db',
 
  66                         lambda *a, **k: _TestTokenizer())
 
  67     assert chkdb.check_tokenizer(temp_db_conn, def_config) == state
 
  70 def test_check_indexing_good(table_factory, temp_db_conn, def_config):
 
  71     table_factory('placex', 'place_id int, indexed_status smallint',
 
  72                   content=((1, 0), (2, 0)))
 
  73     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  76 def test_check_indexing_bad(table_factory, temp_db_conn, def_config):
 
  77     table_factory('placex', 'place_id int, indexed_status smallint',
 
  78                   content=((1, 0), (2, 2)))
 
  79     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
  82 def test_check_database_indexes_bad(temp_db_conn, def_config):
 
  83     assert chkdb.check_database_indexes(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
  86 def test_check_database_indexes_valid(temp_db_conn, def_config):
 
  87     assert chkdb.check_database_index_valid(temp_db_conn, def_config) == chkdb.CheckState.OK
 
  90 def test_check_tiger_table_disabled(temp_db_conn, def_config, monkeypatch):
 
  91     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'no')
 
  92     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.NOT_APPLICABLE
 
  95 def test_check_tiger_table_enabled(temp_db_cursor, temp_db_conn, def_config, monkeypatch):
 
  96     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
 
  97     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
  99     temp_db_cursor.execute('CREATE TABLE location_property_tiger (place_id int)')
 
 100     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
 
 102     temp_db_cursor.execute('INSERT INTO location_property_tiger VALUES (1), (2)')
 
 103     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.OK