add unit tests for new check_database code

This commit is contained in:
Sarah Hoffmann
2021-02-18 20:36:11 +01:00
parent b169e4c88c
commit a0ae4945cd
3 changed files with 97 additions and 2 deletions

View File

@@ -154,6 +154,7 @@ def check_placex_size(conn, config): # pylint: disable=W0613
return CheckState.OK if cnt > 0 else CheckState.FATAL return CheckState.OK if cnt > 0 else CheckState.FATAL
@_check(hint="""\ @_check(hint="""\
The Postgresql extension nominatim.so was not correctly loaded. The Postgresql extension nominatim.so was not correctly loaded.
@@ -198,13 +199,12 @@ def check_indexing(conn, config): # pylint: disable=W0613
# Likely just an interrupted update. # Likely just an interrupted update.
index_cmd = 'nominatim index' index_cmd = 'nominatim index'
else: else:
# Looks like the import process got interupted. # Looks like the import process got interrupted.
index_cmd = 'nominatim import --continue indexing' index_cmd = 'nominatim import --continue indexing'
return CheckState.FAIL, dict(count=cnt, index_cmd=index_cmd) return CheckState.FAIL, dict(count=cnt, index_cmd=index_cmd)
@_check(hint="""\ @_check(hint="""\
The following indexes are missing: The following indexes are missing:
{indexes} {indexes}

View File

@@ -20,12 +20,31 @@ def test_connection_table_exists(db, temp_db_cursor):
assert db.table_exists('foobar') == True assert db.table_exists('foobar') == True
def test_connection_index_exists(db, temp_db_cursor):
assert db.index_exists('some_index') == False
temp_db_cursor.execute('CREATE TABLE foobar (id INT)')
temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
assert db.index_exists('some_index') == True
assert db.index_exists('some_index', table='foobar') == True
assert db.index_exists('some_index', table='bar') == False
def test_connection_server_version_tuple(db):
ver = db.server_version_tuple()
assert isinstance(ver, tuple)
assert len(ver) == 2
assert ver[0] > 8
def test_cursor_scalar(db, temp_db_cursor): def test_cursor_scalar(db, temp_db_cursor):
temp_db_cursor.execute('CREATE TABLE dummy (id INT)') temp_db_cursor.execute('CREATE TABLE dummy (id INT)')
with db.cursor() as cur: with db.cursor() as cur:
assert cur.scalar('SELECT count(*) FROM dummy') == 0 assert cur.scalar('SELECT count(*) FROM dummy') == 0
def test_cursor_scalar_many_rows(db): def test_cursor_scalar_many_rows(db):
with db.cursor() as cur: with db.cursor() as cur:
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):

View File

@@ -0,0 +1,76 @@
"""
Tests for database integrity checks.
"""
import pytest
from nominatim.tools import check_database as chkdb
def test_check_database_unknown_db(def_config, monkeypatch):
monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'pgsql:dbname=fjgkhughwgh2423gsags')
assert 1 == chkdb.check_database(def_config)
def test_check_conection_good(temp_db_conn, def_config):
assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK
def test_check_conection_bad(def_config):
badconn = chkdb._BadConnection('Error')
assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL
def test_check_placex_table_good(temp_db_cursor, temp_db_conn, def_config):
temp_db_cursor.execute('CREATE TABLE placex (place_id int)')
assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.OK
def test_check_placex_table_bad(temp_db_conn, def_config):
assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.FATAL
def test_check_placex_table_size_good(temp_db_cursor, temp_db_conn, def_config):
temp_db_cursor.execute('CREATE TABLE placex (place_id int)')
temp_db_cursor.execute('INSERT INTO placex VALUES (1), (2)')
assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.OK
def test_check_placex_table_size_bad(temp_db_cursor, temp_db_conn, def_config):
temp_db_cursor.execute('CREATE TABLE placex (place_id int)')
assert chkdb.check_placex_size(temp_db_conn, def_config) == chkdb.CheckState.FATAL
def test_check_module_bad(temp_db_conn, def_config):
assert chkdb.check_module(temp_db_conn, def_config) == chkdb.CheckState.FAIL
def test_check_indexing_good(temp_db_cursor, temp_db_conn, def_config):
temp_db_cursor.execute('CREATE TABLE placex (place_id int, indexed_status smallint)')
temp_db_cursor.execute('INSERT INTO placex VALUES (1, 0), (2, 0)')
assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.OK
def test_check_indexing_bad(temp_db_cursor, temp_db_conn, def_config):
temp_db_cursor.execute('CREATE TABLE placex (place_id int, indexed_status smallint)')
temp_db_cursor.execute('INSERT INTO placex VALUES (1, 0), (2, 2)')
assert chkdb.check_indexing(temp_db_conn, def_config) == chkdb.CheckState.FAIL
def test_check_database_indexes_bad(temp_db_conn, def_config):
assert chkdb.check_database_indexes(temp_db_conn, def_config) == chkdb.CheckState.FAIL
def test_check_tiger_table_disabled(temp_db_conn, def_config, monkeypatch):
monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA' , 'no')
assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.NOT_APPLICABLE
def test_check_tiger_table_enabled(temp_db_cursor, temp_db_conn, def_config, monkeypatch):
monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA' , 'yes')
assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
temp_db_cursor.execute('CREATE TABLE location_property_tiger (place_id int)')
assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.FAIL
temp_db_cursor.execute('INSERT INTO location_property_tiger VALUES (1), (2)')
assert chkdb.check_tiger_table(temp_db_conn, def_config) == chkdb.CheckState.OK