]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_exec_utils.py
Merge pull request #2284 from lonvia/cleanup-word-frequency-computation
[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 import tempfile
7
8 import pytest
9
10 import nominatim.tools.exec_utils as exec_utils
11
12 @pytest.fixture
13 def nominatim_env(tmp_phplib_dir, def_config):
14     class _NominatimEnv:
15         config = def_config
16         phplib_dir = tmp_phplib_dir
17         data_dir = Path('data')
18         project_dir = Path('.')
19         sqllib_dir = Path('lib-sql')
20         config_dir = Path('settings')
21         module_dir = 'module'
22         osm2pgsql_path = 'osm2pgsql'
23
24     return _NominatimEnv
25
26 @pytest.fixture
27 def test_script(nominatim_env):
28     def _create_file(code):
29         with (nominatim_env.phplib_dir / 'admin' / 't.php').open(mode='w') as fd:
30             fd.write('<?php\n')
31             fd.write(code + '\n')
32
33         return 't.php'
34
35     return _create_file
36
37 @pytest.fixture(params=[0, 1, 15, 255])
38 def return_code(request):
39     return request.param
40
41 ### run_legacy_script
42
43 def test_run_legacy_return_exit_code(nominatim_env, test_script, return_code):
44     fname = test_script('exit({});'.format(return_code))
45     assert return_code == exec_utils.run_legacy_script(fname,
46                                                        nominatim_env=nominatim_env)
47
48
49 def test_run_legacy_return_throw_on_fail(nominatim_env, test_script):
50     fname = test_script('exit(11);')
51     with pytest.raises(subprocess.CalledProcessError):
52         exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
53                                      throw_on_fail=True)
54
55
56 def test_run_legacy_return_dont_throw_on_success(nominatim_env, test_script):
57     fname = test_script('exit(0);')
58     assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
59                                              throw_on_fail=True)
60
61 def test_run_legacy_use_given_module_path(nominatim_env, test_script):
62     fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
63
64     assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
65
66
67 def test_run_legacy_do_not_overwrite_module_path(nominatim_env, test_script, monkeypatch):
68     monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
69     fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
70
71     assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
72
73 ### run_api_script
74
75 @pytest.fixture
76 def tmp_project_dir():
77     with tempfile.TemporaryDirectory() as tempd:
78         project_dir = Path(tempd)
79         webdir = project_dir / 'website'
80         webdir.mkdir()
81
82         with (webdir / 'test.php').open(mode='w') as fd:
83             fd.write("<?php\necho 'OK\n';")
84
85         yield project_dir
86
87 def test_run_api(tmp_project_dir):
88     assert 0 == exec_utils.run_api_script('test', tmp_project_dir)
89
90 def test_run_api_execution_error(tmp_project_dir):
91     assert 0 != exec_utils.run_api_script('badname', tmp_project_dir)
92
93 def test_run_api_with_extra_env(tmp_project_dir):
94     extra_env = dict(SCRIPT_FILENAME=str(tmp_project_dir / 'website' / 'test.php'))
95     assert 0 == exec_utils.run_api_script('badname', tmp_project_dir,
96                                           extra_env=extra_env)
97
98
99 ### run_osm2pgsql
100
101 def test_run_osm2pgsql(osm2pgsql_options):
102     osm2pgsql_options['append'] = False
103     osm2pgsql_options['import_file'] = 'foo.bar'
104     osm2pgsql_options['tablespaces']['osm_data'] = 'extra'
105     exec_utils.run_osm2pgsql(osm2pgsql_options)
106
107
108 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
109     osm2pgsql_options['append'] = True
110     osm2pgsql_options['import_file'] = 'foo.bar'
111     osm2pgsql_options['disable_jit'] = True
112     exec_utils.run_osm2pgsql(osm2pgsql_options)