2 Test for loading dotenv configuration.
4 from pathlib import Path
9 from nominatim.config import Configuration
10 from nominatim.errors import UsageError
12 DEFCFG_DIR = Path(__file__) / '..' / '..' / '..' / 'settings'
14 def test_no_project_dir():
15 config = Configuration(None, DEFCFG_DIR)
17 assert config.DATABASE_WEBUSER == 'www-data'
20 @pytest.mark.parametrize("val", ('apache', '"apache"'))
21 def test_prefer_project_setting_over_default(val, tmp_path):
22 envfile = tmp_path / '.env'
23 envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
25 config = Configuration(Path(tmp_path), DEFCFG_DIR)
27 assert config.DATABASE_WEBUSER == 'apache'
30 def test_prefer_os_environ_over_project_setting(monkeypatch, tmp_path):
31 envfile = tmp_path / '.env'
32 envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
34 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
36 config = Configuration(Path(tmp_path), DEFCFG_DIR)
38 assert config.DATABASE_WEBUSER == 'nobody'
41 def test_get_os_env_add_defaults(monkeypatch):
42 config = Configuration(None, DEFCFG_DIR)
44 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
46 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
49 def test_get_os_env_prefer_os_environ(monkeypatch):
50 config = Configuration(None, DEFCFG_DIR)
52 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
54 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
57 def test_get_libpq_dsn_convert_default():
58 config = Configuration(None, DEFCFG_DIR)
60 assert config.get_libpq_dsn() == 'dbname=nominatim'
63 def test_get_libpq_dsn_convert_php(monkeypatch):
64 config = Configuration(None, DEFCFG_DIR)
66 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
67 'pgsql:dbname=gis;password=foo;host=localhost')
69 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
72 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
75 def test_get_libpq_dsn_convert_php_special_chars(monkeypatch, val, expect):
76 config = Configuration(None, DEFCFG_DIR)
78 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
79 'pgsql:dbname=gis;password={}'.format(val))
81 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
84 def test_get_libpq_dsn_convert_libpq(monkeypatch):
85 config = Configuration(None, DEFCFG_DIR)
87 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
88 'host=localhost dbname=gis password=foo')
90 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
93 @pytest.mark.parametrize("value,result",
94 [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
95 [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
96 def test_get_bool(monkeypatch, value, result):
97 config = Configuration(None, DEFCFG_DIR)
99 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
101 assert config.get_bool('FOOBAR') == result
103 def test_get_bool_empty():
104 config = Configuration(None, DEFCFG_DIR)
106 assert config.DATABASE_MODULE_PATH == ''
107 assert config.get_bool('DATABASE_MODULE_PATH') == False
110 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
111 ('85762513444', 85762513444)])
112 def test_get_int_success(monkeypatch, value, result):
113 config = Configuration(None, DEFCFG_DIR)
115 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
117 assert config.get_int('FOOBAR') == result
120 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
121 def test_get_int_bad_values(monkeypatch, value):
122 config = Configuration(None, DEFCFG_DIR)
124 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
126 with pytest.raises(UsageError):
127 config.get_int('FOOBAR')
130 def test_get_int_empty():
131 config = Configuration(None, DEFCFG_DIR)
133 assert config.DATABASE_MODULE_PATH == ''
135 with pytest.raises(UsageError):
136 config.get_int('DATABASE_MODULE_PATH')
139 def test_get_import_style_intern(monkeypatch):
140 config = Configuration(None, DEFCFG_DIR)
142 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
144 expected = DEFCFG_DIR / 'import-street.style'
146 assert config.get_import_style_file() == expected
149 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
150 def test_get_import_style_intern(monkeypatch, value):
151 config = Configuration(None, DEFCFG_DIR)
153 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
155 assert str(config.get_import_style_file()) == value