2 Test for loading dotenv configuration.
6 from nominatim.config import Configuration
7 from nominatim.errors import UsageError
10 def make_config(src_dir):
11 """ Create a configuration object from the given project directory.
13 def _mk_config(project_dir=None):
14 return Configuration(project_dir, src_dir / 'settings')
19 def make_config_path(src_dir, tmp_path):
20 """ Create a configuration object with project and config directories
21 in a temporary directory.
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'
33 def test_no_project_dir(make_config):
34 config = make_config()
36 assert config.DATABASE_WEBUSER == 'www-data'
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))
44 config = make_config(tmp_path)
46 assert config.DATABASE_WEBUSER == 'apache'
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')
53 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
55 config = make_config(tmp_path)
57 assert config.DATABASE_WEBUSER == 'nobody'
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')
64 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', '')
66 config = make_config(tmp_path)
68 assert config.DATABASE_WEBUSER == ''
71 def test_get_os_env_add_defaults(make_config, monkeypatch):
72 config = make_config()
74 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
76 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
79 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
80 config = make_config()
82 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
84 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
87 def test_get_libpq_dsn_convert_default(make_config):
88 config = make_config()
90 assert config.get_libpq_dsn() == 'dbname=nominatim'
93 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
94 config = make_config()
96 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
97 'pgsql:dbname=gis;password=foo;host=localhost')
99 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
102 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
105 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
106 config = make_config()
108 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
109 'pgsql:dbname=gis;password={}'.format(val))
111 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
114 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
115 config = make_config()
117 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
118 'host=localhost dbname=gis password=foo')
120 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
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()
129 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
131 assert config.get_bool('FOOBAR') == result
133 def test_get_bool_empty(make_config):
134 config = make_config()
136 assert config.DATABASE_MODULE_PATH == ''
137 assert not config.get_bool('DATABASE_MODULE_PATH')
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()
145 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
147 assert config.get_int('FOOBAR') == result
150 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
151 def test_get_int_bad_values(make_config, monkeypatch, value):
152 config = make_config()
154 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
156 with pytest.raises(UsageError):
157 config.get_int('FOOBAR')
160 def test_get_int_empty(make_config):
161 config = make_config()
163 assert config.DATABASE_MODULE_PATH == ''
165 with pytest.raises(UsageError):
166 config.get_int('DATABASE_MODULE_PATH')
169 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
170 config = make_config()
172 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
174 expected = src_dir / 'settings' / 'import-street.style'
176 assert config.get_import_style_file() == expected
179 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
180 def test_get_import_style_extern(make_config, monkeypatch, value):
181 config = make_config()
183 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
185 assert str(config.get_import_style_file()) == value
188 def test_load_subconf_from_project_dir(make_config_path):
189 config = make_config_path()
191 testfile = config.project_dir / 'test.yaml'
192 testfile.write_text('cow: muh\ncat: miau\n')
194 testfile = config.config_dir / 'test.yaml'
195 testfile.write_text('cow: miau\ncat: muh\n')
197 rules = config.load_sub_configuration('test.yaml')
199 assert rules == dict(cow='muh', cat='miau')
202 def test_load_subconf_from_settings_dir(make_config_path):
203 config = make_config_path()
205 testfile = config.config_dir / 'test.yaml'
206 testfile.write_text('cow: muh\ncat: miau\n')
208 rules = config.load_sub_configuration('test.yaml')
210 assert rules == dict(cow='muh', cat='miau')
213 def test_load_subconf_empty_env_conf(make_config_path, monkeypatch):
214 monkeypatch.setenv('NOMINATIM_MY_CONFIG', '')
215 config = make_config_path()
217 testfile = config.config_dir / 'test.yaml'
218 testfile.write_text('cow: muh\ncat: miau\n')
220 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
222 assert rules == dict(cow='muh', cat='miau')
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()
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')
232 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
234 assert rules == dict(dog='muh', frog='miau')
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()
241 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
243 with pytest.raises(UsageError, match='Config file not found.'):
244 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
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()
252 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
253 (getattr(config, location) / 'other.yaml').write_text('dog: bark\n')
255 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
257 assert rules == dict(dog='bark')
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()
264 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
266 with pytest.raises(UsageError, match='Config file not found.'):
267 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
270 def test_load_subconf_not_found(make_config_path):
271 config = make_config_path()
273 with pytest.raises(UsageError, match='Config file not found.'):
274 rules = config.load_sub_configuration('test.yaml')
277 def test_load_subconf_include_absolute(make_config_path, tmp_path):
278 config = make_config_path()
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')
284 rules = config.load_sub_configuration('test.yaml')
286 assert rules == dict(base=dict(first=1, second=2))
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()
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')
297 rules = config.load_sub_configuration('test.yaml')
299 assert rules == dict(base=dict(first=1, second=2))
302 def test_load_subconf_include_bad_format(make_config_path):
303 config = make_config_path()
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')
309 with pytest.raises(UsageError, match='Cannot handle config file format.'):
310 rules = config.load_sub_configuration('test.yaml')
313 def test_load_subconf_include_not_found(make_config_path):
314 config = make_config_path()
316 testfile = config.config_dir / 'test.yaml'
317 testfile.write_text(f'base: !include inc.txt\n')
319 with pytest.raises(UsageError, match='Config file not found.'):
320 rules = config.load_sub_configuration('test.yaml')
323 def test_load_subconf_include_recursive(make_config_path):
324 config = make_config_path()
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')
331 rules = config.load_sub_configuration('test.yaml')
333 assert rules == dict(base=[['the end'], 'upper'])