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

Support exclusive range in zRange, lRange, bitCount, getRange #989

Merged
merged 1 commit into from
Aug 19, 2024
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
16 changes: 16 additions & 0 deletions modules/redis-it/src/test/scala/zio/redis/ListSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ trait ListSpec extends IntegrationSpec {
range <- redis.lRange(key, 0 to 1).returning[String]
} yield assert(range)(equalTo(Chunk("hello", "world")))
},
test("lRange elements by exclusive range") {
for {
redis <- ZIO.service[Redis]
key <- uuid
_ <- redis.lPush(key, "c", "b", "a")
range <- redis.lRange(key, 0 until 2).returning[String]
} yield assert(range)(equalTo(Chunk("a", "b")))
},
test("lRange two elements negative indices") {
for {
redis <- ZIO.service[Redis]
Expand All @@ -349,6 +357,14 @@ trait ListSpec extends IntegrationSpec {
range <- redis.lRange(key, -2 to -1).returning[String]
} yield assert(range)(equalTo(Chunk("hello", "world")))
},
test("lRange elements by exclusive range with negative indices") {
for {
redis <- ZIO.service[Redis]
key <- uuid
_ <- redis.lPush(key, "d", "c", "b", "a")
range <- redis.lRange(key, -3 until -1).returning[String]
} yield assert(range)(equalTo(Chunk("b", "c")))
},
test("lRange start out of bounds") {
for {
redis <- ZIO.service[Redis]
Expand Down
41 changes: 40 additions & 1 deletion modules/redis-it/src/test/scala/zio/redis/SortedSetsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ trait SortedSetsSpec extends IntegrationSpec {
} yield assert(result.toList)(isEmpty))
),
suite("zRange")(
test("non-empty set") {
test("all members for non-empty set") {
for {
redis <- ZIO.service[Redis]
key <- uuid
Expand All @@ -891,6 +891,45 @@ trait SortedSetsSpec extends IntegrationSpec {
)
)
},
test("some members for non-empty set by inclusive range with negative bounds") {
for {
redis <- ZIO.service[Redis]
key <- uuid
delhi = MemberScore("Delhi", 1d)
mumbai = MemberScore("Mumbai", 2d)
london = MemberScore("London", 3d)
paris = MemberScore("Paris", 4d)
tokyo = MemberScore("Tokyo", 5d)
_ <- redis.zAdd(key)(delhi, mumbai, london, tokyo, paris)
result <- redis.zRange(key, -3 to -2).returning[String]
} yield assert(result.toList)(equalTo(List("London", "Paris")))
},
test("some members for non-empty set by inclusive range with positive bounds") {
for {
redis <- ZIO.service[Redis]
key <- uuid
delhi = MemberScore("Delhi", 1d)
mumbai = MemberScore("Mumbai", 2d)
london = MemberScore("London", 3d)
paris = MemberScore("Paris", 4d)
tokyo = MemberScore("Tokyo", 5d)
_ <- redis.zAdd(key)(delhi, mumbai, london, tokyo, paris)
result <- redis.zRange(key, 0 to 2).returning[String]
} yield assert(result.toList)(equalTo(List("Delhi", "Mumbai", "London")))
},
test("some members for non-empty set by exclusive range") {
for {
redis <- ZIO.service[Redis]
key <- uuid
delhi = MemberScore("Delhi", 1d)
mumbai = MemberScore("Mumbai", 2d)
london = MemberScore("London", 3d)
paris = MemberScore("Paris", 4d)
tokyo = MemberScore("Tokyo", 5d)
_ <- redis.zAdd(key)(delhi, mumbai, london, tokyo, paris)
result <- redis.zRange(key, 1 until -2).returning[String]
} yield assert(result.toList)(equalTo(List("Mumbai", "London")))
},
test("empty set") {
for {
redis <- ZIO.service[Redis]
Expand Down
18 changes: 17 additions & 1 deletion modules/redis-it/src/test/scala/zio/redis/StringsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ trait StringsSpec extends IntegrationSpec {
for {
redis <- ZIO.service[Redis]
key <- uuid
_ <- redis.set(key, "value")
_ <- redis.set(key, "value") // "alu" => 01100001 01101100 01110101
count <- redis.bitCount(key, Some(1 to 3))
} yield assert(count)(equalTo(12L))
},
test("over non-empty string with exclusive range") {
for {
redis <- ZIO.service[Redis]
key <- uuid
_ <- redis.set(key, "value")
count <- redis.bitCount(key, Some(1 until 4))
} yield assert(count)(equalTo(12L))
},
test("over non-empty string with range that is too large") {
for {
redis <- ZIO.service[Redis]
Expand Down Expand Up @@ -1047,6 +1055,14 @@ trait StringsSpec extends IntegrationSpec {
substr <- redis.getRange(key, 1 to 3).returning[String]
} yield assert(substr)(isSome(equalTo("alu")))
},
test("from non-empty string by exclusive range") {
for {
redis <- ZIO.service[Redis]
key <- uuid
_ <- redis.set(key, "value")
substr <- redis.getRange(key, 1 until 3).returning[String]
} yield assert(substr)(isSome(equalTo("al")))
},
test("with range that exceeds non-empty string length") {
for {
redis <- ZIO.service[Redis]
Expand Down
6 changes: 4 additions & 2 deletions modules/redis/src/main/scala/zio/redis/Input.scala
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ object Input {
}

case object RangeInput extends Input[Range] {
def encode(data: Range): RespCommand =
RespCommand(RespCommandArgument.Value(data.start.toString), RespCommandArgument.Value(data.end.toString))
def encode(data: Range): RespCommand = {
val end = if (data.isInclusive) data.end else data.end - 1
RespCommand(RespCommandArgument.Value(data.start.toString), RespCommandArgument.Value(end.toString))
}
}

case object RankInput extends Input[Rank] {
Expand Down
28 changes: 24 additions & 4 deletions modules/redis/src/test/scala/zio/redis/InputSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -649,23 +649,43 @@ object InputSpec extends BaseSpec {
suite("Range")(
test("with positive start and positive end") {
for {
result <- ZIO.attempt(RangeInput.encode(Range(1, 5)))
result <- ZIO.attempt(RangeInput.encode(1 to 5))
} yield assert(result)(equalTo(RespCommand(Value("1"), Value("5"))))
},
test("with negative start and positive end") {
for {
result <- ZIO.attempt(RangeInput.encode(Range(-1, 5)))
result <- ZIO.attempt(RangeInput.encode(-1 to 5))
} yield assert(result)(equalTo(RespCommand(Value("-1"), Value("5"))))
},
test("with positive start and negative end") {
for {
result <- ZIO.attempt(RangeInput.encode(Range(1, -5)))
result <- ZIO.attempt(RangeInput.encode(1 to -5))
} yield assert(result)(equalTo(RespCommand(Value("1"), Value("-5"))))
},
test("with negative start and negative end") {
for {
result <- ZIO.attempt(RangeInput.encode(Range(-1, -5)))
result <- ZIO.attempt(RangeInput.encode(-1 to -5))
} yield assert(result)(equalTo(RespCommand(Value("-1"), Value("-5"))))
},
test("with positive start and exclusive positive end") {
for {
result <- ZIO.attempt(RangeInput.encode(1 until 3))
} yield assert(result)(equalTo(RespCommand(Value("1"), Value("2"))))
},
test("with positive start and exclusive negative end") {
for {
result <- ZIO.attempt(RangeInput.encode(1 until -1))
} yield assert(result)(equalTo(RespCommand(Value("1"), Value("-2"))))
},
test("with negative start and exclusive positive end") {
for {
result <- ZIO.attempt(RangeInput.encode(-5 until 8))
} yield assert(result)(equalTo(RespCommand(Value("-5"), Value("7"))))
},
test("with negative start and exclusive negative end") {
for {
result <- ZIO.attempt(RangeInput.encode(-5 until -3))
} yield assert(result)(equalTo(RespCommand(Value("-5"), Value("-4"))))
}
),
suite("Pattern")(
Expand Down