Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion decouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __contains__(self, key):
return False

def __getitem__(self, key):
return None
raise KeyError(key)


class RepositoryIni(RepositoryEmpty):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from decouple import Config, RepositoryEmpty, UndefinedValueError


@pytest.mark.parametrize('key', ['MISSING', '', None, 42])
def test_empty_repository_missing_key(key):
repository = RepositoryEmpty()

assert key not in repository
with pytest.raises(KeyError) as error:
repository[key]
assert error.value.args == (key,)


def test_empty_repository_config_missing(monkeypatch):
monkeypatch.delenv('DECOUPLE_EMPTY_TEST', raising=False)
config = Config(RepositoryEmpty())

with pytest.raises(UndefinedValueError):
config('DECOUPLE_EMPTY_TEST')
assert config('DECOUPLE_EMPTY_TEST', default=None) is None
assert config('DECOUPLE_EMPTY_TEST', default='42', cast=int) == 42


@pytest.mark.parametrize('value', ['', 'from environment'])
def test_empty_repository_config_environment(monkeypatch, value):
monkeypatch.setenv('DECOUPLE_EMPTY_TEST', value)
repository = RepositoryEmpty()

assert 'DECOUPLE_EMPTY_TEST' not in repository
with pytest.raises(KeyError):
repository['DECOUPLE_EMPTY_TEST']
assert Config(repository)('DECOUPLE_EMPTY_TEST', default='fallback') == value