]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_refresh_setup_website.py
port setup-website to python
[nominatim.git] / test / python / test_tools_refresh_setup_website.py
1 """
2 Tests for setting up the website scripts.
3 """
4 from pathlib import Path
5 import subprocess
6
7 import pytest
8
9 from nominatim.tools import refresh
10
11 @pytest.fixture
12 def envdir(tmpdir):
13     (tmpdir / 'php').mkdir()
14     (tmpdir / 'php' / 'website').mkdir()
15     return tmpdir
16
17
18 @pytest.fixture
19 def test_script(envdir):
20     def _create_file(code):
21         outfile = envdir / 'php' / 'website' / 'search.php'
22         outfile.write_text('<?php\n{}\n'.format(code), 'utf-8')
23
24     return _create_file
25
26
27 def run_website_script(envdir, config):
28     refresh.setup_website(envdir, envdir / 'php', config)
29
30     proc = subprocess.run(['/usr/bin/env', 'php', '-Cq',
31                            envdir / 'search.php'], check=False)
32
33     return proc.returncode
34
35
36 @pytest.mark.parametrize("setting,retval", (('yes', 10), ('no', 20)))
37 def test_setup_website_check_bool(def_config, monkeypatch, envdir, test_script,
38                                   setting, retval):
39     monkeypatch.setenv('NOMINATIM_CORS_NOACCESSCONTROL', setting)
40
41     test_script('exit(CONST_NoAccessControl ? 10 : 20);')
42
43     assert run_website_script(envdir, def_config) == retval
44
45
46 @pytest.mark.parametrize("setting", (0, 10, 99067))
47 def test_setup_website_check_int(def_config, monkeypatch, envdir, test_script, setting):
48     monkeypatch.setenv('NOMINATIM_LOOKUP_MAX_COUNT', str(setting))
49
50     test_script('exit(CONST_Places_Max_ID_count == {} ? 10 : 20);'.format(setting))
51
52     assert run_website_script(envdir, def_config) == 10
53
54
55 def test_setup_website_check_empty_str(def_config, monkeypatch, envdir, test_script):
56     monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', '')
57
58     test_script('exit(CONST_Default_Language === false ? 10 : 20);')
59
60     assert run_website_script(envdir, def_config) == 10
61
62
63 def test_setup_website_check_str(def_config, monkeypatch, envdir, test_script):
64     monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'ffde 2')
65
66     test_script('exit(CONST_Default_Language === "ffde 2" ? 10 : 20);')
67
68     assert run_website_script(envdir, def_config) == 10
69
70