2 Test for loading dotenv configuration.
4 from pathlib import Path
8 from nominatim.config import Configuration
9 from nominatim.errors import UsageError
12 def make_config(src_dir):
13 """ Create a configuration object from the given project directory.
15 def _mk_config(project_dir=None):
16 return Configuration(project_dir, src_dir / 'settings')
21 def test_no_project_dir(make_config):
22 config = make_config()
24 assert config.DATABASE_WEBUSER == 'www-data'
27 @pytest.mark.parametrize("val", ('apache', '"apache"'))
28 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
29 envfile = tmp_path / '.env'
30 envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
32 config = make_config(tmp_path)
34 assert config.DATABASE_WEBUSER == 'apache'
37 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
38 envfile = tmp_path / '.env'
39 envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
41 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
43 config = make_config(tmp_path)
45 assert config.DATABASE_WEBUSER == 'nobody'
48 def test_get_os_env_add_defaults(make_config, monkeypatch):
49 config = make_config()
51 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
53 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
56 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
57 config = make_config()
59 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
61 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
64 def test_get_libpq_dsn_convert_default(make_config):
65 config = make_config()
67 assert config.get_libpq_dsn() == 'dbname=nominatim'
70 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
71 config = make_config()
73 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
74 'pgsql:dbname=gis;password=foo;host=localhost')
76 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
79 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
82 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
83 config = make_config()
85 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
86 'pgsql:dbname=gis;password={}'.format(val))
88 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
91 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
92 config = make_config()
94 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
95 'host=localhost dbname=gis password=foo')
97 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
100 @pytest.mark.parametrize("value,result",
101 [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
102 [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
103 def test_get_bool(make_config, monkeypatch, value, result):
104 config = make_config()
106 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
108 assert config.get_bool('FOOBAR') == result
110 def test_get_bool_empty(make_config):
111 config = make_config()
113 assert config.DATABASE_MODULE_PATH == ''
114 assert config.get_bool('DATABASE_MODULE_PATH') == False
117 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
118 ('85762513444', 85762513444)])
119 def test_get_int_success(make_config, monkeypatch, value, result):
120 config = make_config()
122 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
124 assert config.get_int('FOOBAR') == result
127 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
128 def test_get_int_bad_values(make_config, monkeypatch, value):
129 config = make_config()
131 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
133 with pytest.raises(UsageError):
134 config.get_int('FOOBAR')
137 def test_get_int_empty(make_config):
138 config = make_config()
140 assert config.DATABASE_MODULE_PATH == ''
142 with pytest.raises(UsageError):
143 config.get_int('DATABASE_MODULE_PATH')
146 def test_get_import_style_intern(make_config, monkeypatch):
147 config = make_config()
149 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
151 expected = DEFCFG_DIR / 'import-street.style'
153 assert config.get_import_style_file() == expected
156 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
157 def test_get_import_style_intern(make_config, monkeypatch, value):
158 config = make_config()
160 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
162 assert str(config.get_import_style_file()) == value