Skip to content

Commit 07a3fdc

Browse files
MPalaryadanni-m
authored andcommitted
RED-74787: add support for hset(name, mapping) to deprecate hmset
1 parent 8d93737 commit 07a3fdc

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

mockredis/client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,26 @@ def hmget(self, hashkey, keys, *args):
583583
attributes = self._list_or_args(keys, args)
584584
return [self._decode(redis_hash.get(self._encode(attribute))) for attribute in attributes]
585585

586-
def hset(self, hashkey, attribute, value):
586+
def hset(self, hashkey, attribute=None, value=None, mapping=None):
587587
"""Emulate hset."""
588+
if attribute is None and not mapping:
589+
raise DataError("'hset' with no key value pairs")
588590

589591
redis_hash = self._get_hash(hashkey, 'HSET', create=True)
590-
attribute = self._encode(attribute)
591-
attribute_present = attribute in redis_hash
592-
redis_hash[attribute] = self._encode(value)
593-
return long(0) if attribute_present else long(1)
592+
if attribute is not None:
593+
attribute = self._encode(attribute)
594+
attribute_present = attribute in redis_hash
595+
redis_hash[attribute] = self._encode(value)
596+
return long(0) if attribute_present else long(1)
597+
598+
elif mapping:
599+
created = long(0)
600+
for attr, val in mapping.items():
601+
attr = self._encode(attr)
602+
attribute_present = attr in redis_hash
603+
redis_hash[attr] = self._encode(val)
604+
created += 0 if attribute_present else 1
605+
return created
594606

595607
def hsetnx(self, hashkey, attribute, value):
596608
"""Emulate hsetnx."""

mockredis/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Prefer actual exceptions to defining our own, so code that swaps
77
# in implementations does not have to swap in different exception
88
# classes.
9-
from redis.exceptions import RedisError, ResponseError, WatchError
9+
from redis.exceptions import RedisError, ResponseError, WatchError, DataError
1010
except ImportError:
1111
class RedisError(Exception):
1212
pass
@@ -16,3 +16,6 @@ class ResponseError(RedisError):
1616

1717
class WatchError(RedisError):
1818
pass
19+
20+
class DataError(RedisError):
21+
pass

0 commit comments

Comments
 (0)