Skip to content

Commit b277715

Browse files
committed
104 -- Use correct base method for zrevrange overload methods
1 parent 16037bb commit b277715

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

Sources/RediStack/Commands/SortedSetCommands.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the RediStack open source project
44
//
5-
// Copyright (c) 2019 RediStack project authors
5+
// Copyright (c) 2019-2022 RediStack project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -1121,7 +1121,7 @@ extension RedisClient {
11211121
indices range: ClosedRange<Int>,
11221122
includeScoresInResponse includeScores: Bool = false
11231123
) -> EventLoopFuture<[RESPValue]> {
1124-
return self.zrange(from: key, firstIndex: range.lowerBound, lastIndex: range.upperBound, includeScoresInResponse: includeScores)
1124+
return self.zrevrange(from: key, firstIndex: range.lowerBound, lastIndex: range.upperBound, includeScoresInResponse: includeScores)
11251125
}
11261126

11271127
/// Gets all the elements from a SortedSet starting with the first index bound up to, but not including, the element at the last index bound.
@@ -1163,7 +1163,7 @@ extension RedisClient {
11631163
indices range: Range<Int>,
11641164
includeScoresInResponse includeScores: Bool = false
11651165
) -> EventLoopFuture<[RESPValue]> {
1166-
return self.zrange(from: key, firstIndex: range.lowerBound, lastIndex: range.upperBound - 1, includeScoresInResponse: includeScores)
1166+
return self.zrevrange(from: key, firstIndex: range.lowerBound, lastIndex: range.upperBound - 1, includeScoresInResponse: includeScores)
11671167
}
11681168

11691169
/// Gets all elements from the index specified to the end of a SortedSet.
@@ -1191,7 +1191,7 @@ extension RedisClient {
11911191
fromIndex index: Int,
11921192
includeScoresInResponse includeScores: Bool = false
11931193
) -> EventLoopFuture<[RESPValue]> {
1194-
return self.zrange(from: key, firstIndex: index, lastIndex: -1, includeScoresInResponse: includeScores)
1194+
return self.zrevrange(from: key, firstIndex: index, lastIndex: -1, includeScoresInResponse: includeScores)
11951195
}
11961196

11971197
/// Gets all elements from the start of a SortedSet up to, and including, the element at the index specified.
@@ -1219,7 +1219,7 @@ extension RedisClient {
12191219
throughIndex index: Int,
12201220
includeScoresInResponse includeScores: Bool = false
12211221
) -> EventLoopFuture<[RESPValue]> {
1222-
return self.zrange(from: key, firstIndex: 0, lastIndex: index, includeScoresInResponse: includeScores)
1222+
return self.zrevrange(from: key, firstIndex: 0, lastIndex: index, includeScoresInResponse: includeScores)
12231223
}
12241224

12251225
/// Gets all elements from the start of a SortedSet up to, but not including, the element at the index specified.
@@ -1247,7 +1247,7 @@ extension RedisClient {
12471247
upToIndex index: Int,
12481248
includeScoresInResponse includeScores: Bool = false
12491249
) -> EventLoopFuture<[RESPValue]> {
1250-
return self.zrange(from: key, firstIndex: 0, lastIndex: index - 1, includeScoresInResponse: includeScores)
1250+
return self.zrevrange(from: key, firstIndex: 0, lastIndex: index - 1, includeScoresInResponse: includeScores)
12511251
}
12521252

12531253
func _zrange(

Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the RediStack open source project
44
//
5-
// Copyright (c) 2019 RediStack project authors
5+
// Copyright (c) 2019-2022 RediStack project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -518,3 +518,57 @@ final class SortedSetCommandsTests: RediStackIntegrationTestCase {
518518
XCTAssertEqual(count, 1)
519519
}
520520
}
521+
522+
// MARK: - #104 zrevrange & zrange bug
523+
524+
extension SortedSetCommandsTests {
525+
func test_zrange_realworld() throws {
526+
struct Keys {
527+
static let first = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0185"
528+
static let second = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0186"
529+
static let third = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0187"
530+
static let fourth = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0188"
531+
static let fifth = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0189"
532+
}
533+
_ = try self.connection.zadd([
534+
(Keys.first, 1),
535+
(Keys.second, 1),
536+
(Keys.third, 1),
537+
(Keys.fourth, 1),
538+
(Keys.fifth, 1),
539+
], to: #function).wait()
540+
541+
let elements = try self.connection
542+
.zrange(from: #function, fromIndex: 0)
543+
.wait()
544+
.compactMap { $0.string }
545+
546+
XCTAssertEqual(elements.count, 5)
547+
XCTAssertEqual(elements, elements.sorted(by: <))
548+
}
549+
550+
func test_zrevrange_realworld() throws {
551+
struct Keys {
552+
static let first = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0185"
553+
static let second = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0186"
554+
static let third = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0187"
555+
static let fourth = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0188"
556+
static let fifth = "1E4FD2C5-C32E-4E3F-91B3-45478BCF0189"
557+
}
558+
_ = try self.connection.zadd([
559+
(Keys.first, 1),
560+
(Keys.second, 1),
561+
(Keys.third, 1),
562+
(Keys.fourth, 1),
563+
(Keys.fifth, 1),
564+
], to: #function).wait()
565+
566+
let elements = try self.connection
567+
.zrevrange(from: #function, fromIndex: 0)
568+
.wait()
569+
.compactMap { $0.string }
570+
571+
XCTAssertEqual(elements.count, 5)
572+
XCTAssertEqual(elements, elements.sorted(by: >))
573+
}
574+
}

0 commit comments

Comments
 (0)