|
| 1 | +@testable import NIORedis |
| 2 | +import XCTest |
| 3 | + |
| 4 | +final class ListCommandsTests: XCTestCase { |
| 5 | + private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1)) |
| 6 | + deinit { try? redis.terminate() } |
| 7 | + |
| 8 | + private var connection: RedisConnection! |
| 9 | + |
| 10 | + override func setUp() { |
| 11 | + do { |
| 12 | + connection = try redis.makeConnection().wait() |
| 13 | + } catch { |
| 14 | + XCTFail("Failed to create NIORedisConnection!") |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + override func tearDown() { |
| 19 | + _ = try? connection.send(command: "FLUSHALL").wait() |
| 20 | + connection.close() |
| 21 | + connection = nil |
| 22 | + } |
| 23 | + |
| 24 | + func test_llen() throws { |
| 25 | + var length = try connection.llen(of: #function).wait() |
| 26 | + XCTAssertEqual(length, 0) |
| 27 | + _ = try connection.lpush([30], to: #function).wait() |
| 28 | + length = try connection.llen(of: #function).wait() |
| 29 | + XCTAssertEqual(length, 1) |
| 30 | + } |
| 31 | + |
| 32 | + func test_lindex() throws { |
| 33 | + XCTAssertThrowsError(try connection.lindex(#function, index: 0).wait()) |
| 34 | + _ = try connection.lpush([10], to: #function).wait() |
| 35 | + let element = try connection.lindex(#function, index: 0).wait() |
| 36 | + XCTAssertEqual(Int(element), 10) |
| 37 | + } |
| 38 | + |
| 39 | + func test_lset() throws { |
| 40 | + XCTAssertThrowsError(try connection.lset(#function, index: 0, to: 30).wait()) |
| 41 | + _ = try connection.lpush([10], to: #function).wait() |
| 42 | + XCTAssertNoThrow(try connection.lset(#function, index: 0, to: 30).wait()) |
| 43 | + let element = try connection.lindex(#function, index: 0).wait() |
| 44 | + XCTAssertEqual(Int(element), 30) |
| 45 | + } |
| 46 | + |
| 47 | + func test_lrem() throws { |
| 48 | + _ = try connection.lpush([10, 10, 20, 30, 10], to: #function).wait() |
| 49 | + var count = try connection.lrem(10, from: #function, count: 2).wait() |
| 50 | + XCTAssertEqual(count, 2) |
| 51 | + count = try connection.lrem(10, from: #function, count: 2).wait() |
| 52 | + XCTAssertEqual(count, 1) |
| 53 | + } |
| 54 | + |
| 55 | + func test_lrange() throws { |
| 56 | + var elements = try connection.lrange(of: #function, startIndex: 0, endIndex: 10).wait() |
| 57 | + XCTAssertEqual(elements.count, 0) |
| 58 | + |
| 59 | + _ = try connection.lpush([5, 4, 3, 2, 1], to: #function).wait() |
| 60 | + |
| 61 | + elements = try connection.lrange(of: #function, startIndex: 0, endIndex: 4).wait() |
| 62 | + XCTAssertEqual(elements.count, 5) |
| 63 | + XCTAssertEqual(Int(elements[0]), 1) |
| 64 | + XCTAssertEqual(Int(elements[4]), 5) |
| 65 | + |
| 66 | + elements = try connection.lrange(of: #function, startIndex: 2, endIndex: 0).wait() |
| 67 | + XCTAssertEqual(elements.count, 0) |
| 68 | + |
| 69 | + elements = try connection.lrange(of: #function, startIndex: 4, endIndex: 5).wait() |
| 70 | + XCTAssertEqual(elements.count, 1) |
| 71 | + |
| 72 | + elements = try connection.lrange(of: #function, startIndex: 0, endIndex: -4).wait() |
| 73 | + XCTAssertEqual(elements.count, 2) |
| 74 | + } |
| 75 | + |
| 76 | + func test_rpoplpush() throws { |
| 77 | + _ = try connection.lpush([10], to: "first").wait() |
| 78 | + _ = try connection.lpush([30], to: "second").wait() |
| 79 | + |
| 80 | + var element = try connection.rpoplpush(from: "first", to: "second").wait() |
| 81 | + XCTAssertEqual(Int(element), 10) |
| 82 | + XCTAssertEqual(try connection.llen(of: "first").wait(), 0) |
| 83 | + XCTAssertEqual(try connection.llen(of: "second").wait(), 2) |
| 84 | + |
| 85 | + element = try connection.rpoplpush(from: "second", to: "first").wait() |
| 86 | + XCTAssertEqual(Int(element), 30) |
| 87 | + XCTAssertEqual(try connection.llen(of: "second").wait(), 1) |
| 88 | + } |
| 89 | + |
| 90 | + func test_linsert() throws { |
| 91 | + _ = try connection.lpush([10], to: #function).wait() |
| 92 | + |
| 93 | + _ = try connection.linsert(20, into: #function, after: 10).wait() |
| 94 | + var elements = try connection.lrange(of: #function, startIndex: 0, endIndex: 1) |
| 95 | + .map { response in response.compactMap { Int($0) } } |
| 96 | + .wait() |
| 97 | + XCTAssertEqual(elements, [10, 20]) |
| 98 | + |
| 99 | + _ = try connection.linsert(30, into: #function, before: 10).wait() |
| 100 | + elements = try connection.lrange(of: #function, startIndex: 0, endIndex: 2) |
| 101 | + .map { response in response.compactMap { Int($0) } } |
| 102 | + .wait() |
| 103 | + XCTAssertEqual(elements, [30, 10, 20]) |
| 104 | + } |
| 105 | + |
| 106 | + func test_lpop() throws { |
| 107 | + _ = try connection.lpush([10, 20, 30], to: #function).wait() |
| 108 | + |
| 109 | + let element = try connection.lpop(from: #function).wait() |
| 110 | + XCTAssertNotNil(element) |
| 111 | + XCTAssertEqual(Int(element ?? .null), 30) |
| 112 | + } |
| 113 | + |
| 114 | + func test_lpush() throws { |
| 115 | + _ = try connection.rpush([10, 20, 30], to: #function).wait() |
| 116 | + |
| 117 | + let size = try connection.lpush([100], to: #function).wait() |
| 118 | + let element = try connection.lindex(#function, index: 0).mapFromRESP(to: Int.self).wait() |
| 119 | + XCTAssertEqual(size, 4) |
| 120 | + XCTAssertEqual(element, 100) |
| 121 | + } |
| 122 | + |
| 123 | + func test_lpushx() throws { |
| 124 | + var size = try connection.lpushx(10, to: #function).wait() |
| 125 | + XCTAssertEqual(size, 0) |
| 126 | + |
| 127 | + _ = try connection.lpush([10], to: #function).wait() |
| 128 | + |
| 129 | + size = try connection.lpushx(30, to: #function).wait() |
| 130 | + XCTAssertEqual(size, 2) |
| 131 | + let element = try connection.rpop(from: #function) |
| 132 | + .map { return Int($0 ?? .null) } |
| 133 | + .wait() |
| 134 | + XCTAssertEqual(element, 10) |
| 135 | + } |
| 136 | + |
| 137 | + func test_rpop() throws { |
| 138 | + _ = try connection.lpush([10, 20, 30], to: #function).wait() |
| 139 | + |
| 140 | + let element = try connection.rpop(from: #function).wait() |
| 141 | + XCTAssertNotNil(element) |
| 142 | + XCTAssertEqual(Int(element ?? .null), 10) |
| 143 | + } |
| 144 | + |
| 145 | + func test_rpush() throws { |
| 146 | + _ = try connection.lpush([10, 20, 30], to: #function).wait() |
| 147 | + |
| 148 | + let size = try connection.rpush([100], to: #function).wait() |
| 149 | + let element = try connection.lindex(#function, index: 3).mapFromRESP(to: Int.self).wait() |
| 150 | + XCTAssertEqual(size, 4) |
| 151 | + XCTAssertEqual(element, 100) |
| 152 | + } |
| 153 | + |
| 154 | + func test_rpushx() throws { |
| 155 | + var size = try connection.rpushx(10, to: #function).wait() |
| 156 | + XCTAssertEqual(size, 0) |
| 157 | + |
| 158 | + _ = try connection.rpush([10], to: #function).wait() |
| 159 | + |
| 160 | + size = try connection.rpushx(30, to: #function).wait() |
| 161 | + XCTAssertEqual(size, 2) |
| 162 | + let element = try connection.lpop(from: #function) |
| 163 | + .map { return Int($0 ?? .null) } |
| 164 | + .wait() |
| 165 | + XCTAssertEqual(element, 10) |
| 166 | + } |
| 167 | + |
| 168 | + static var allTests = [ |
| 169 | + ("test_llen", test_llen), |
| 170 | + ("test_lindex", test_lindex), |
| 171 | + ("test_lset", test_lset), |
| 172 | + ("test_lrem", test_lrem), |
| 173 | + ("test_lrange", test_lrange), |
| 174 | + ("test_rpoplpush", test_rpoplpush), |
| 175 | + ("test_linsert", test_linsert), |
| 176 | + ("test_lpop", test_lpop), |
| 177 | + ("test_lpush", test_lpush), |
| 178 | + ("test_lpushx", test_lpushx), |
| 179 | + ("test_rpop", test_rpop), |
| 180 | + ("test_rpush", test_rpush), |
| 181 | + ("test_rpushx", test_rpushx), |
| 182 | + ] |
| 183 | +} |
0 commit comments