]> git.openstreetmap.org Git - nominatim.git/blob - test/python/config/test_config.py
Merge pull request #3991 from lonvia/interpolation-on-addresses
[nominatim.git] / test / python / config / test_config.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Test for loading dotenv configuration.
9 """
10 from pathlib import Path
11 import pytest
12
13 from nominatim_db.config import Configuration, flatten_config_list
14 from nominatim_db.errors import UsageError
15
16
17 @pytest.fixture
18 def make_config():
19     """ Create a configuration object from the given project directory.
20     """
21     def _mk_config(project_dir=None):
22         return Configuration(project_dir)
23
24     return _mk_config
25
26
27 @pytest.fixture
28 def make_config_path(tmp_path):
29     """ Create a configuration object with project and config directories
30         in a temporary directory.
31     """
32     def _mk_config():
33         (tmp_path / 'project').mkdir()
34         (tmp_path / 'config').mkdir()
35         conf = Configuration(tmp_path / 'project')
36         conf.config_dir = tmp_path / 'config'
37         return conf
38
39     return _mk_config
40
41
42 def test_no_project_dir(make_config):
43     config = make_config()
44
45     assert config.DATABASE_WEBUSER == 'www-data'
46
47
48 @pytest.mark.parametrize("val", ('apache', '"apache"'))
49 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
50     envfile = tmp_path / '.env'
51     envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val), encoding='utf-8')
52
53     config = make_config(tmp_path)
54
55     assert config.DATABASE_WEBUSER == 'apache'
56
57
58 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
59     envfile = tmp_path / '.env'
60     envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n', encoding='utf-8')
61
62     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
63
64     config = make_config(tmp_path)
65
66     assert config.DATABASE_WEBUSER == 'nobody'
67
68
69 def test_prefer_os_environ_can_unset_project_setting(make_config, monkeypatch, tmp_path):
70     envfile = tmp_path / '.env'
71     envfile.write_text('NOMINATIM_OSM2PGSQL_BINARY=osm2pgsql\n', encoding='utf-8')
72
73     monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '')
74
75     config = make_config(tmp_path)
76
77     assert config.OSM2PGSQL_BINARY == ''
78
79
80 def test_get_os_env_add_defaults(make_config, monkeypatch):
81     config = make_config()
82
83     monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
84
85     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
86
87
88 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
89     config = make_config()
90
91     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
92
93     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
94
95
96 def test_get_libpq_dsn_convert_default(make_config):
97     config = make_config()
98
99     assert config.get_libpq_dsn() == 'dbname=nominatim'
100
101
102 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
103     config = make_config()
104
105     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
106                        'pgsql:dbname=gis;password=foo;host=localhost')
107
108     assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
109
110
111 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
112                                         ("xy'z", "xy\\'z"),
113                                         ])
114 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
115     config = make_config()
116
117     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
118                        'pgsql:dbname=gis;password={}'.format(val))
119
120     assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
121
122
123 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
124     config = make_config()
125
126     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
127                        'host=localhost dbname=gis password=foo')
128
129     assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
130
131
132 @pytest.mark.parametrize("value,result",
133                          [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
134                          [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
135 def test_get_bool(make_config, monkeypatch, value, result):
136     config = make_config()
137
138     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
139
140     assert config.get_bool('FOOBAR') == result
141
142
143 def test_get_bool_empty(make_config):
144     config = make_config()
145
146     assert config.TOKENIZER_CONFIG == ''
147     assert not config.get_bool('TOKENIZER_CONFIG')
148
149
150 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
151                                           ('85762513444', 85762513444)])
152 def test_get_int_success(make_config, monkeypatch, value, result):
153     config = make_config()
154
155     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
156
157     assert config.get_int('FOOBAR') == result
158
159
160 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
161 def test_get_int_bad_values(make_config, monkeypatch, value):
162     config = make_config()
163
164     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
165
166     with pytest.raises(UsageError):
167         config.get_int('FOOBAR')
168
169
170 def test_get_int_empty(make_config):
171     config = make_config()
172
173     assert config.TOKENIZER_CONFIG == ''
174
175     with pytest.raises(UsageError):
176         config.get_int('TOKENIZER_CONFIG')
177
178
179 @pytest.mark.parametrize("value,outlist", [('sd', ['sd']),
180                                            ('dd,rr', ['dd', 'rr']),
181                                            (' a , b ', ['a', 'b'])])
182 def test_get_str_list_success(make_config, monkeypatch, value, outlist):
183     config = make_config()
184
185     monkeypatch.setenv('NOMINATIM_MYLIST', value)
186
187     assert config.get_str_list('MYLIST') == outlist
188
189
190 def test_get_str_list_empty(make_config):
191     config = make_config()
192
193     assert config.get_str_list('LANGUAGES') is None
194
195
196 def test_get_path_empty(make_config):
197     config = make_config()
198
199     assert config.TOKENIZER_CONFIG == ''
200     assert not config.get_path('TOKENIZER_CONFIG')
201
202
203 def test_get_path_absolute(make_config, monkeypatch, tmp_path):
204     config = make_config()
205
206     p = (tmp_path / "does_not_exist").resolve()
207     monkeypatch.setenv('NOMINATIM_FOOBAR', str(p))
208     result = config.get_path('FOOBAR')
209
210     assert isinstance(result, Path)
211     assert str(result) == str(p)
212
213
214 def test_get_path_relative(make_config, monkeypatch, tmp_path):
215     config = make_config(tmp_path)
216
217     monkeypatch.setenv('NOMINATIM_FOOBAR', 'an/oyster')
218     result = config.get_path('FOOBAR')
219
220     assert isinstance(result, Path)
221     assert str(result) == str(tmp_path / 'an/oyster')
222
223
224 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
225     config = make_config()
226
227     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
228
229     expected = src_dir / 'lib-lua' / 'import-street.lua'
230
231     assert config.get_import_style_file() == expected
232
233
234 def test_get_import_style_extern_relative(make_config_path, monkeypatch):
235     config = make_config_path()
236     (config.project_dir / 'custom.style').write_text('x', encoding='utf-8')
237
238     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'custom.style')
239
240     assert str(config.get_import_style_file()) == str(config.project_dir / 'custom.style')
241
242
243 def test_get_import_style_extern_absolute(make_config, tmp_path, monkeypatch):
244     config = make_config()
245     cfgfile = tmp_path / 'test.style'
246
247     cfgfile.write_text('x', encoding='utf-8')
248
249     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', str(cfgfile))
250
251     assert str(config.get_import_style_file()) == str(cfgfile)
252
253
254 def test_load_subconf_from_project_dir(make_config_path):
255     config = make_config_path()
256
257     testfile = config.project_dir / 'test.yaml'
258     testfile.write_text('cow: muh\ncat: miau\n', encoding='utf-8')
259
260     testfile = config.config_dir / 'test.yaml'
261     testfile.write_text('cow: miau\ncat: muh\n', encoding='utf-8')
262
263     rules = config.load_sub_configuration('test.yaml')
264
265     assert rules == dict(cow='muh', cat='miau')
266
267
268 def test_load_subconf_from_settings_dir(make_config_path):
269     config = make_config_path()
270
271     testfile = config.config_dir / 'test.yaml'
272     testfile.write_text('cow: muh\ncat: miau\n', encoding='utf-8')
273
274     rules = config.load_sub_configuration('test.yaml')
275
276     assert rules == dict(cow='muh', cat='miau')
277
278
279 def test_load_subconf_empty_env_conf(make_config_path, monkeypatch):
280     monkeypatch.setenv('NOMINATIM_MY_CONFIG', '')
281     config = make_config_path()
282
283     testfile = config.config_dir / 'test.yaml'
284     testfile.write_text('cow: muh\ncat: miau\n', encoding='utf-8')
285
286     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
287
288     assert rules == dict(cow='muh', cat='miau')
289
290
291 def test_load_subconf_env_absolute_found(make_config_path, monkeypatch, tmp_path):
292     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
293     config = make_config_path()
294
295     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
296     (tmp_path / 'other.yaml').write_text('dog: muh\nfrog: miau\n', encoding='utf-8')
297
298     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
299
300     assert rules == dict(dog='muh', frog='miau')
301
302
303 def test_load_subconf_env_absolute_not_found(make_config_path, monkeypatch, tmp_path):
304     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
305     config = make_config_path()
306
307     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
308
309     with pytest.raises(UsageError, match='Config file not found.'):
310         config.load_sub_configuration('test.yaml', config='MY_CONFIG')
311
312
313 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
314 def test_load_subconf_env_relative_found(make_config_path, monkeypatch, location):
315     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
316     config = make_config_path()
317
318     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
319     (getattr(config, location) / 'other.yaml').write_text('dog: bark\n', encoding='utf-8')
320
321     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
322
323     assert rules == dict(dog='bark')
324
325
326 def test_load_subconf_env_relative_not_found(make_config_path, monkeypatch):
327     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
328     config = make_config_path()
329
330     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
331
332     with pytest.raises(UsageError, match='Config file not found.'):
333         config.load_sub_configuration('test.yaml', config='MY_CONFIG')
334
335
336 def test_load_subconf_json(make_config_path):
337     config = make_config_path()
338
339     (config.project_dir / 'test.json').write_text('{"cow": "muh", "cat": "miau"}', encoding='utf-8')
340
341     rules = config.load_sub_configuration('test.json')
342
343     assert rules == dict(cow='muh', cat='miau')
344
345
346 def test_load_subconf_not_found(make_config_path):
347     config = make_config_path()
348
349     with pytest.raises(UsageError, match='Config file not found.'):
350         config.load_sub_configuration('test.yaml')
351
352
353 def test_load_subconf_env_unknown_format(make_config_path):
354     config = make_config_path()
355
356     (config.project_dir / 'test.xml').write_text('<html></html>', encoding='utf-8')
357
358     with pytest.raises(UsageError, match='unknown format'):
359         config.load_sub_configuration('test.xml')
360
361
362 def test_load_subconf_include_absolute(make_config_path, tmp_path):
363     config = make_config_path()
364
365     testfile = config.config_dir / 'test.yaml'
366     testfile.write_text(f'base: !include {tmp_path}/inc.yaml\n', encoding='utf-8')
367     (tmp_path / 'inc.yaml').write_text('first: 1\nsecond: 2\n', encoding='utf-8')
368
369     rules = config.load_sub_configuration('test.yaml')
370
371     assert rules == dict(base=dict(first=1, second=2))
372
373
374 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
375 def test_load_subconf_include_relative(make_config_path, tmp_path, location):
376     config = make_config_path()
377
378     testfile = config.config_dir / 'test.yaml'
379     testfile.write_text('base: !include inc.yaml\n', encoding='utf-8')
380     (getattr(config, location) / 'inc.yaml').write_text('first: 1\nsecond: 2\n', encoding='utf-8')
381
382     rules = config.load_sub_configuration('test.yaml')
383
384     assert rules == dict(base=dict(first=1, second=2))
385
386
387 def test_load_subconf_include_bad_format(make_config_path):
388     config = make_config_path()
389
390     testfile = config.config_dir / 'test.yaml'
391     testfile.write_text('base: !include inc.txt\n', encoding='utf-8')
392     (config.config_dir / 'inc.txt').write_text('first: 1\nsecond: 2\n', encoding='utf-8')
393
394     with pytest.raises(UsageError, match='Cannot handle config file format.'):
395         config.load_sub_configuration('test.yaml')
396
397
398 def test_load_subconf_include_not_found(make_config_path):
399     config = make_config_path()
400
401     testfile = config.config_dir / 'test.yaml'
402     testfile.write_text('base: !include inc.txt\n', encoding='utf-8')
403
404     with pytest.raises(UsageError, match='Config file not found.'):
405         config.load_sub_configuration('test.yaml')
406
407
408 def test_load_subconf_include_recursive(make_config_path):
409     config = make_config_path()
410
411     testfile = config.config_dir / 'test.yaml'
412     testfile.write_text('base: !include inc.yaml\n', encoding='utf-8')
413     (config.config_dir / 'inc.yaml').write_text('- !include more.yaml\n- upper\n', encoding='utf-8')
414     (config.config_dir / 'more.yaml').write_text('- the end\n', encoding='utf-8')
415
416     rules = config.load_sub_configuration('test.yaml')
417
418     assert rules == dict(base=[['the end'], 'upper'])
419
420
421 @pytest.mark.parametrize("content", [[], None])
422 def test_flatten_config_list_empty(content):
423     assert flatten_config_list(content) == []
424
425
426 @pytest.mark.parametrize("content", [{'foo': 'bar'}, 'hello world', 3])
427 def test_flatten_config_list_no_list(content):
428     with pytest.raises(UsageError):
429         flatten_config_list(content)
430
431
432 def test_flatten_config_list_allready_flat():
433     assert flatten_config_list([1, 2, 456]) == [1, 2, 456]
434
435
436 def test_flatten_config_list_nested():
437     content = [
438         34,
439         [{'first': '1st', 'second': '2nd'}, {}],
440         [[2, 3], [45, [56, 78], 66]],
441         'end'
442     ]
443
444     assert flatten_config_list(content) == \
445         [34, {'first': '1st', 'second': '2nd'}, {}, 2, 3, 45, 56, 78, 66, 'end']