Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lmove operation #11

Merged
merged 4 commits into from
May 23, 2022
Merged
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
14 changes: 14 additions & 0 deletions fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def __init__(self, value):
assert isinstance(value, bytes)
self.value = value

@classmethod
def decode(cls, value):
return value


class SimpleError(Exception):
"""Exception that will be turned into a frontend-specific exception."""
Expand Down Expand Up @@ -1779,6 +1783,16 @@ def linsert(self, key, where, pivot, value):
def llen(self, key):
return len(key.value)

@command((Key(list, None), Key(list), SimpleString, SimpleString))
def lmove(self, first_list, second_list, src, dst):
if src not in [b'LEFT', b'RIGHT']:
raise SimpleError(SYNTAX_ERROR_MSG)
if dst not in [b'LEFT', b'RIGHT']:
raise SimpleError(SYNTAX_ERROR_MSG)
el = self.rpop(first_list) if src == b'RIGHT' else self.lpop(first_list)
self.lpush(second_list, el) if dst == b'LEFT' else self.rpush(second_list, el)
return el

def _list_pop(self, get_slice, key, *args):
"""Implements lpop and rpop.

Expand Down
55 changes: 55 additions & 0 deletions test/test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,57 @@ def test_linsert_wrong_type(r):
with pytest.raises(redis.ResponseError):
r.linsert('foo', 'after', 'bar', 'element')

def test_lmove(r):
assert r.lmove('foo', 'bar', 'RIGHT', 'LEFT') is None
assert r.lpop('bar') is None
r.rpush('foo', 'one')
r.rpush('foo', 'two')
r.rpush('bar', 'one')

# RPOPLPUSH
assert r.lmove('foo', 'bar', 'RIGHT', 'LEFT') == b'two'
assert r.lrange('foo', 0, -1) == [b'one']
assert r.lrange('bar', 0, -1) == [b'two', b'one']
# LPOPRPUSH
assert r.lmove('bar', 'bar', 'LEFT', 'RIGHT') == b'two'
assert r.lrange('bar', 0, -1) == [b'one', b'two']
# RPOPRPUSH
r.rpush('foo', 'three')
assert r.lmove('foo', 'bar', 'RIGHT', 'RIGHT') == b'three'
assert r.lrange('foo', 0, -1) == [b'one']
assert r.lrange('bar', 0, -1) == [b'one', b'two', b'three']
# LPOPLPUSH
assert r.lmove('bar', 'foo', 'LEFT', 'LEFT') == b'one'
assert r.lrange('foo', 0, -1) == [b'one', b'one']
assert r.lrange('bar', 0, -1) == [b'two', b'three']

# Catch instances where we store bytes and strings inconsistently
# and thus bar = ['two', b'one']
assert r.lrem('bar', -1, 'two') == 1

def test_lmove_to_nonexistent_destination(r):
r.rpush('foo', 'one')
assert r.lmove('foo', 'bar', 'RIGHT', 'LEFT') == b'one'
assert r.rpop('bar') == b'one'

def test_lmove_expiry(r):
r.rpush('foo', 'one')
r.rpush('bar', 'two')
r.expire('bar', 10)
r.lmove('foo', 'bar', 'RIGHT', 'LEFT')
assert r.ttl('bar') > 0

def test_lmove_wrong_type(r):
r.set('foo', 'bar')
r.rpush('list', 'element')
with pytest.raises(redis.ResponseError):
r.lmove('foo', 'list', 'RIGHT', 'LEFT')
assert r.get('foo') == b'bar'
assert r.lrange('list', 0, -1) == [b'element']
with pytest.raises(redis.ResponseError):
r.lmove('list', 'foo', 'RIGHT', 'LEFT')
assert r.get('foo') == b'bar'
assert r.lrange('list', 0, -1) == [b'element']

def test_rpoplpush(r):
assert r.rpoplpush('foo', 'bar') is None
Expand Down Expand Up @@ -5273,6 +5324,10 @@ def test_llen(self, r):
with pytest.raises(redis.ConnectionError):
r.llen('name')

def test_lmove(self, r):
with pytest.raises(redis.ConnectionError):
r.lmove(1, 2, 'LEFT', 'RIGHT')

def test_lrem(self, r):
with pytest.raises(redis.ConnectionError):
r.lrem('name', 2, 2)
Expand Down