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