]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/python/conftest.py
test: use src_dir fixture instead of self-computed paths
[nominatim.git] / test / python / conftest.py
index 923e6876a9beaced058600e303c8fa5e874ecafb..77093c4e349e5fe8b0cd85e76322f71a93157a7d 100644 (file)
@@ -1,12 +1,10 @@
-import importlib
 import itertools
 import sys
+import tempfile
 from pathlib import Path
 
 import psycopg2
-import psycopg2.extras
 import pytest
-import tempfile
 
 SRC_DIR = Path(__file__) / '..' / '..' / '..'
 
@@ -17,41 +15,12 @@ from nominatim.config import Configuration
 from nominatim.db import connection
 from nominatim.db.sql_preprocessor import SQLPreprocessor
 from nominatim.db import properties
+import nominatim.tokenizer.factory
+import nominatim.cli
 
 import dummy_tokenizer
 import mocks
-
-class _TestingCursor(psycopg2.extras.DictCursor):
-    """ Extension to the DictCursor class that provides execution
-        short-cuts that simplify writing assertions.
-    """
-
-    def scalar(self, sql, params=None):
-        """ Execute a query with a single return value and return this value.
-            Raises an assertion when not exactly one row is returned.
-        """
-        self.execute(sql, params)
-        assert self.rowcount == 1
-        return self.fetchone()[0]
-
-    def row_set(self, sql, params=None):
-        """ Execute a query and return the result as a set of tuples.
-        """
-        self.execute(sql, params)
-
-        return set((tuple(row) for row in self))
-
-    def table_exists(self, table):
-        """ Check that a table with the given name exists in the database.
-        """
-        num = self.scalar("""SELECT count(*) FROM pg_tables
-                             WHERE tablename = %s""", (table, ))
-        return num == 1
-
-    def table_rows(self, table):
-        """ Return the number of rows in the given table.
-        """
-        return self.scalar('SELECT count(*) FROM ' + table)
+from cursor import CursorForTesting
 
 
 @pytest.fixture
@@ -69,7 +38,7 @@ def temp_db(monkeypatch):
 
     conn.close()
 
-    monkeypatch.setenv('NOMINATIM_DATABASE_DSN' , 'dbname=' + name)
+    monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'dbname=' + name)
 
     yield name
 
@@ -112,7 +81,7 @@ def temp_db_cursor(temp_db):
     """
     conn = psycopg2.connect('dbname=' + temp_db)
     conn.set_isolation_level(0)
-    with conn.cursor(cursor_factory=_TestingCursor) as cur:
+    with conn.cursor(cursor_factory=CursorForTesting) as cur:
         yield cur
     conn.close()
 
@@ -122,8 +91,7 @@ def table_factory(temp_db_cursor):
     def mk_table(name, definition='id INT', content=None):
         temp_db_cursor.execute('CREATE TABLE {} ({})'.format(name, definition))
         if content is not None:
-            psycopg2.extras.execute_values(
-                temp_db_cursor, "INSERT INTO {} VALUES %s".format(name), content)
+            temp_db_cursor.execute_values("INSERT INTO {} VALUES %s".format(name), content)
 
     return mk_table
 
@@ -137,10 +105,28 @@ def def_config():
                     data=SRC_DIR / 'data')
     return cfg
 
+
 @pytest.fixture
 def src_dir():
     return SRC_DIR.resolve()
 
+
+@pytest.fixture
+def cli_call():
+    def _call_nominatim(*args):
+        return nominatim.cli.nominatim(
+                   module_dir='MODULE NOT AVAILABLE',
+                   osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
+                   phplib_dir=str(SRC_DIR / 'lib-php'),
+                   data_dir=str(SRC_DIR / 'data'),
+                   phpcgi_path='/usr/bin/php-cgi',
+                   sqllib_dir=str(SRC_DIR / 'lib-sql'),
+                   config_dir=str(SRC_DIR / 'settings'),
+                   cli_args=args)
+
+    return _call_nominatim
+
+
 @pytest.fixture
 def tmp_phplib_dir():
     with tempfile.TemporaryDirectory() as phpdir:
@@ -273,7 +259,7 @@ def tokenizer_mock(monkeypatch, property_table, temp_db_conn, tmp_path):
     def _import_dummy(module, *args, **kwargs):
         return dummy_tokenizer
 
-    monkeypatch.setattr(importlib, "import_module", _import_dummy)
+    monkeypatch.setattr(nominatim.tokenizer.factory, "_import_tokenizer", _import_dummy)
     properties.set_property(temp_db_conn, 'tokenizer', 'dummy')
 
     def _create_tokenizer():