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 def test_prefer_project_setting_over_default():
21 with tempfile.TemporaryDirectory() as project_dir:
22 with open(project_dir + '/.env', 'w') as envfile:
23 envfile.write('NOMINATIM_DATABASE_WEBUSER=apache\n')
25 config = Configuration(Path(project_dir), DEFCFG_DIR)
27 assert config.DATABASE_WEBUSER == 'apache'
30 def test_prefer_os_environ_over_project_setting(monkeypatch):
31 with tempfile.TemporaryDirectory() as project_dir:
32 with open(project_dir + '/.env', 'w') as envfile:
33 envfile.write('NOMINATIM_DATABASE_WEBUSER=apache\n')
35 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
37 config = Configuration(Path(project_dir), DEFCFG_DIR)
39 assert config.DATABASE_WEBUSER == 'nobody'
42 def test_get_os_env_add_defaults(monkeypatch):
43 config = Configuration(None, DEFCFG_DIR)
45 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
47 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
50 def test_get_os_env_prefer_os_environ(monkeypatch):
51 config = Configuration(None, DEFCFG_DIR)
53 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
55 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
58 def test_get_libpq_dsn_convert_default():
59 config = Configuration(None, DEFCFG_DIR)
61 assert config.get_libpq_dsn() == 'dbname=nominatim'
64 def test_get_libpq_dsn_convert_php(monkeypatch):
65 config = Configuration(None, DEFCFG_DIR)
67 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
68 'pgsql:dbname=gis;password=foo;host=localhost')
70 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
73 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
76 def test_get_libpq_dsn_convert_php_special_chars(monkeypatch, val, expect):
77 config = Configuration(None, DEFCFG_DIR)
79 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
80 'pgsql:dbname=gis;password={}'.format(val))
82 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
85 def test_get_libpq_dsn_convert_libpq(monkeypatch):
86 config = Configuration(None, DEFCFG_DIR)
88 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
89 'host=localhost dbname=gis password=foo')
91 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
94 @pytest.mark.parametrize("value,result",
95 [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
96 [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
97 def test_get_bool(monkeypatch, value, result):
98 config = Configuration(None, DEFCFG_DIR)
100 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
102 assert config.get_bool('FOOBAR') == result
104 def test_get_bool_empty():
105 config = Configuration(None, DEFCFG_DIR)
107 assert config.DATABASE_MODULE_PATH == ''
108 assert config.get_bool('DATABASE_MODULE_PATH') == False
111 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
112 ('85762513444', 85762513444)])
113 def test_get_int_success(monkeypatch, value, result):
114 config = Configuration(None, DEFCFG_DIR)
116 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
118 assert config.get_int('FOOBAR') == result
121 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
122 def test_get_int_bad_values(monkeypatch, value):
123 config = Configuration(None, DEFCFG_DIR)
125 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
127 with pytest.raises(UsageError):
128 config.get_int('FOOBAR')
131 def test_get_int_empty():
132 config = Configuration(None, DEFCFG_DIR)
134 assert config.DATABASE_MODULE_PATH == ''
136 with pytest.raises(UsageError):
137 config.get_int('DATABASE_MODULE_PATH')
140 def test_get_import_style_intern(monkeypatch):
141 config = Configuration(None, DEFCFG_DIR)
143 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
145 expected = DEFCFG_DIR / 'import-street.style'
147 assert config.get_import_style_file() == expected
150 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
151 def test_get_import_style_intern(monkeypatch, value):
152 config = Configuration(None, DEFCFG_DIR)
154 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
156 assert str(config.get_import_style_file()) == value