]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_setup_website.py
ICU: better letter identification in normalization
[nominatim.git] / test / python / tools / test_refresh_setup_website.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for setting up the website scripts.
9 """
10 import subprocess
11
12 import pytest
13
14 from nominatim.tools import refresh
15
16 @pytest.fixture
17 def test_script(tmp_path):
18     (tmp_path / 'php').mkdir()
19
20     website_dir = (tmp_path / 'php' / 'website')
21     website_dir.mkdir()
22
23     def _create_file(code):
24         outfile = website_dir / 'reverse-only-search.php'
25         outfile.write_text('<?php\n{}\n'.format(code), 'utf-8')
26
27     return _create_file
28
29
30 @pytest.fixture
31 def run_website_script(tmp_path, project_env, temp_db_conn):
32     project_env.lib_dir.php = tmp_path / 'php'
33
34     def _runner():
35         refresh.setup_website(tmp_path, project_env, temp_db_conn)
36
37         proc = subprocess.run(['/usr/bin/env', 'php', '-Cq',
38                                tmp_path / 'search.php'], check=False)
39
40         return proc.returncode
41
42     return _runner
43
44
45 def test_basedir_created(tmp_path, project_env, temp_db_conn):
46     webdir = tmp_path / 'website'
47
48     assert not webdir.exists()
49
50     refresh.setup_website(webdir, project_env, temp_db_conn)
51
52     assert webdir.exists()
53
54
55 @pytest.mark.parametrize("setting,retval", (('yes', 10), ('no', 20)))
56 def test_setup_website_check_bool(monkeypatch, test_script, run_website_script,
57                                   setting, retval):
58     monkeypatch.setenv('NOMINATIM_CORS_NOACCESSCONTROL', setting)
59
60     test_script('exit(CONST_NoAccessControl ? 10 : 20);')
61
62     assert run_website_script() == retval
63
64
65 @pytest.mark.parametrize("setting", (0, 10, 99067))
66 def test_setup_website_check_int(monkeypatch, test_script, run_website_script, setting):
67     monkeypatch.setenv('NOMINATIM_LOOKUP_MAX_COUNT', str(setting))
68
69     test_script('exit(CONST_Places_Max_ID_count == {} ? 10 : 20);'.format(setting))
70
71     assert run_website_script() == 10
72
73
74 def test_setup_website_check_empty_str(monkeypatch, test_script, run_website_script):
75     monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', '')
76
77     test_script('exit(CONST_Default_Language === false ? 10 : 20);')
78
79     assert run_website_script() == 10
80
81
82 def test_setup_website_check_str(monkeypatch, test_script, run_website_script):
83     monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'ffde 2')
84
85     test_script('exit(CONST_Default_Language === "ffde 2" ? 10 : 20);')
86
87     assert run_website_script() == 10
88
89
90 def test_relative_log_file(project_env, monkeypatch, test_script, run_website_script):
91     monkeypatch.setenv('NOMINATIM_LOG_FILE', 'access.log')
92
93     expected_file = str(project_env.project_dir / 'access.log')
94     test_script(f'exit(CONST_Log_File === "{expected_file}" ? 10 : 20);')
95
96     assert run_website_script() == 10
97