Skip to content

Commit

Permalink
Fix zadd when key already exists with same score
Browse files Browse the repository at this point in the history
Real redis treats it as a no-op rather than replacing it. This means
that if the score was -0.0 and is being changed to +0.0 (or vice versa),
it isn't updated. It also affects watches.
  • Loading branch information
bmerry committed Jan 6, 2019
1 parent 38e9da2 commit f71d09b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1891,8 +1891,9 @@ def zadd(self, key, *args):
items.append((score, args[i + 1]))
old_len = len(key.value)
for item in items:
key.value[item[1]] = item[0]
key.updated()
if item[1] not in key.value or key.value[item[1]] != item[0]:
key.value[item[1]] = item[0]
key.updated()
return len(key.value) - old_len

@command((Key(ZSet),))
Expand Down
6 changes: 6 additions & 0 deletions test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,12 @@ def test_zadd_empty(self):
with self.assertRaises(redis.RedisError):
self.zadd('foo', {})

def test_zadd_minus_zero(self):
# Changing -0 to +0 is ignored
self.zadd('foo', {'a': -0.0})
self.zadd('foo', {'a': 0.0})
self.assertEqual(self.redis.execute_command('zscore', 'foo', 'a'), b'-0')

def test_zadd_wrong_type(self):
self.redis.sadd('foo', 'bar')
with self.assertRaises(redis.ResponseError):
Expand Down

0 comments on commit f71d09b

Please sign in to comment.