]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_check_database.py
enable flake for Python tests
[nominatim.git] / test / python / tools / test_check_database.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for database integrity checks.
9 """
10 import pytest
11
12 from nominatim_db.tools import check_database as chkdb
13 import nominatim_db.version
14
15
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
19
20
21 def test_check_database_fatal_test(def_config, temp_db):
22     assert chkdb.check_database(def_config) == 1
23
24
25 def test_check_connection_good(temp_db_conn, def_config):
26     assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK
27
28
29 def test_check_connection_bad(def_config):
30     badconn = chkdb._BadConnection('Error')
31     assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL
32
33
34 def test_check_database_version_good(property_table, temp_db_conn, def_config):
35     property_table.set('database_version',
36                        str(nominatim_db.version.NOMINATIM_VERSION))
37     assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.OK
38
39
40 def test_check_database_version_bad(property_table, temp_db_conn, def_config):
41     property_table.set('database_version', '3.9.9-9')
42     assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.FATAL
43
44
45 def test_check_placex_table_good(table_factory, temp_db_conn, def_config):
46     table_factory('placex')
47     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.OK
48
49
50 def test_check_placex_table_bad(temp_db_conn, def_config):
51     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.FATAL
52
53
54 def test_check_placex_table_size_good(table_factory, temp_db_conn, def_config):
55     table_factory('placex', content=((1, ), (2, )))
56     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.OK
57
58
59 def test_check_placex_table_size_bad(table_factory, temp_db_conn, def_config):
60     table_factory('placex')
61     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.FATAL
62
63
64 def test_check_tokenizer_missing(temp_db_conn, def_config, tmp_path):
65     def_config.project_dir = tmp_path
66     assert chkdb.check_tokenizer(temp_db_conn, def_config) == chkdb.CheckState.FAIL
67
68
69 @pytest.mark.parametrize("check_result,state", [(None, chkdb.CheckState.OK),
70                                                 ("Something wrong", chkdb.CheckState.FAIL)])
71 def test_check_tokenizer(temp_db_conn, def_config, monkeypatch,
72                          check_result, state):
73     class _TestTokenizer:
74         @staticmethod
75         def check_database(_):
76             return check_result
77
78     monkeypatch.setattr(chkdb.tokenizer_factory, 'get_tokenizer_for_db',
79                         lambda *a, **k: _TestTokenizer())
80     assert chkdb.check_tokenizer(temp_db_conn, def_config) == state
81
82
83 def test_check_indexing_good(table_factory, temp_db_conn, def_config):
84     table_factory('placex', 'place_id int, indexed_status smallint',
85                   content=((1, 0), (2, 0)))
86     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.OK
87
88
89 def test_check_indexing_bad(table_factory, temp_db_conn, def_config):
90     table_factory('placex', 'place_id int, indexed_status smallint',
91                   content=((1, 0), (2, 2)))
92     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.WARN
93
94
95 def test_check_database_indexes_bad(temp_db_conn, def_config):
96     assert chkdb.check_database_indexes(temp_db_conn, def_config) == chkdb.CheckState.FAIL
97
98
99 def test_check_database_indexes_valid(temp_db_conn, def_config):
100     assert chkdb.check_database_index_valid(temp_db_conn, def_config) == chkdb.CheckState.OK
101
102
103 def test_check_tiger_table_disabled(temp_db_conn, def_config, monkeypatch):
104     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'no')
105     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.NOT_APPLICABLE
106
107
108 def test_check_tiger_table_enabled(temp_db_cursor, temp_db_conn, def_config, monkeypatch):
109     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
110     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
111
112     temp_db_cursor.execute('CREATE TABLE location_property_tiger (place_id int)')
113     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
114
115     temp_db_cursor.execute('INSERT INTO location_property_tiger VALUES (1), (2)')
116     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.OK