2 Test for loading dotenv configuration.
4 from pathlib import Path
9 from nominatim.config import Configuration
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'
18 def test_prefer_project_setting_over_default():
19 with tempfile.TemporaryDirectory() as project_dir:
20 with open(project_dir + '/.env', 'w') as envfile:
21 envfile.write('NOMINATIM_DATABASE_WEBUSER=apache\n')
23 config = Configuration(Path(project_dir), DEFCFG_DIR)
25 assert config.DATABASE_WEBUSER == 'apache'
27 def test_prefer_os_environ_over_project_setting(monkeypatch):
28 with tempfile.TemporaryDirectory() as project_dir:
29 with open(project_dir + '/.env', 'w') as envfile:
30 envfile.write('NOMINATIM_DATABASE_WEBUSER=apache\n')
32 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
34 config = Configuration(Path(project_dir), DEFCFG_DIR)
36 assert config.DATABASE_WEBUSER == 'nobody'
38 def test_get_os_env_add_defaults(monkeypatch):
39 config = Configuration(None, DEFCFG_DIR)
41 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
43 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
45 def test_get_os_env_prefer_os_environ(monkeypatch):
46 config = Configuration(None, DEFCFG_DIR)
48 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
50 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
52 def test_get_libpq_dsn_convert_default():
53 config = Configuration(None, DEFCFG_DIR)
55 assert config.get_libpq_dsn() == 'dbname=nominatim'
57 def test_get_libpq_dsn_convert_php(monkeypatch):
58 config = Configuration(None, DEFCFG_DIR)
60 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
61 'pgsql:dbname=gis;password=foo;host=localhost')
63 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
65 def test_get_libpq_dsn_convert_libpq(monkeypatch):
66 config = Configuration(None, DEFCFG_DIR)
68 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
69 'host=localhost dbname=gis password=foo')
71 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'