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 test_no_project_dir(make_config):
20 config = make_config()
22 assert config.DATABASE_WEBUSER == 'www-data'
25 @pytest.mark.parametrize("val", ('apache', '"apache"'))
26 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
27 envfile = tmp_path / '.env'
28 envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
30 config = make_config(tmp_path)
32 assert config.DATABASE_WEBUSER == 'apache'
35 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
36 envfile = tmp_path / '.env'
37 envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
39 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
41 config = make_config(tmp_path)
43 assert config.DATABASE_WEBUSER == 'nobody'
46 def test_get_os_env_add_defaults(make_config, monkeypatch):
47 config = make_config()
49 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
51 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
54 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
55 config = make_config()
57 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
59 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
62 def test_get_libpq_dsn_convert_default(make_config):
63 config = make_config()
65 assert config.get_libpq_dsn() == 'dbname=nominatim'
68 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
69 config = make_config()
71 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
72 'pgsql:dbname=gis;password=foo;host=localhost')
74 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
77 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
80 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
81 config = make_config()
83 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
84 'pgsql:dbname=gis;password={}'.format(val))
86 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
89 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
90 config = make_config()
92 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
93 'host=localhost dbname=gis password=foo')
95 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
98 @pytest.mark.parametrize("value,result",
99 [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
100 [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
101 def test_get_bool(make_config, monkeypatch, value, result):
102 config = make_config()
104 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
106 assert config.get_bool('FOOBAR') == result
108 def test_get_bool_empty(make_config):
109 config = make_config()
111 assert config.DATABASE_MODULE_PATH == ''
112 assert not config.get_bool('DATABASE_MODULE_PATH')
115 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
116 ('85762513444', 85762513444)])
117 def test_get_int_success(make_config, monkeypatch, value, result):
118 config = make_config()
120 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
122 assert config.get_int('FOOBAR') == result
125 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
126 def test_get_int_bad_values(make_config, monkeypatch, value):
127 config = make_config()
129 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
131 with pytest.raises(UsageError):
132 config.get_int('FOOBAR')
135 def test_get_int_empty(make_config):
136 config = make_config()
138 assert config.DATABASE_MODULE_PATH == ''
140 with pytest.raises(UsageError):
141 config.get_int('DATABASE_MODULE_PATH')
144 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
145 config = make_config()
147 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
149 expected = src_dir / 'settings' / 'import-street.style'
151 assert config.get_import_style_file() == expected
154 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
155 def test_get_import_style_extern(make_config, monkeypatch, value):
156 config = make_config()
158 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
160 assert str(config.get_import_style_file()) == value