From: Sarah Hoffmann Date: Thu, 18 Feb 2021 19:36:11 +0000 (+0100) Subject: add unit tests for new check_database code X-Git-Tag: v3.7.0~32^2~4 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/a0ae4945cd60fb395e9d08a8306fb4f7201fa7d1 add unit tests for new check_database code --- diff --git a/nominatim/tools/check_database.py b/nominatim/tools/check_database.py index e99a3572..3031026b 100644 --- a/nominatim/tools/check_database.py +++ b/nominatim/tools/check_database.py @@ -154,6 +154,7 @@ def check_placex_size(conn, config): # pylint: disable=W0613 return CheckState.OK if cnt > 0 else CheckState.FATAL + @_check(hint="""\ 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. index_cmd = 'nominatim index' else: - # Looks like the import process got interupted. + # Looks like the import process got interrupted. index_cmd = 'nominatim import --continue indexing' return CheckState.FAIL, dict(count=cnt, index_cmd=index_cmd) - @_check(hint="""\ The following indexes are missing: {indexes} diff --git a/test/python/test_db_connection.py b/test/python/test_db_connection.py index ef1ae741..11ad691a 100644 --- a/test/python/test_db_connection.py +++ b/test/python/test_db_connection.py @@ -20,12 +20,31 @@ def test_connection_table_exists(db, temp_db_cursor): 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): temp_db_cursor.execute('CREATE TABLE dummy (id INT)') with db.cursor() as cur: assert cur.scalar('SELECT count(*) FROM dummy') == 0 + def test_cursor_scalar_many_rows(db): with db.cursor() as cur: with pytest.raises(RuntimeError): diff --git a/test/python/test_tools_check_database.py b/test/python/test_tools_check_database.py new file mode 100644 index 00000000..0b5c23a6 --- /dev/null +++ b/test/python/test_tools_check_database.py @@ -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 +