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
13 import nominatim.version
15 def test_check_database_unknown_db(def_config, monkeypatch):
16 monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'pgsql:dbname=fjgkhughwgh2423gsags')
17 assert chkdb.check_database(def_config) == 1
20 def test_check_database_fatal_test(def_config, temp_db):
21 assert chkdb.check_database(def_config) == 1
24 def test_check_connection_good(temp_db_conn, def_config):
25 assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK
28 def test_check_connection_bad(def_config):
29 badconn = chkdb._BadConnection('Error')
30 assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL
33 def test_check_database_version_good(property_table, temp_db_conn, def_config):
34 property_table.set('database_version',
35 '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(nominatim.version.NOMINATIM_VERSION))
36 assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.OK
38 def test_check_database_version_bad(property_table, temp_db_conn, def_config):
39 property_table.set('database_version', '3.9.9-9')
40 assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.FATAL
43 def test_check_placex_table_good(table_factory, temp_db_conn, def_config):
44 table_factory('placex')
45 assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.OK
48 def test_check_placex_table_bad(temp_db_conn, def_config):
49 assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.FATAL
52 def test_check_placex_table_size_good(table_factory, temp_db_conn, def_config):
53 table_factory('placex', content=((1, ), (2, )))
54 assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.OK
57 def test_check_placex_table_size_bad(table_factory, temp_db_conn, def_config):
58 table_factory('placex')
59 assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.FATAL
62 def test_check_tokenizer_missing(temp_db_conn, def_config, tmp_path):
63 def_config.project_dir = tmp_path
64 assert chkdb.check_tokenizer(temp_db_conn, def_config) == chkdb.CheckState.FAIL
67 @pytest.mark.parametrize("check_result,state", [(None, chkdb.CheckState.OK),
68 ("Something wrong", chkdb.CheckState.FAIL)])
69 def test_check_tokenizer(temp_db_conn, def_config, monkeypatch,
73 def check_database(_):
76 monkeypatch.setattr(chkdb.tokenizer_factory, 'get_tokenizer_for_db',
77 lambda *a, **k: _TestTokenizer())
78 assert chkdb.check_tokenizer(temp_db_conn, def_config) == state
81 def test_check_indexing_good(table_factory, temp_db_conn, def_config):
82 table_factory('placex', 'place_id int, indexed_status smallint',
83 content=((1, 0), (2, 0)))
84 assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.OK
87 def test_check_indexing_bad(table_factory, temp_db_conn, def_config):
88 table_factory('placex', 'place_id int, indexed_status smallint',
89 content=((1, 0), (2, 2)))
90 assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.WARN
93 def test_check_database_indexes_bad(temp_db_conn, def_config):
94 assert chkdb.check_database_indexes(temp_db_conn, def_config) == chkdb.CheckState.FAIL
97 def test_check_database_indexes_valid(temp_db_conn, def_config):
98 assert chkdb.check_database_index_valid(temp_db_conn, def_config) == chkdb.CheckState.OK
101 def test_check_tiger_table_disabled(temp_db_conn, def_config, monkeypatch):
102 monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'no')
103 assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.NOT_APPLICABLE
106 def test_check_tiger_table_enabled(temp_db_cursor, temp_db_conn, def_config, monkeypatch):
107 monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
108 assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
110 temp_db_cursor.execute('CREATE TABLE location_property_tiger (place_id int)')
111 assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
113 temp_db_cursor.execute('INSERT INTO location_property_tiger VALUES (1), (2)')
114 assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.OK