]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_config.py
have ADDRESS_LEVEL_CONFIG use load_sub_configuration
[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 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
180 def test_get_import_style_extern(make_config, monkeypatch, value):
181     config = make_config()
182
183     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
184
185     assert str(config.get_import_style_file()) == value
186
187
188 def test_load_subconf_from_project_dir(make_config_path):
189     config = make_config_path()
190
191     testfile = config.project_dir / 'test.yaml'
192     testfile.write_text('cow: muh\ncat: miau\n')
193
194     testfile = config.config_dir / 'test.yaml'
195     testfile.write_text('cow: miau\ncat: muh\n')
196
197     rules = config.load_sub_configuration('test.yaml')
198
199     assert rules == dict(cow='muh', cat='miau')
200
201
202 def test_load_subconf_from_settings_dir(make_config_path):
203     config = make_config_path()
204
205     testfile = config.config_dir / 'test.yaml'
206     testfile.write_text('cow: muh\ncat: miau\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_empty_env_conf(make_config_path, monkeypatch):
214     monkeypatch.setenv('NOMINATIM_MY_CONFIG', '')
215     config = make_config_path()
216
217     testfile = config.config_dir / 'test.yaml'
218     testfile.write_text('cow: muh\ncat: miau\n')
219
220     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
221
222     assert rules == dict(cow='muh', cat='miau')
223
224
225 def test_load_subconf_env_absolute_found(make_config_path, monkeypatch, tmp_path):
226     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
227     config = make_config_path()
228
229     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
230     (tmp_path / 'other.yaml').write_text('dog: muh\nfrog: miau\n')
231
232     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
233
234     assert rules == dict(dog='muh', frog='miau')
235
236
237 def test_load_subconf_env_absolute_not_found(make_config_path, monkeypatch, tmp_path):
238     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
239     config = make_config_path()
240
241     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
242
243     with pytest.raises(UsageError, match='Config file not found.'):
244         rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
245
246
247 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
248 def test_load_subconf_env_relative_found(make_config_path, monkeypatch, location):
249     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
250     config = make_config_path()
251
252     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
253     (getattr(config, location) / 'other.yaml').write_text('dog: bark\n')
254
255     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
256
257     assert rules == dict(dog='bark')
258
259
260 def test_load_subconf_env_relative_not_found(make_config_path, monkeypatch):
261     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
262     config = make_config_path()
263
264     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
265
266     with pytest.raises(UsageError, match='Config file not found.'):
267         rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
268
269
270 def test_load_subconf_not_found(make_config_path):
271     config = make_config_path()
272
273     with pytest.raises(UsageError, match='Config file not found.'):
274         rules = config.load_sub_configuration('test.yaml')
275
276
277 def test_load_subconf_include_absolute(make_config_path, tmp_path):
278     config = make_config_path()
279
280     testfile = config.config_dir / 'test.yaml'
281     testfile.write_text(f'base: !include {tmp_path}/inc.yaml\n')
282     (tmp_path / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
283
284     rules = config.load_sub_configuration('test.yaml')
285
286     assert rules == dict(base=dict(first=1, second=2))
287
288
289 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
290 def test_load_subconf_include_relative(make_config_path, tmp_path, location):
291     config = make_config_path()
292
293     testfile = config.config_dir / 'test.yaml'
294     testfile.write_text(f'base: !include inc.yaml\n')
295     (getattr(config, location) / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
296
297     rules = config.load_sub_configuration('test.yaml')
298
299     assert rules == dict(base=dict(first=1, second=2))
300
301
302 def test_load_subconf_include_bad_format(make_config_path):
303     config = make_config_path()
304
305     testfile = config.config_dir / 'test.yaml'
306     testfile.write_text(f'base: !include inc.txt\n')
307     (config.config_dir / 'inc.txt').write_text('first: 1\nsecond: 2\n')
308
309     with pytest.raises(UsageError, match='Cannot handle config file format.'):
310         rules = config.load_sub_configuration('test.yaml')
311
312
313 def test_load_subconf_include_not_found(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
319     with pytest.raises(UsageError, match='Config file not found.'):
320         rules = config.load_sub_configuration('test.yaml')
321
322
323 def test_load_subconf_include_recursive(make_config_path):
324     config = make_config_path()
325
326     testfile = config.config_dir / 'test.yaml'
327     testfile.write_text(f'base: !include inc.yaml\n')
328     (config.config_dir / 'inc.yaml').write_text('- !include more.yaml\n- upper\n')
329     (config.config_dir / 'more.yaml').write_text('- the end\n')
330
331     rules = config.load_sub_configuration('test.yaml')
332
333     assert rules == dict(base=[['the end'], 'upper'])