diff --git a/fakeredis.py b/fakeredis.py index 0a6fa2d..03c9393 100644 --- a/fakeredis.py +++ b/fakeredis.py @@ -469,7 +469,10 @@ def get(self, name): return to_bytes(value) def __getitem__(self, name): - return self.get(name) + value = self.get(name) + if value is not None: + return value + raise KeyError(name) def getbit(self, name, offset): """Returns a boolean indicating the value of ``offset`` in ``name``""" diff --git a/test_fakeredis.py b/test_fakeredis.py index f3cd506..bd46528 100644 --- a/test_fakeredis.py +++ b/test_fakeredis.py @@ -260,6 +260,11 @@ def test_setitem_getitem(self): self.redis['foo'] = 'bar' self.assertEqual(self.redis['foo'], b'bar') + def test_getitem_non_existent_key(self): + self.assertEqual(self.redis.keys(), []) + with self.assertRaises(KeyError): + self.redis['noexists'] + def test_strlen(self): self.redis['foo'] = 'bar'