]> git.openstreetmap.org Git - nominatim.git/commitdiff
cli indexer tests need a fake database
authorSarah Hoffmann <lonvia@denofr.de>
Wed, 20 Jan 2021 19:24:39 +0000 (20:24 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Wed, 20 Jan 2021 20:30:27 +0000 (21:30 +0100)
The Indexer constructor opens a connection to the given database.

test/python/conftest.py
test/python/test_cli.py
test/python/test_indexing.py

index 26bbb397614ef4b6c408002ba0d03408890733fe..1cc9ef9c718eb2884077270f6ce76c42dc267078 100644 (file)
@@ -1,6 +1,26 @@
 import sys
 from pathlib import Path
 
+import psycopg2
+import pytest
+
 # always test against the source
 sys.path.insert(0, str((Path(__file__) / '..' / '..' / '..').resolve()))
 
+@pytest.fixture
+def temp_db(monkeypatch):
+    name = 'test_nominatim_python_unittest'
+    with psycopg2.connect(database='postgres') as conn:
+        conn.set_isolation_level(0)
+        with conn.cursor() as cur:
+            cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
+            cur.execute('CREATE DATABASE {}'.format(name))
+
+    monkeypatch.setenv('NOMINATIM_DATABASE_DSN' , 'dbname=' + name)
+
+    yield name
+
+    with psycopg2.connect(database='postgres') as conn:
+        conn.set_isolation_level(0)
+        with conn.cursor() as cur:
+            cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
index eae4f65a28c7bc98aacdc86588fc1ae01e5f6887..92b42372ca56cadc4ec4b7b60682a3ff050b93e7 100644 (file)
@@ -1,6 +1,7 @@
 """
 Tests for command line interface wrapper.
 """
+import psycopg2
 import pytest
 
 import nominatim.cli
@@ -81,7 +82,10 @@ def test_add_data_command(mock_run_legacy, name, oid):
                           (['--boundaries-only'], 1, 0),
                           (['--no-boundaries'], 0, 1),
                           (['--boundaries-only', '--no-boundaries'], 0, 0)])
-def test_index_command(monkeypatch, params, do_bnds, do_ranks):
+def test_index_command(monkeypatch, temp_db, params, do_bnds, do_ranks):
+    with psycopg2.connect(database=temp_db) as conn:
+        with conn.cursor() as cur:
+            cur.execute("CREATE TABLE import_status (indexed bool)")
     bnd_mock = MockParamCapture()
     monkeypatch.setattr(nominatim.cli.Indexer, 'index_boundaries', bnd_mock)
     rank_mock = MockParamCapture()
index e1a3a4d064433b86394a113933d39b3f89be8e9c..2fe21e80baf15500462d5f9a3314ac267678e23d 100644 (file)
@@ -9,19 +9,11 @@ from nominatim.indexer.indexer import Indexer
 
 class IndexerTestDB:
 
-    def __init__(self, name):
-        self.name = name
-        self.conn = None
+    def __init__(self, conn):
         self.placex_id = itertools.count(100000)
         self.osmline_id = itertools.count(500000)
 
-    def setup(self):
-        with psycopg2.connect(database='postgres') as conn:
-            conn.set_isolation_level(0)
-            with conn.cursor() as cur:
-                cur.execute('DROP DATABASE IF EXISTS {}'.format(self.name))
-                cur.execute('CREATE DATABASE {}'.format(self.name))
-        self.conn = psycopg2.connect(database=self.name)
+        self.conn = conn
         self.conn.set_isolation_level(0)
         with self.conn.cursor() as cur:
             cur.execute("""CREATE TABLE placex (place_id BIGINT,
@@ -52,16 +44,6 @@ class IndexerTestDB:
             cur.execute("""CREATE TRIGGER osmline_update BEFORE UPDATE ON location_property_osmline
                            FOR EACH ROW EXECUTE PROCEDURE date_update()""")
 
-
-    def drop(self):
-        if self.conn:
-            self.conn.close()
-            self.conn = None
-        with psycopg2.connect(database='postgres') as conn:
-            conn.set_isolation_level(0)
-            with conn.cursor() as cur:
-                cur.execute('DROP DATABASE IF EXISTS {}'.format(self.name))
-
     def scalar(self, query):
         with self.conn.cursor() as cur:
             cur.execute(query)
@@ -100,11 +82,10 @@ class IndexerTestDB:
 
 
 @pytest.fixture
-def test_db():
-    db = IndexerTestDB('test_nominatim_python_unittest')
-    db.setup()
-    yield db
-    db.drop()
+def test_db(temp_db):
+    conn = psycopg2.connect(database=temp_db)
+    yield IndexerTestDB(conn)
+    conn.close()
 
 
 @pytest.mark.parametrize("threads", [1, 15])