2 Test for loading dotenv configuration.
4 from pathlib import Path
8 from nominatim.config import Configuration
9 from nominatim.errors import UsageError
11 DEFCFG_DIR = Path(__file__) / '..' / '..' / '..' / 'settings'
13 def test_no_project_dir():
14 config = Configuration(None, DEFCFG_DIR)
16 assert config.DATABASE_WEBUSER == 'www-data'
19 @pytest.mark.parametrize("val", ('apache', '"apache"'))
20 def test_prefer_project_setting_over_default(val, tmp_path):
21 envfile = tmp_path / '.env'
22 envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
24 config = Configuration(Path(tmp_path), DEFCFG_DIR)
26 assert config.DATABASE_WEBUSER == 'apache'
29 def test_prefer_os_environ_over_project_setting(monkeypatch, tmp_path):
30 envfile = tmp_path / '.env'
31 envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
33 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
35 config = Configuration(Path(tmp_path), DEFCFG_DIR)
37 assert config.DATABASE_WEBUSER == 'nobody'
40 def test_get_os_env_add_defaults(monkeypatch):
41 config = Configuration(None, DEFCFG_DIR)
43 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
45 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
48 def test_get_os_env_prefer_os_environ(monkeypatch):
49 config = Configuration(None, DEFCFG_DIR)
51 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
53 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
56 def test_get_libpq_dsn_convert_default():
57 config = Configuration(None, DEFCFG_DIR)
59 assert config.get_libpq_dsn() == 'dbname=nominatim'
62 def test_get_libpq_dsn_convert_php(monkeypatch):
63 config = Configuration(None, DEFCFG_DIR)
65 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
66 'pgsql:dbname=gis;password=foo;host=localhost')
68 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
71 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
74 def test_get_libpq_dsn_convert_php_special_chars(monkeypatch, val, expect):
75 config = Configuration(None, DEFCFG_DIR)
77 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
78 'pgsql:dbname=gis;password={}'.format(val))
80 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
83 def test_get_libpq_dsn_convert_libpq(monkeypatch):
84 config = Configuration(None, DEFCFG_DIR)
86 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
87 'host=localhost dbname=gis password=foo')
89 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
92 @pytest.mark.parametrize("value,result",
93 [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
94 [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
95 def test_get_bool(monkeypatch, value, result):
96 config = Configuration(None, DEFCFG_DIR)
98 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
100 assert config.get_bool('FOOBAR') == result
102 def test_get_bool_empty():
103 config = Configuration(None, DEFCFG_DIR)
105 assert config.DATABASE_MODULE_PATH == ''
106 assert config.get_bool('DATABASE_MODULE_PATH') == False
109 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
110 ('85762513444', 85762513444)])
111 def test_get_int_success(monkeypatch, value, result):
112 config = Configuration(None, DEFCFG_DIR)
114 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
116 assert config.get_int('FOOBAR') == result
119 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
120 def test_get_int_bad_values(monkeypatch, value):
121 config = Configuration(None, DEFCFG_DIR)
123 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
125 with pytest.raises(UsageError):
126 config.get_int('FOOBAR')
129 def test_get_int_empty():
130 config = Configuration(None, DEFCFG_DIR)
132 assert config.DATABASE_MODULE_PATH == ''
134 with pytest.raises(UsageError):
135 config.get_int('DATABASE_MODULE_PATH')
138 def test_get_import_style_intern(monkeypatch):
139 config = Configuration(None, DEFCFG_DIR)
141 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
143 expected = DEFCFG_DIR / 'import-street.style'
145 assert config.get_import_style_file() == expected
148 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
149 def test_get_import_style_intern(monkeypatch, value):
150 config = Configuration(None, DEFCFG_DIR)
152 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
154 assert str(config.get_import_style_file()) == value