2 Test for loading dotenv configuration.
4 from pathlib import Path
10 from nominatim.config import Configuration
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'
19 def test_prefer_project_setting_over_default():
20 with tempfile.TemporaryDirectory() as project_dir:
21 with open(project_dir + '/.env', 'w') as envfile:
22 envfile.write('NOMINATIM_DATABASE_WEBUSER=apache\n')
24 config = Configuration(Path(project_dir), DEFCFG_DIR)
26 assert config.DATABASE_WEBUSER == 'apache'
28 def test_prefer_os_environ_over_project_setting():
29 with tempfile.TemporaryDirectory() as project_dir:
30 with open(project_dir + '/.env', 'w') as envfile:
31 envfile.write('NOMINATIM_DATABASE_WEBUSER=apache\n')
33 os.environ['NOMINATIM_DATABASE_WEBUSER'] = 'nobody'
35 config = Configuration(Path(project_dir), DEFCFG_DIR)
37 assert config.DATABASE_WEBUSER == 'nobody'
39 del os.environ['NOMINATIM_DATABASE_WEBUSER']
41 def test_get_os_env_add_defaults():
42 config = Configuration(None, DEFCFG_DIR)
44 if 'NOMINATIM_DATABASE_WEBUSER' in os.environ:
45 del os.environ['NOMINATIM_DATABASE_WEBUSER']
47 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
49 def test_get_os_env_prefer_os_environ():
50 config = Configuration(None, DEFCFG_DIR)
52 os.environ['NOMINATIM_DATABASE_WEBUSER'] = 'nobody'
54 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
56 del os.environ['NOMINATIM_DATABASE_WEBUSER']
58 def test_get_libpq_dsn_convert_default():
59 config = Configuration(None, DEFCFG_DIR)
61 assert config.get_libpq_dsn() == 'dbname=nominatim'
63 def test_get_libpq_dsn_convert_php():
64 config = Configuration(None, DEFCFG_DIR)
66 os.environ['NOMINATIM_DATABASE_DSN'] = 'pgsql:dbname=gis;password=foo;host=localhost'
68 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
70 def test_get_libpq_dsn_convert_libpq():
71 config = Configuration(None, DEFCFG_DIR)
73 os.environ['NOMINATIM_DATABASE_DSN'] = 'host=localhost dbname=gis password=foo'
75 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'