Skip to content

Commit

Permalink
Implement ltrim method
Browse files Browse the repository at this point in the history
  • Loading branch information
James Saryerwinnie committed Apr 15, 2012
1 parent 592c1cd commit 55e4859
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,15 @@ def rpushx(self, name, value):
return

def ltrim(self, name, start, end):
raise NotImplementedError()
try:
val = self._db[name]
except KeyError:
return
if end == -1:
end = None
else:
end += 1
self._db[name] = val[start:end]

def lindex(self, name, index):
try:
Expand Down
12 changes: 12 additions & 0 deletions test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,18 @@ def test_rpushx(self):
self.assertEqual(self.redis.lrange('foo', 0, -1), ['one', 'two'])
self.assertEqual(self.redis.lrange('bar', 0, -1), [])

def test_ltrim(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
self.redis.rpush('foo', 'three')
self.redis.rpush('foo', 'four')

self.redis.ltrim('foo', 1, 3)
self.assertEqual(self.redis.lrange('foo', 0, -1), ['two', 'three',
'four'])
self.redis.ltrim('foo', 1, -1)
self.assertEqual(self.redis.lrange('foo', 0, -1), ['three', 'four'])

def test_lindex(self):
self.redis.rpush('foo', 'one')
self.redis.rpush('foo', 'two')
Expand Down

0 comments on commit 55e4859

Please sign in to comment.