def test_as_dict(self, mock_envfile): settings = Settings(env_prefix="PREFIX_") settings.load_dotenv_file(mock_envfile) as_dict = settings.as_dict() assert as_dict == settings._config assert isinstance(as_dict, dict)
def test_load_environment_does_not_load_unprefixed_vars( self, mock_environment): settings = Settings(env_prefix="PREFIX_") settings.load_env() with pytest.raises(AttributeError): settings.USER
def test_load_environment_with_override_keys(self, mock_environment, override_keys_function): settings = Settings("PREFIX_", "OVERRIDE_", override_keys_function) settings.load_env() assert settings.DEBUG is False assert settings.HASH == {"dict": "test"}
def test_override_keys_function_not_callable_raises_warning( self, mock_environment): settings = Settings("PREFIX_", "OVERRIDE_", ["KEY"]) settings.load_env() with pytest.warns(NotCallableWarning): assert settings.DEBUG is True
def test_load_envfile_with_prefix_strips_out_prefix(self, mock_envfile): settings = Settings(env_prefix="PREFIX_") settings.load_dotenv_file(mock_envfile) with pytest.raises(AttributeError): settings.PREFIX_DEBUG assert settings.DEBUG is True
def test_override_keys_reverse_lookup(self, mock_environment, mocker): settings = Settings("PREFIX_", "OVERRIDE_", override_keys_function, override_keys_reverse_lookup=True) mocker.patch.object(settings, "override_keys_function") settings.override_keys_function.return_value = ("KEY1", "KEY2") settings.load_env() assert settings.VALUE_1 == "key2-value-1" assert settings.VALUE_2 == "key1-value-2" assert settings.VALUE_3 == "default-value-3"
def test_override_keys_function_preserves_order_of_overrides(self, mock_environment, mocker): settings = Settings("PREFIX_", "OVERRIDE_", override_keys_function) mocker.patch.object(settings, "override_keys_function") settings.override_keys_function.return_value = ("KEY1", "KEY2") settings.load_env() assert settings.VALUE_1 == "key1-value-1" assert settings.VALUE_2 == "key1-value-2" assert settings.VALUE_3 == "default-value-3"
def test_load_environment_with_prefix_strips_out_prefix(self, mock_environment): settings = Settings(env_prefix="PREFIX_") settings.load_env() with pytest.raises(AttributeError): settings.PREFIX_DEBUG assert settings.DEBUG is True
def test_override_keys_function_does_not_return_list_or_set_raises_warning(self, mock_environment, mocker): settings = Settings("PREFIX_", "OVERRIDE_", override_keys_function) mocker.patch.object(settings, "override_keys_function") settings.override_keys_function.return_value = "KEY1, KEY2" settings.load_env() with pytest.warns(UnsupportedSetTypeWarning): assert settings.HASH == {"dict": "test"} assert settings.DEBUG is True
def test_load_envfile_with_prefix(self, mock_envfile): settings = Settings(env_prefix="PREFIX_") settings.load_dotenv_file(mock_envfile) assert settings.DEBUG is True assert settings.ESCAPED_HASH == {"dict": "test"} assert settings.UNESCAPED_HASH == {"dict": "test"} assert settings.MULTILINE_KEY == ( "===BEGIN PUBLIC CERTIFICATE=== \n" " it is a certificate value \n" "===END PUBLIC CERTIFICATE===" )
def test_missing_missing_override_key_from_callable_raises_warning(self, mock_environment, mocker): settings = Settings("PREFIX_", "OVERRIDE_", override_keys_function) mocker.patch.object(settings, "override_keys_function") settings.override_keys_function.return_value = ["MISSING_KEY"] settings.load_env() with pytest.warns(MissingOverrideKeysWarning): assert settings.DEBUG is True settings.override_keys_function.return_value = [] assert settings.HASH == {"dict": "test"} assert settings.DEBUG is True
def test_prefix_gets_stripped_once_only(self, mock_environment): settings = Settings(env_prefix="PREFIX_") settings.load_env() assert settings.SOME_KEY_PREFIX_REPEATED == "nested"
def test_load_environment_with_prefix(self, mock_environment): settings = Settings(env_prefix="PREFIX_") settings.load_env() assert settings.DEBUG is True assert settings.HASH == {"dict": "test"}
def settings(): settings = Settings(env_prefix="PREFIX_") settings.load_env() return settings
def test_prefix_gets_stripped_once_only(self, mock_envfile): settings = Settings(env_prefix="PREFIX_") settings.load_dotenv_file(mock_envfile) assert settings.SOME_KEY_PREFIX_REPEATED == "nested"
def test_load_envfile_does_not_load_unprefixed_vars(self, mock_envfile): settings = Settings(env_prefix="PREFIX_") settings.load_dotenv_file(mock_envfile) with pytest.raises(AttributeError): settings.USER