2 Test for loading dotenv configuration.
 
   6 from nominatim.config import Configuration
 
   7 from nominatim.errors import UsageError
 
  10 def make_config(src_dir):
 
  11     """ Create a configuration object from the given project directory.
 
  13     def _mk_config(project_dir=None):
 
  14         return Configuration(project_dir, src_dir / 'settings')
 
  19 def make_config_path(src_dir, tmp_path):
 
  20     """ Create a configuration object with project and config directories
 
  21         in a temporary directory.
 
  24         (tmp_path / 'project').mkdir()
 
  25         (tmp_path / 'config').mkdir()
 
  26         conf = Configuration(tmp_path / 'project', src_dir / 'settings')
 
  27         conf.config_dir = tmp_path / 'config'
 
  33 def test_no_project_dir(make_config):
 
  34     config = make_config()
 
  36     assert config.DATABASE_WEBUSER == 'www-data'
 
  39 @pytest.mark.parametrize("val", ('apache', '"apache"'))
 
  40 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
 
  41     envfile = tmp_path / '.env'
 
  42     envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
 
  44     config = make_config(tmp_path)
 
  46     assert config.DATABASE_WEBUSER == 'apache'
 
  49 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
 
  50     envfile = tmp_path / '.env'
 
  51     envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
 
  53     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
 
  55     config = make_config(tmp_path)
 
  57     assert config.DATABASE_WEBUSER == 'nobody'
 
  60 def test_prefer_os_environ_can_unset_project_setting(make_config, monkeypatch, tmp_path):
 
  61     envfile = tmp_path / '.env'
 
  62     envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
 
  64     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', '')
 
  66     config = make_config(tmp_path)
 
  68     assert config.DATABASE_WEBUSER == ''
 
  71 def test_get_os_env_add_defaults(make_config, monkeypatch):
 
  72     config = make_config()
 
  74     monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
 
  76     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
 
  79 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
 
  80     config = make_config()
 
  82     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
 
  84     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
 
  87 def test_get_libpq_dsn_convert_default(make_config):
 
  88     config = make_config()
 
  90     assert config.get_libpq_dsn() == 'dbname=nominatim'
 
  93 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
 
  94     config = make_config()
 
  96     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
 
  97                        'pgsql:dbname=gis;password=foo;host=localhost')
 
  99     assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
 
 102 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
 
 105 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
 
 106     config = make_config()
 
 108     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
 
 109                        'pgsql:dbname=gis;password={}'.format(val))
 
 111     assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
 
 114 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
 
 115     config = make_config()
 
 117     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
 
 118                        'host=localhost dbname=gis password=foo')
 
 120     assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
 
 123 @pytest.mark.parametrize("value,result",
 
 124                          [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
 
 125                          [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
 
 126 def test_get_bool(make_config, monkeypatch, value, result):
 
 127     config = make_config()
 
 129     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
 
 131     assert config.get_bool('FOOBAR') == result
 
 133 def test_get_bool_empty(make_config):
 
 134     config = make_config()
 
 136     assert config.DATABASE_MODULE_PATH == ''
 
 137     assert not config.get_bool('DATABASE_MODULE_PATH')
 
 140 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
 
 141                                           ('85762513444', 85762513444)])
 
 142 def test_get_int_success(make_config, monkeypatch, value, result):
 
 143     config = make_config()
 
 145     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
 
 147     assert config.get_int('FOOBAR') == result
 
 150 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
 
 151 def test_get_int_bad_values(make_config, monkeypatch, value):
 
 152     config = make_config()
 
 154     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
 
 156     with pytest.raises(UsageError):
 
 157         config.get_int('FOOBAR')
 
 160 def test_get_int_empty(make_config):
 
 161     config = make_config()
 
 163     assert config.DATABASE_MODULE_PATH == ''
 
 165     with pytest.raises(UsageError):
 
 166         config.get_int('DATABASE_MODULE_PATH')
 
 169 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
 
 170     config = make_config()
 
 172     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
 
 174     expected = src_dir / 'settings' / 'import-street.style'
 
 176     assert config.get_import_style_file() == expected
 
 179 def test_get_import_style_extern_relative(make_config_path, monkeypatch):
 
 180     config = make_config_path()
 
 181     (config.project_dir / 'custom.style').write_text('x')
 
 183     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'custom.style')
 
 185     assert str(config.get_import_style_file()) == str(config.project_dir / 'custom.style')
 
 188 def test_get_import_style_extern_absolute(make_config, tmp_path, monkeypatch):
 
 189     config = make_config()
 
 190     cfgfile = tmp_path / 'test.style'
 
 192     cfgfile.write_text('x')
 
 194     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', str(cfgfile))
 
 196     assert str(config.get_import_style_file()) == str(cfgfile)
 
 199 def test_load_subconf_from_project_dir(make_config_path):
 
 200     config = make_config_path()
 
 202     testfile = config.project_dir / 'test.yaml'
 
 203     testfile.write_text('cow: muh\ncat: miau\n')
 
 205     testfile = config.config_dir / 'test.yaml'
 
 206     testfile.write_text('cow: miau\ncat: muh\n')
 
 208     rules = config.load_sub_configuration('test.yaml')
 
 210     assert rules == dict(cow='muh', cat='miau')
 
 213 def test_load_subconf_from_settings_dir(make_config_path):
 
 214     config = make_config_path()
 
 216     testfile = config.config_dir / 'test.yaml'
 
 217     testfile.write_text('cow: muh\ncat: miau\n')
 
 219     rules = config.load_sub_configuration('test.yaml')
 
 221     assert rules == dict(cow='muh', cat='miau')
 
 224 def test_load_subconf_empty_env_conf(make_config_path, monkeypatch):
 
 225     monkeypatch.setenv('NOMINATIM_MY_CONFIG', '')
 
 226     config = make_config_path()
 
 228     testfile = config.config_dir / 'test.yaml'
 
 229     testfile.write_text('cow: muh\ncat: miau\n')
 
 231     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
 
 233     assert rules == dict(cow='muh', cat='miau')
 
 236 def test_load_subconf_env_absolute_found(make_config_path, monkeypatch, tmp_path):
 
 237     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
 
 238     config = make_config_path()
 
 240     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
 
 241     (tmp_path / 'other.yaml').write_text('dog: muh\nfrog: miau\n')
 
 243     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
 
 245     assert rules == dict(dog='muh', frog='miau')
 
 248 def test_load_subconf_env_absolute_not_found(make_config_path, monkeypatch, tmp_path):
 
 249     monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
 
 250     config = make_config_path()
 
 252     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
 
 254     with pytest.raises(UsageError, match='Config file not found.'):
 
 255         rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
 
 258 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
 
 259 def test_load_subconf_env_relative_found(make_config_path, monkeypatch, location):
 
 260     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
 
 261     config = make_config_path()
 
 263     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
 
 264     (getattr(config, location) / 'other.yaml').write_text('dog: bark\n')
 
 266     rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
 
 268     assert rules == dict(dog='bark')
 
 271 def test_load_subconf_env_relative_not_found(make_config_path, monkeypatch):
 
 272     monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
 
 273     config = make_config_path()
 
 275     (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
 
 277     with pytest.raises(UsageError, match='Config file not found.'):
 
 278         rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
 
 281 def test_load_subconf_not_found(make_config_path):
 
 282     config = make_config_path()
 
 284     with pytest.raises(UsageError, match='Config file not found.'):
 
 285         rules = config.load_sub_configuration('test.yaml')
 
 288 def test_load_subconf_include_absolute(make_config_path, tmp_path):
 
 289     config = make_config_path()
 
 291     testfile = config.config_dir / 'test.yaml'
 
 292     testfile.write_text(f'base: !include {tmp_path}/inc.yaml\n')
 
 293     (tmp_path / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
 
 295     rules = config.load_sub_configuration('test.yaml')
 
 297     assert rules == dict(base=dict(first=1, second=2))
 
 300 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
 
 301 def test_load_subconf_include_relative(make_config_path, tmp_path, location):
 
 302     config = make_config_path()
 
 304     testfile = config.config_dir / 'test.yaml'
 
 305     testfile.write_text(f'base: !include inc.yaml\n')
 
 306     (getattr(config, location) / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
 
 308     rules = config.load_sub_configuration('test.yaml')
 
 310     assert rules == dict(base=dict(first=1, second=2))
 
 313 def test_load_subconf_include_bad_format(make_config_path):
 
 314     config = make_config_path()
 
 316     testfile = config.config_dir / 'test.yaml'
 
 317     testfile.write_text(f'base: !include inc.txt\n')
 
 318     (config.config_dir / 'inc.txt').write_text('first: 1\nsecond: 2\n')
 
 320     with pytest.raises(UsageError, match='Cannot handle config file format.'):
 
 321         rules = config.load_sub_configuration('test.yaml')
 
 324 def test_load_subconf_include_not_found(make_config_path):
 
 325     config = make_config_path()
 
 327     testfile = config.config_dir / 'test.yaml'
 
 328     testfile.write_text(f'base: !include inc.txt\n')
 
 330     with pytest.raises(UsageError, match='Config file not found.'):
 
 331         rules = config.load_sub_configuration('test.yaml')
 
 334 def test_load_subconf_include_recursive(make_config_path):
 
 335     config = make_config_path()
 
 337     testfile = config.config_dir / 'test.yaml'
 
 338     testfile.write_text(f'base: !include inc.yaml\n')
 
 339     (config.config_dir / 'inc.yaml').write_text('- !include more.yaml\n- upper\n')
 
 340     (config.config_dir / 'more.yaml').write_text('- the end\n')
 
 342     rules = config.load_sub_configuration('test.yaml')
 
 344     assert rules == dict(base=[['the end'], 'upper'])