]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/python/test_tools_exec_utils.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / test_tools_exec_utils.py
index 283f486a9367fe40ac59aad79a628886d9c8a877..25ccf163439dcd77053ed07c65d58e1b8c5090f5 100644 (file)
@@ -3,110 +3,107 @@ Tests for tools.exec_utils module.
 """
 from pathlib import Path
 import subprocess
-import tempfile
 
 import pytest
 
 import nominatim.tools.exec_utils as exec_utils
 
-@pytest.fixture
-def tmp_phplib_dir():
-    with tempfile.TemporaryDirectory() as phpdir:
-        (Path(phpdir) / 'admin').mkdir()
-
-        yield Path(phpdir)
-
-@pytest.fixture
-def nominatim_env(tmp_phplib_dir, def_config):
-    class _NominatimEnv:
-        config = def_config
-        phplib_dir = tmp_phplib_dir
-        data_dir = Path('data')
-        project_dir = Path('.')
-        sqllib_dir = Path('lib-sql')
-        config_dir = Path('settings')
-        module_dir = 'module'
-        osm2pgsql_path = 'osm2pgsql'
-
-    return _NominatimEnv
-
-@pytest.fixture
-def test_script(nominatim_env):
-    def _create_file(code):
-        with (nominatim_env.phplib_dir / 'admin' / 't.php').open(mode='w') as fd:
-            fd.write('<?php\n')
-            fd.write(code + '\n')
+class TestRunLegacyScript:
 
-        return 't.php'
+    @pytest.fixture(autouse=True)
+    def setup_nominatim_env(self, tmp_path, def_config):
+        tmp_phplib_dir = tmp_path / 'phplib'
+        tmp_phplib_dir.mkdir()
+        (tmp_phplib_dir / 'admin').mkdir()
 
-    return _create_file
+        class _NominatimEnv:
+            config = def_config
+            phplib_dir = tmp_phplib_dir
+            data_dir = Path('data')
+            project_dir = Path('.')
+            sqllib_dir = Path('lib-sql')
+            config_dir = Path('settings')
+            module_dir = 'module'
+            osm2pgsql_path = 'osm2pgsql'
 
-@pytest.fixture(params=[0, 1, 15, 255])
-def return_code(request):
-    return request.param
+        self.testenv = _NominatimEnv
 
-### run_legacy_script
 
-def test_run_legacy_return_exit_code(nominatim_env, test_script, return_code):
-    fname = test_script('exit({});'.format(return_code))
-    assert return_code == exec_utils.run_legacy_script(fname,
-                                                       nominatim_env=nominatim_env)
+    def mk_script(self, code):
+        codefile = self.testenv.phplib_dir / 'admin' / 't.php'
+        codefile.write_text('<?php\n' + code + '\n')
 
+        return 't.php'
 
-def test_run_legacy_return_throw_on_fail(nominatim_env, test_script):
-    fname = test_script('exit(11);')
-    with pytest.raises(subprocess.CalledProcessError):
-        exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
-                                     throw_on_fail=True)
 
+    @pytest.mark.parametrize("return_code", (0, 1, 15, 255))
+    def test_run_legacy_return_exit_code(self, return_code):
+        fname = self.mk_script('exit({});'.format(return_code))
+        assert return_code == \
+                 exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
 
-def test_run_legacy_return_dont_throw_on_success(nominatim_env, test_script):
-    fname = test_script('exit(0);')
-    assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
-                                             throw_on_fail=True)
 
-def test_run_legacy_use_given_module_path(nominatim_env, test_script):
-    fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
+    def test_run_legacy_return_throw_on_fail(self):
+        fname = self.mk_script('exit(11);')
+        with pytest.raises(subprocess.CalledProcessError):
+            exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
+                                         throw_on_fail=True)
 
-    assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
 
+    def test_run_legacy_return_dont_throw_on_success(self):
+        fname = self.mk_script('exit(0);')
+        assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
+                                            throw_on_fail=True) == 0
 
-def test_run_legacy_do_not_overwrite_module_path(nominatim_env, test_script, monkeypatch):
-    monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
-    fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
+    def test_run_legacy_use_given_module_path(self):
+        fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
 
-    assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
+        assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
 
-### run_api_script
 
-@pytest.fixture
-def tmp_project_dir():
-    with tempfile.TemporaryDirectory() as tempd:
-        project_dir = Path(tempd)
-        webdir = project_dir / 'website'
-        webdir.mkdir()
+    def test_run_legacy_do_not_overwrite_module_path(self, monkeypatch):
+        monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
+        fname = self.mk_script(
+            "exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
 
-        with (webdir / 'test.php').open(mode='w') as fd:
-            fd.write("<?php\necho 'OK\n';")
+        assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
 
-        yield project_dir
 
-def test_run_api(tmp_project_dir):
-    assert 0 == exec_utils.run_api_script('test', tmp_project_dir)
+class TestRunApiScript:
+
+    @staticmethod
+    @pytest.fixture(autouse=True)
+    def setup_project_dir(tmp_path):
+        webdir = tmp_path / 'website'
+        webdir.mkdir()
+        (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
 
-def test_run_api_execution_error(tmp_project_dir):
-    assert 0 != exec_utils.run_api_script('badname', tmp_project_dir)
 
-def test_run_api_with_extra_env(tmp_project_dir):
-    extra_env = dict(SCRIPT_FILENAME=str(tmp_project_dir / 'website' / 'test.php'))
-    assert 0 == exec_utils.run_api_script('badname', tmp_project_dir,
-                                          extra_env=extra_env)
+    @staticmethod
+    def test_run_api(tmp_path):
+        assert exec_utils.run_api_script('test', tmp_path) == 0
+
+    @staticmethod
+    def test_run_api_execution_error(tmp_path):
+        assert exec_utils.run_api_script('badname', tmp_path) != 0
+
+    @staticmethod
+    def test_run_api_with_extra_env(tmp_path):
+        extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
+        assert exec_utils.run_api_script('badname', tmp_path, extra_env=extra_env) == 0
 
 
 ### run_osm2pgsql
 
-def test_run_osm2pgsql():
-    exec_utils.run_osm2pgsql(dict(osm2pgsql='echo', append=False, flatnode_file=None,
-                                  dsn='dbname=foobar', threads=1, osm2pgsql_cache=500,
-                                  osm2pgsql_style='./my.style',
-                                  import_file='foo.bar'))
+def test_run_osm2pgsql(osm2pgsql_options):
+    osm2pgsql_options['append'] = False
+    osm2pgsql_options['import_file'] = 'foo.bar'
+    osm2pgsql_options['tablespaces']['osm_data'] = 'extra'
+    exec_utils.run_osm2pgsql(osm2pgsql_options)
+
+
+def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
+    osm2pgsql_options['append'] = True
+    osm2pgsql_options['import_file'] = 'foo.bar'
+    osm2pgsql_options['disable_jit'] = True
+    exec_utils.run_osm2pgsql(osm2pgsql_options)