Skip to content

Commit

Permalink
Add support for SORT_RO (#1858)
Browse files Browse the repository at this point in the history
* add sort_ro

* mark test as onlynon cluster

* delete mark test as onlynoncluster

* skip test
  • Loading branch information
dvora-h authored Mar 14, 2022
1 parent 160a7f6 commit 4b22476
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,39 @@ def sort(
options = {"groups": len(get) if groups else None}
return self.execute_command("SORT", *pieces, **options)

def sort_ro(
self,
key: str,
start: Optional[int] = None,
num: Optional[int] = None,
by: Optional[str] = None,
get: Optional[List[str]] = None,
desc: bool = False,
alpha: bool = False,
) -> list:
"""
Returns the elements contained in the list, set or sorted set at key.
(read-only variant of the SORT command)
``start`` and ``num`` allow for paging through the sorted data
``by`` allows using an external key to weight and sort the items.
Use an "*" to indicate where in the key the item value is located
``get`` allows for returning items from external keys rather than the
sorted data itself. Use an "*" to indicate where in the key
the item value is located
``desc`` allows for reversing the sort
``alpha`` allows for sorting lexicographically rather than numerically
For more information check https://redis.io/commands/sort_ro
"""
return self.sort(
key, start=start, num=num, by=by, get=get, desc=desc, alpha=alpha
)


AsyncListCommands = ListCommands

Expand Down
10 changes: 10 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,16 @@ def test_sort_all_options(self, r):
assert num == 4
assert r.lrange("sorted", 0, 10) == [b"vodka", b"milk", b"gin", b"apple juice"]

@skip_if_server_version_lt("7.0.0")
def test_sort_ro(self, r):
r["score:1"] = 8
r["score:2"] = 3
r["score:3"] = 5
r.rpush("a", "3", "2", "1")
assert r.sort_ro("a", by="score:*") == [b"2", b"3", b"1"]
r.rpush("b", "2", "3", "1")
assert r.sort_ro("b", desc=True) == [b"3", b"2", b"1"]

def test_sort_issue_924(self, r):
# Tests for issue https://github.com/andymccurdy/redis-py/issues/924
r.execute_command("SADD", "issue#924", 1)
Expand Down

0 comments on commit 4b22476

Please sign in to comment.