]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_check_database.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / tools / test_check_database.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 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.tools import check_database as chkdb
13 import nominatim.version
14
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
18
19
20 def test_check_database_fatal_test(def_config, temp_db):
21     assert chkdb.check_database(def_config) == 1
22
23
24 def test_check_connection_good(temp_db_conn, def_config):
25     assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK
26
27
28 def test_check_connection_bad(def_config):
29     badconn = chkdb._BadConnection('Error')
30     assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL
31
32
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
37
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
41
42
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
46
47
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
50
51
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
55
56
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
60
61
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
65
66
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,
70                          check_result, state):
71     class _TestTokenizer:
72         @staticmethod
73         def check_database(_):
74             return check_result
75
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
79
80
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
85
86
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
91
92
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
95
96
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
99
100
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
104
105
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
109
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
112
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