]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_config.py
de7b93016658a177a77869109fe6ad2a5aae73de
[nominatim.git] / test / python / test_config.py
1 """
2 Test for loading dotenv configuration.
3 """
4 import pytest
5
6 from nominatim.config import Configuration
7 from nominatim.errors import UsageError
8
9 @pytest.fixture
10 def make_config(src_dir):
11     """ Create a configuration object from the given project directory.
12     """
13     def _mk_config(project_dir=None):
14         return Configuration(project_dir, src_dir / 'settings')
15
16     return _mk_config
17
18 @pytest.fixture
19 def make_config_path(src_dir, tmp_path):
20     """ Create a configuration object with project and config directories
21         in a temporary directory.
22     """
23     def _mk_config():
24         (tmp_path / 'project').mkdir()
25         (tmp_path / 'config').mkdir()
26         conf = Configuration(tmp_path / 'project', src_dir / 'settings')
27         conf.config_dir = tmp_path / 'config'
28         return conf
29
30     return _mk_config
31
32
33 def test_no_project_dir(make_config):
34     config = make_config()
35
36     assert config.DATABASE_WEBUSER == 'www-data'
37
38
39 @pytest.mark.parametrize("val", ('apache', '"apache"'))
40 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
41     envfile = tmp_path / '.env'
42     envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
43
44     config = make_config(tmp_path)
45
46     assert config.DATABASE_WEBUSER == 'apache'
47
48
49 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
50     envfile = tmp_path / '.env'
51     envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
52
53     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
54
55     config = make_config(tmp_path)
56
57     assert config.DATABASE_WEBUSER == 'nobody'
58
59
60 def test_prefer_os_environ_can_unset_project_setting(make_config, monkeypatch, tmp_path):
61     envfile = tmp_path / '.env'
62     envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
63
64     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', '')
65
66     config = make_config(tmp_path)
67
68     assert config.DATABASE_WEBUSER == ''
69
70
71 def test_get_os_env_add_defaults(make_config, monkeypatch):
72     config = make_config()
73
74     monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
75
76     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
77
78
79 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
80     config = make_config()
81
82     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
83
84     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
85
86
87 def test_get_libpq_dsn_convert_default(make_config):
88     config = make_config()
89
90     assert config.get_libpq_dsn() == 'dbname=nominatim'
91
92
93 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
94     config = make_config()
95
96     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
97                        'pgsql:dbname=gis;password=foo;host=localhost')
98
99     assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
100
101
102 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
103                                         ("xy'z", "xy\\'z"),
104                                        ])
105 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
106     config = make_config()
107
108     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
109                        'pgsql:dbname=gis;password={}'.format(val))
110
111     assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
112
113
114 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
115     config = make_config()
116
117     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
118                        'host=localhost dbname=gis password=foo')
119
120     assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
121
122
123 @pytest.mark.parametrize("value,result",
124                          [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
125                          [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
126 def test_get_bool(make_config, monkeypatch, value, result):
127     config = make_config()
128
129     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
130
131     assert config.get_bool('FOOBAR') == result
132
133 def test_get_bool_empty(make_config):
134     config = make_config()
135
136     assert config.DATABASE_MODULE_PATH == ''
137     assert not config.get_bool('DATABASE_MODULE_PATH')
138
139
140 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
141                                           ('85762513444', 85762513444)])
142 def test_get_int_success(make_config, monkeypatch, value, result):
143     config = make_config()
144
145     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
146
147     assert config.get_int('FOOBAR') == result
148
149
150 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
151 def test_get_int_bad_values(make_config, monkeypatch, value):
152     config = make_config()
153
154     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
155
156     with pytest.raises(UsageError):
157         config.get_int('FOOBAR')
158
159
160 def test_get_int_empty(make_config):
161     config = make_config()
162
163     assert config.DATABASE_MODULE_PATH == ''
164
165     with pytest.raises(UsageError):
166         config.get_int('DATABASE_MODULE_PATH')
167
168
169 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
170     config = make_config()
171
172     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
173
174     expected = src_dir / 'settings' / 'import-street.style'
175
176     assert config.get_import_style_file() == expected
177
178
179 def test_get_import_style_extern_relative(make_config_path, monkeypatch):
180     config = make_config_path()
181     (config.project_dir / 'custom.style').write_text('x')
182
183     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'custom.style')
184
185     assert str(config.get_import_style_file()) == str(config.project_dir / 'custom.style')
186
187
188 def test_get_import_style_extern_absolute(make_config, tmp_path, monkeypatch):
189     config = make_config()
190     cfgfile = tmp_path / 'test.style'
191
192     cfgfile.write_text('x')
193
194     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', str(cfgfile))
195
196     assert str(config.get_import_style_file()) == str(cfgfile)
197
198
199 def test_load_subconf_from_project_dir(make_config_path):
200     config = make_config_path()
201
202     testfile = config.project_dir / 'test.yaml'
203     testfile.write_text('cow: muh\ncat: miau\n')
204
205     testfile = config.config_dir / 'test.yaml'
206     testfile.write_text('cow: miau\ncat: muh\n')
207
208     rules = config.load_sub_configuration('test.yaml')
209
210     assert rules == dict(cow='muh', cat='miau')
211
212
213 def test_load_subconf_from_settings_dir(make_config_path):
214     config = make_config_path()
215
216     testfile = config.config_dir / 'test.yaml'
217     testfile.write_text('cow: muh\ncat: miau\n')
218
219     rules = config.load_sub_configuration('test.yaml')
220
221     assert rules == dict(cow='muh', cat='miau')
222
223
224 def test_load_subconf_empty_env_conf(make_config_path, monkeypatch):
225     monkeypatch.setenv('NOMINATIM_MY_CONFIG', '')
226     config = make_config_path()
227
228     testfile = config.config_dir / 'test.yaml'
229     testfile.write_text('cow: muh\ncat: miau\n')
230
231     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
232
233     assert rules == dict(cow='muh', cat='miau')
234
235
236 def test_load_subconf_env_absolute_found(make_config_path, monkeypatch, tmp_path):
237     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
238     config = make_config_path()
239
240     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
241     (tmp_path / 'other.yaml').write_text('dog: muh\nfrog: miau\n')
242
243     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
244
245     assert rules == dict(dog='muh', frog='miau')
246
247
248 def test_load_subconf_env_absolute_not_found(make_config_path, monkeypatch, tmp_path):
249     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
250     config = make_config_path()
251
252     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
253
254     with pytest.raises(UsageError, match='Config file not found.'):
255         rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
256
257
258 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
259 def test_load_subconf_env_relative_found(make_config_path, monkeypatch, location):
260     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
261     config = make_config_path()
262
263     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
264     (getattr(config, location) / 'other.yaml').write_text('dog: bark\n')
265
266     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
267
268     assert rules == dict(dog='bark')
269
270
271 def test_load_subconf_env_relative_not_found(make_config_path, monkeypatch):
272     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
273     config = make_config_path()
274
275     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
276
277     with pytest.raises(UsageError, match='Config file not found.'):
278         rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
279
280
281 def test_load_subconf_not_found(make_config_path):
282     config = make_config_path()
283
284     with pytest.raises(UsageError, match='Config file not found.'):
285         rules = config.load_sub_configuration('test.yaml')
286
287
288 def test_load_subconf_include_absolute(make_config_path, tmp_path):
289     config = make_config_path()
290
291     testfile = config.config_dir / 'test.yaml'
292     testfile.write_text(f'base: !include {tmp_path}/inc.yaml\n')
293     (tmp_path / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
294
295     rules = config.load_sub_configuration('test.yaml')
296
297     assert rules == dict(base=dict(first=1, second=2))
298
299
300 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
301 def test_load_subconf_include_relative(make_config_path, tmp_path, location):
302     config = make_config_path()
303
304     testfile = config.config_dir / 'test.yaml'
305     testfile.write_text(f'base: !include inc.yaml\n')
306     (getattr(config, location) / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
307
308     rules = config.load_sub_configuration('test.yaml')
309
310     assert rules == dict(base=dict(first=1, second=2))
311
312
313 def test_load_subconf_include_bad_format(make_config_path):
314     config = make_config_path()
315
316     testfile = config.config_dir / 'test.yaml'
317     testfile.write_text(f'base: !include inc.txt\n')
318     (config.config_dir / 'inc.txt').write_text('first: 1\nsecond: 2\n')
319
320     with pytest.raises(UsageError, match='Cannot handle config file format.'):
321         rules = config.load_sub_configuration('test.yaml')
322
323
324 def test_load_subconf_include_not_found(make_config_path):
325     config = make_config_path()
326
327     testfile = config.config_dir / 'test.yaml'
328     testfile.write_text(f'base: !include inc.txt\n')
329
330     with pytest.raises(UsageError, match='Config file not found.'):
331         rules = config.load_sub_configuration('test.yaml')
332
333
334 def test_load_subconf_include_recursive(make_config_path):
335     config = make_config_path()
336
337     testfile = config.config_dir / 'test.yaml'
338     testfile.write_text(f'base: !include inc.yaml\n')
339     (config.config_dir / 'inc.yaml').write_text('- !include more.yaml\n- upper\n')
340     (config.config_dir / 'more.yaml').write_text('- the end\n')
341
342     rules = config.load_sub_configuration('test.yaml')
343
344     assert rules == dict(base=[['the end'], 'upper'])