]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_check_database.py
Add check for duplicate partial terms in word table (#4233)
[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) 2026 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_not_found(property_table, temp_db_conn, def_config):
35     assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.FATAL
36
37
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
42
43
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
47
48
49 def test_check_placex_table_good(placex_table, temp_db_conn, def_config):
50     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.OK
51
52
53 def test_check_placex_table_bad(temp_db_conn, def_config):
54     assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.FATAL
55
56
57 def test_check_placex_table_size_good(placex_row, temp_db_conn, def_config):
58     for _ in range(2):
59         placex_row()
60     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.OK
61
62
63 def test_check_placex_table_size_bad(placex_table, temp_db_conn, def_config):
64     assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.FATAL
65
66
67 def test_check_tokenizer_missing(temp_db_conn, def_config, tmp_path):
68     def_config.project_dir = tmp_path
69     assert chkdb.check_tokenizer(temp_db_conn, def_config) == chkdb.CheckState.FAIL
70
71
72 @pytest.mark.parametrize("check_result,state", [(None, chkdb.CheckState.OK),
73                                                 ("Something wrong", chkdb.CheckState.FAIL)])
74 def test_check_tokenizer(temp_db_conn, def_config, monkeypatch,
75                          check_result, state):
76     class _TestTokenizer:
77         @staticmethod
78         def check_database(_):
79             return check_result
80
81     monkeypatch.setattr(chkdb.tokenizer_factory, 'get_tokenizer_for_db',
82                         lambda *a, **k: _TestTokenizer())
83     assert chkdb.check_tokenizer(temp_db_conn, def_config) == state
84
85
86 def test_check_duplicate_partial_terms_no_word_table(temp_db_conn, def_config):
87     assert chkdb.check_duplicate_partial_terms(temp_db_conn, def_config) \
88         == chkdb.CheckState.NOT_APPLICABLE
89
90
91 @pytest.mark.parametrize("words,state", [
92     ([], chkdb.CheckState.OK),
93     ([(1, 'foo', 'w'), (2, 'bar', 'w'), (3, 'foo', 'W')], chkdb.CheckState.OK),
94     ([(1, 'foo', 'w'), (2, 'bar', 'w'), (3, 'foo', 'w')], chkdb.CheckState.FAIL)])
95 def test_check_duplicate_partial_terms(temp_db_cursor, temp_db_conn, def_config,
96                                        words, state):
97     temp_db_cursor.execute("""CREATE TABLE word (word_id INTEGER, word_token text NOT NULL,
98                                                  type text NOT NULL, word text, info jsonb)""")
99     for word in words:
100         temp_db_cursor.execute('INSERT INTO word (word_id, word_token, type) VALUES (%s, %s, %s)',
101                                word)
102
103     assert chkdb.check_duplicate_partial_terms(temp_db_conn, def_config) == state
104
105
106 def test_check_indexing_good(placex_row, temp_db_conn, def_config):
107     for _ in range(2):
108         placex_row(indexed_status=0)
109     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.OK
110
111
112 def test_check_indexing_bad(placex_row, temp_db_conn, def_config):
113     for status in (0, 2):
114         placex_row(indexed_status=status)
115     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.FAIL
116
117
118 def test_check_indexing_bad_frozen(placex_row, temp_db_conn, def_config):
119     for status in (0, 2):
120         placex_row(indexed_status=status)
121     temp_db_conn.execute('DROP TABLE place')
122     assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.WARN
123
124
125 def test_check_database_indexes_bad(temp_db_conn, def_config):
126     assert chkdb.check_database_indexes(temp_db_conn, def_config) == chkdb.CheckState.FAIL
127
128
129 def test_check_database_indexes_valid(temp_db_conn, def_config):
130     assert chkdb.check_database_index_valid(temp_db_conn, def_config) == chkdb.CheckState.OK
131
132
133 def test_check_tiger_table_disabled(temp_db_conn, def_config, monkeypatch):
134     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'no')
135     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.NOT_APPLICABLE
136
137
138 def test_check_tiger_table_enabled(temp_db_cursor, temp_db_conn, def_config, monkeypatch):
139     monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
140     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
141
142     temp_db_cursor.execute('CREATE TABLE location_property_tiger (place_id int)')
143     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
144
145     temp_db_cursor.execute('INSERT INTO location_property_tiger VALUES (1), (2)')
146     assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.OK