Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ redis-cli shutdown
|[x]|[x]|[ ]|Hashes|
|[ ]|[ ]|[ ]|HyperLogLog|
|[ ]|[ ]|[ ]|sscan, hscan, zscan|
|[ ]|[ ]|[ ]|sort, sortNStore|
|[x]|[x]|[ ]|sort, sortNStore|

#### Features
|started|feature impl|complete|feature|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ trait MockListOperations extends ListOperations with MockOperations with Storage
* http://redis.io/commands/lpush
*/
override def lpush(key: Any, value: Any, values: Any*)(implicit format: Format): Option[Long] = withDB {
(value :: values.toList).map(Bytes.apply) ++ getRawOrEmpty(key).data <| (set(key, _)) |> (v => Some(v.size))
(value :: values.toList).reverse.map(Bytes.apply) ++ getRawOrEmpty(key).data <|
(set(key, _)) |> (v => Some(v.size))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ trait MockOperations extends Operations with Storage {
(implicit format: Format, parse: Parse[A]): Option[List[Option[A]]] = withDB {
currentDB.get(Key(key)) map { v =>
Sorter(getBytesSeq(v)).setLookup(by).setAlpha(alpha).setOrder(desc).setLimit(limit).setGetter(get).result
}
} orElse Some(List.empty)
}

/**
Expand All @@ -128,7 +128,7 @@ trait MockOperations extends Operations with Storage {
by: Option[String] = None,
get: List[String] = Nil,
storeAt: String)(implicit format: Format, parse: Parse[A]): Option[Long] = withDB {
val xs = sort(key, limit, desc, alpha, by, get).map(_.flatten).getOrElse(Nil)
val xs = sort(key, limit, desc, alpha, by, get).map(_.flatten).get
currentDB.update(Key(storeAt), ListValue(xs.map(Bytes.apply)))
Some(xs.length)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,99 @@ with BeforeAndAfterAll {
thrown.getMessage shouldBe "ERR Client sent AUTH, but no password is set"
}
}

describe("sort (additional)") {
it("should sort list") {
r.lpush("list-1", 0, -100, -25, 30, 456, 7, 8901)
r.lpush("list-1", 20000000000L)
r.lpush("list-1", -0.01, 0.01, 3.14)

r.sort("list-1", None, desc=false, alpha=false) shouldBe Some(List(
"-100", "-25", "-0.01", "0", "0.01", "3.14", "7", "30", "456", "8901", "20000000000").map(Some.apply))
r.sort("list-1", None, desc=false, alpha=true) shouldBe Some(List(
"-0.01", "-100", "-25", "0", "0.01", "20000000000", "3.14", "30", "456", "7", "8901").map(Some.apply))
r.sort("list-1", None, desc=true, alpha=false) shouldBe Some(List(
"20000000000", "8901", "456", "30", "7", "3.14", "0.01", "0", "-0.01", "-25", "-100").map(Some.apply))
r.sort("list-1", None, desc=true, alpha=true) shouldBe Some(List(
"8901", "7", "456", "30", "3.14", "20000000000", "0.01", "0", "-25", "-100", "-0.01").map(Some.apply))
}
it("should sort set") {
r.sadd("set-1", 0, -100, -25, 30, 456, 7, 8901)
r.sadd("set-1", 20000000000L)
r.sadd("set-1", -0.01, 0.01, 3.14)

r.sort("set-1", None, desc=false, alpha=false) shouldBe Some(List(
"-100", "-25", "-0.01", "0", "0.01", "3.14", "7", "30", "456", "8901", "20000000000").map(Some.apply))
r.sort("set-1", None, desc=false, alpha=true) shouldBe Some(List(
"-0.01", "-100", "-25", "0", "0.01", "20000000000", "3.14", "30", "456", "7", "8901").map(Some.apply))
r.sort("set-1", None, desc=true, alpha=false) shouldBe Some(List(
"20000000000", "8901", "456", "30", "7", "3.14", "0.01", "0", "-0.01", "-25", "-100").map(Some.apply))
r.sort("set-1", None, desc=true, alpha=true) shouldBe Some(List(
"8901", "7", "456", "30", "3.14", "20000000000", "0.01", "0", "-25", "-100", "-0.01").map(Some.apply))
}
it("should sort sorted set") {
r.zadd("zset-1", 0, 0, (0, -100), (0, -25), (0, 30), (0, 456), (0, 7), (0, 8901))
r.zadd("zset-1", 0, 20000000000L)
r.zadd("zset-1", 0, -0.01, (0, 0.01), (0, 3.14))

r.sort("zset-1", None, desc=false, alpha=false) shouldBe Some(List(
"-100", "-25", "-0.01", "0", "0.01", "3.14", "7", "30", "456", "8901", "20000000000").map(Some.apply))
r.sort("zset-1", None, desc=false, alpha=true) shouldBe Some(List(
"-0.01", "-100", "-25", "0", "0.01", "20000000000", "3.14", "30", "456", "7", "8901").map(Some.apply))
r.sort("zset-1", None, desc=true, alpha=false) shouldBe Some(List(
"20000000000", "8901", "456", "30", "7", "3.14", "0.01", "0", "-0.01", "-25", "-100").map(Some.apply))
r.sort("zset-1", None, desc=true, alpha=true) shouldBe Some(List(
"8901", "7", "456", "30", "3.14", "20000000000", "0.01", "0", "-25", "-100", "-0.01").map(Some.apply))
}
it("should return empty list when the key does not exist") {
r.sort("xxx-1") shouldBe Some(List.empty)
}
it("should throw exception when illegal type") {
r.set("str-1", 0)
r.hset("hash-1", "field", 0)

val t1 = the[Exception] thrownBy r.sort("str-1")
t1.getMessage shouldBe "WRONGTYPE Operation against a key holding the wrong kind of value"

val t2 = the[Exception] thrownBy r.sort("hash-1")
t2.getMessage shouldBe "WRONGTYPE Operation against a key holding the wrong kind of value"
}
it("should work with nosort option") {
r.lpush("list-1", 1, 23, 4, 567, 8)
r.sort("list-1", desc=false, alpha=false, by = Some("nosort")) shouldBe Some(List(
Some("8"), Some("567"), Some("4"), Some("23"), Some("1")))
r.sort("list-1", desc=false, alpha=false, by = Some("")) shouldBe Some(List(
Some("8"), Some("567"), Some("4"), Some("23"), Some("1")))
r.sort("list-1", desc=false, alpha=false, by = Some("xxx")) shouldBe Some(List(
Some("8"), Some("567"), Some("4"), Some("23"), Some("1")))
}
it("should sort by string lookup") {
r.lpush("list-1", 1, 23, 4, 567, 8)
r.lpush("list-2", 1, 9, 99, 23)
r.mset("x-1-y" -> 5, "x-23-y" -> 40, "x-4-y" -> 1, "x-567-y" -> 2, "x-8-y" -> 3)

r.sort("list-1", desc=false, alpha=false, by = Some("x-*-y")) shouldBe Some(List(
Some("4"), Some("567"), Some("8"), Some("1"), Some("23")))
r.sort("list-1", desc=false, alpha=true, by = Some("x-*-y")) shouldBe Some(List(
Some("4"), Some("567"), Some("8"), Some("23"), Some("1")))
r.sort("list-1", desc=true, alpha=false, by = Some("x-*-y")) shouldBe Some(List(
Some("23"), Some("1"), Some("8"), Some("567"), Some("4")))
r.sort("list-1", desc=true, alpha=true, by = Some("x-*-y")) shouldBe Some(List(
Some("1"), Some("23"), Some("8"), Some("567"), Some("4")))

r.sort("list-2", desc=false, alpha=true, by = Some("x-*-y")) shouldBe Some(List(
Some("99"), Some("9"), Some("23"), Some("1")))
}
it("should work with string getter") {
r.lpush("list-1", 1, 23, 4)
r.set("x-1-y", "v1")
r.set("x-23-y", "v23")
r.set("x-4-y", "v4")
r.sort("list-1", desc=false, alpha=false, get = List("x-*-y", "xxx-*", "list-1", "#", "xxx")) shouldBe Some(List(
Some("v1"), None, None, Some("1"), None,
Some("v4"), None, None, Some("4"), None,
Some("v23"), None, None, Some("23"), None
))
}
}
}