]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_check_database.py
add tests for discarding bad postcodes
[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
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
17
18
19 def test_check_database_fatal_test(def_config, temp_db):
20     assert chkdb.check_database(def_config) == 1
21
22
23 def test_check_conection_good(temp_db_conn, def_config):
24     assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK
25
26
27 def test_check_conection_bad(def_config):
28     badconn = chkdb._BadConnection('Error')
29     assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL
30
31
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
35
36
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
39
40
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
44
45
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
49
50
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
54
55
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,
59                          check_result, state):
60     class _TestTokenizer:
61         @staticmethod
62         def check_database(_):
63             return check_result
64
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
68
69
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
74
75
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
80
81
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
84
85
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
88
89
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
93
94
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
98
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
101
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