Skip to content

Commit

Permalink
fix:xrevrange to work with exclusive ranges (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
hurlenko authored Aug 24, 2024
1 parent 6af8177 commit 21a3fe5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions fakeredis/_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ def find_index(self, entry_key: StreamEntryKey, from_left: bool = True) -> Tuple
return 0, False
if from_left:
ind = bisect.bisect_left(self._ids, entry_key)
check_idx = ind
else:
ind = bisect.bisect_right(self._ids, entry_key)
return ind, (ind < len(self._ids) and self._ids[ind] == entry_key)
check_idx = ind - 1
return ind, (check_idx < len(self._ids) and self._ids[check_idx] == entry_key)

def find_index_key_as_str(self, entry_key_str: Union[str, bytes]) -> Tuple[int, bool]:
"""Find the closest index to entry_key_str in the stream
Expand Down Expand Up @@ -500,7 +502,7 @@ def _find_index(elem: StreamRangeTest, from_left: bool = True) -> int:
return len(self._ids)
ind, found = self.find_index(elem.value, from_left)
if found and elem.exclusive:
ind += 1
ind += 1 if from_left else -1
return ind

start_ind = _find_index(start)
Expand Down
20 changes: 20 additions & 0 deletions test/test_mixins/test_streams_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ def test_xrevrange(r: redis.Redis):
assert get_ids(results) == [m4]


def test_xrevrange_exclusive(r: redis.Redis):
stream = "stream"
m1, m2, m3, m4 = _add_to_stream(r, stream, 4)

def _exc(key: bytes) -> bytes:
return b"(" + key

results = r.xrevrange(stream, max=_exc(m4))
assert get_ids(results) == [m3, m2, m1]

results = r.xrevrange(stream, max=_exc(m3), min=m2)
assert get_ids(results) == [m2]

results = r.xrevrange(stream, min=_exc(m3))
assert get_ids(results) == [m4]

results = r.xrevrange(stream, min=_exc(m1))
assert get_ids(results) == [m4, m3, m2]


def test_xrange(r: redis.Redis):
m = r.xadd("stream1", {"foo": "bar"})
assert r.xrange("stream1") == [
Expand Down

0 comments on commit 21a3fe5

Please sign in to comment.