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