]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_exec_utils.py
move SearchDescription building into tokens
[nominatim.git] / test / python / test_tools_exec_utils.py
1 """
2 Tests for tools.exec_utils module.
3 """
4 from pathlib import Path
5 import subprocess
6
7 import pytest
8
9 import nominatim.tools.exec_utils as exec_utils
10
11 class TestRunLegacyScript:
12
13     @pytest.fixture(autouse=True)
14     def setup_nominatim_env(self, tmp_path, def_config):
15         tmp_phplib_dir = tmp_path / 'phplib'
16         tmp_phplib_dir.mkdir()
17         (tmp_phplib_dir / 'admin').mkdir()
18
19         class _NominatimEnv:
20             config = def_config
21             phplib_dir = tmp_phplib_dir
22             data_dir = Path('data')
23             project_dir = Path('.')
24             sqllib_dir = Path('lib-sql')
25             config_dir = Path('settings')
26             module_dir = 'module'
27             osm2pgsql_path = 'osm2pgsql'
28
29         self.testenv = _NominatimEnv
30
31
32     def mk_script(self, code):
33         codefile = self.testenv.phplib_dir / 'admin' / 't.php'
34         codefile.write_text('<?php\n' + code + '\n')
35
36         return 't.php'
37
38
39     @pytest.mark.parametrize("return_code", (0, 1, 15, 255))
40     def test_run_legacy_return_exit_code(self, return_code):
41         fname = self.mk_script('exit({});'.format(return_code))
42         assert return_code == \
43                  exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
44
45
46     def test_run_legacy_return_throw_on_fail(self):
47         fname = self.mk_script('exit(11);')
48         with pytest.raises(subprocess.CalledProcessError):
49             exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
50                                          throw_on_fail=True)
51
52
53     def test_run_legacy_return_dont_throw_on_success(self):
54         fname = self.mk_script('exit(0);')
55         assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
56                                             throw_on_fail=True) == 0
57
58     def test_run_legacy_use_given_module_path(self):
59         fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
60
61         assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
62
63
64     def test_run_legacy_do_not_overwrite_module_path(self, monkeypatch):
65         monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
66         fname = self.mk_script(
67             "exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
68
69         assert exec_utils.run_legacy_script(fname, nominatim_env=self.testenv) == 0
70
71
72 class TestRunApiScript:
73
74     @staticmethod
75     @pytest.fixture(autouse=True)
76     def setup_project_dir(tmp_path):
77         webdir = tmp_path / 'website'
78         webdir.mkdir()
79         (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
80
81
82     @staticmethod
83     def test_run_api(tmp_path):
84         assert exec_utils.run_api_script('test', tmp_path) == 0
85
86     @staticmethod
87     def test_run_api_execution_error(tmp_path):
88         assert exec_utils.run_api_script('badname', tmp_path) != 0
89
90     @staticmethod
91     def test_run_api_with_extra_env(tmp_path):
92         extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
93         assert exec_utils.run_api_script('badname', tmp_path, extra_env=extra_env) == 0
94
95
96 ### run_osm2pgsql
97
98 def test_run_osm2pgsql(osm2pgsql_options):
99     osm2pgsql_options['append'] = False
100     osm2pgsql_options['import_file'] = 'foo.bar'
101     osm2pgsql_options['tablespaces']['osm_data'] = 'extra'
102     exec_utils.run_osm2pgsql(osm2pgsql_options)
103
104
105 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
106     osm2pgsql_options['append'] = True
107     osm2pgsql_options['import_file'] = 'foo.bar'
108     osm2pgsql_options['disable_jit'] = True
109     exec_utils.run_osm2pgsql(osm2pgsql_options)