]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_config.py
Added wrapper in specialphrases.php to call corresponding nominatim command.
[nominatim.git] / test / python / test_config.py
1 """
2 Test for loading dotenv configuration.
3 """
4 from pathlib import Path
5
6 import pytest
7
8 from nominatim.config import Configuration
9 from nominatim.errors import UsageError
10
11 DEFCFG_DIR = Path(__file__) / '..' / '..' / '..' / 'settings'
12
13 def test_no_project_dir():
14     config = Configuration(None, DEFCFG_DIR)
15
16     assert config.DATABASE_WEBUSER == 'www-data'
17
18
19 @pytest.mark.parametrize("val", ('apache', '"apache"'))
20 def test_prefer_project_setting_over_default(val, tmp_path):
21     envfile = tmp_path / '.env'
22     envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
23
24     config = Configuration(Path(tmp_path), DEFCFG_DIR)
25
26     assert config.DATABASE_WEBUSER == 'apache'
27
28
29 def test_prefer_os_environ_over_project_setting(monkeypatch, tmp_path):
30     envfile = tmp_path / '.env'
31     envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
32
33     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
34
35     config = Configuration(Path(tmp_path), DEFCFG_DIR)
36
37     assert config.DATABASE_WEBUSER == 'nobody'
38
39
40 def test_get_os_env_add_defaults(monkeypatch):
41     config = Configuration(None, DEFCFG_DIR)
42
43     monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
44
45     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
46
47
48 def test_get_os_env_prefer_os_environ(monkeypatch):
49     config = Configuration(None, DEFCFG_DIR)
50
51     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
52
53     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
54
55
56 def test_get_libpq_dsn_convert_default():
57     config = Configuration(None, DEFCFG_DIR)
58
59     assert config.get_libpq_dsn() == 'dbname=nominatim'
60
61
62 def test_get_libpq_dsn_convert_php(monkeypatch):
63     config = Configuration(None, DEFCFG_DIR)
64
65     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
66                        'pgsql:dbname=gis;password=foo;host=localhost')
67
68     assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
69
70
71 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
72                                         ("xy'z", "xy\\'z"),
73                                        ])
74 def test_get_libpq_dsn_convert_php_special_chars(monkeypatch, val, expect):
75     config = Configuration(None, DEFCFG_DIR)
76
77     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
78                        'pgsql:dbname=gis;password={}'.format(val))
79
80     assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
81
82
83 def test_get_libpq_dsn_convert_libpq(monkeypatch):
84     config = Configuration(None, DEFCFG_DIR)
85
86     monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 
87                        'host=localhost dbname=gis password=foo')
88
89     assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
90
91
92 @pytest.mark.parametrize("value,result",
93                          [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
94                          [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
95 def test_get_bool(monkeypatch, value, result):
96     config = Configuration(None, DEFCFG_DIR)
97
98     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
99
100     assert config.get_bool('FOOBAR') == result
101
102 def test_get_bool_empty():
103     config = Configuration(None, DEFCFG_DIR)
104
105     assert config.DATABASE_MODULE_PATH == ''
106     assert config.get_bool('DATABASE_MODULE_PATH') == False
107
108
109 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
110                                           ('85762513444', 85762513444)])
111 def test_get_int_success(monkeypatch, value, result):
112     config = Configuration(None, DEFCFG_DIR)
113
114     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
115
116     assert config.get_int('FOOBAR') == result
117
118
119 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
120 def test_get_int_bad_values(monkeypatch, value):
121     config = Configuration(None, DEFCFG_DIR)
122
123     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
124
125     with pytest.raises(UsageError):
126         config.get_int('FOOBAR')
127
128
129 def test_get_int_empty():
130     config = Configuration(None, DEFCFG_DIR)
131
132     assert config.DATABASE_MODULE_PATH == ''
133
134     with pytest.raises(UsageError):
135         config.get_int('DATABASE_MODULE_PATH')
136
137
138 def test_get_import_style_intern(monkeypatch):
139     config = Configuration(None, DEFCFG_DIR)
140
141     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
142
143     expected = DEFCFG_DIR / 'import-street.style'
144
145     assert config.get_import_style_file() == expected
146
147
148 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
149 def test_get_import_style_intern(monkeypatch, value):
150     config = Configuration(None, DEFCFG_DIR)
151
152     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
153
154     assert str(config.get_import_style_file()) == value