]> git.openstreetmap.org Git - nominatim.git/commitdiff
add unit tests for new check_database code
authorSarah Hoffmann <lonvia@denofr.de>
Thu, 18 Feb 2021 19:36:11 +0000 (20:36 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Thu, 18 Feb 2021 19:36:11 +0000 (20:36 +0100)
nominatim/tools/check_database.py
test/python/test_db_connection.py
test/python/test_tools_check_database.py [new file with mode: 0644]

index e99a3572d49553b28eee6ca3b14f7baf93c55fa7..3031026b8cbeba3f01edd1d164187e7d8242b9c2 100644 (file)
@@ -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}
index ef1ae7416cc9d23a3d3c11433d5ea444c3c9262b..11ad691aa64e3ab7bef6b182936a2ad57529d670 100644 (file)
@@ -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 (file)
index 0000000..0b5c23a
--- /dev/null
@@ -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
+