|
| 1 | +@testable import NIORedis |
| 2 | +import XCTest |
| 3 | + |
| 4 | +final class HashCommandsTests: 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_hset() throws { |
| 25 | + var result = try connection.hset(#function, field: "test", to: "\(#line)").wait() |
| 26 | + XCTAssertTrue(result) |
| 27 | + result = try connection.hset(#function, field: "test", to: "\(#line)").wait() |
| 28 | + XCTAssertFalse(result) |
| 29 | + } |
| 30 | + |
| 31 | + func test_hmset() throws { |
| 32 | + XCTAssertNoThrow(try connection.hmset(#function, to: ["field": "30"]).wait()) |
| 33 | + let value = try connection.hget(#function, field: "field").wait() |
| 34 | + XCTAssertEqual(value, "30") |
| 35 | + } |
| 36 | + |
| 37 | + func test_hsetnx() throws { |
| 38 | + var success = try connection.hsetnx(#function, field: "field", to: "foo").wait() |
| 39 | + XCTAssertTrue(success) |
| 40 | + success = try connection.hsetnx(#function, field: "field", to: "30").wait() |
| 41 | + XCTAssertFalse(success) |
| 42 | + |
| 43 | + let value = try connection.hget(#function, field: "field").wait() |
| 44 | + XCTAssertEqual(value, "foo") |
| 45 | + } |
| 46 | + |
| 47 | + func test_hget() throws { |
| 48 | + _ = try connection.hset(#function, field: "test", to: "30").wait() |
| 49 | + let value = try connection.hget(#function, field: "test").wait() |
| 50 | + XCTAssertEqual(value, "30") |
| 51 | + } |
| 52 | + |
| 53 | + func test_hmget() throws { |
| 54 | + _ = try connection.hmset(#function, to: ["first": "foo", "second": "bar"]).wait() |
| 55 | + let values = try connection.hmget(#function, fields: ["first", "second", "fake"]).wait() |
| 56 | + XCTAssertEqual(values[0], "foo") |
| 57 | + XCTAssertEqual(values[1], "bar") |
| 58 | + XCTAssertNil(values[2]) |
| 59 | + } |
| 60 | + |
| 61 | + func test_hgetall() throws { |
| 62 | + let dataset = ["first": "foo", "second": "bar"] |
| 63 | + _ = try connection.hmset(#function, to: dataset).wait() |
| 64 | + let hashes = try connection.hgetall(from: #function).wait() |
| 65 | + XCTAssertEqual(hashes, dataset) |
| 66 | + } |
| 67 | + |
| 68 | + func test_hdel() throws { |
| 69 | + _ = try connection.hmset(#function, to: ["first": "foo", "second": "bar"]).wait() |
| 70 | + let count = try connection.hdel(#function, fields: ["first", "second", "fake"]).wait() |
| 71 | + XCTAssertEqual(count, 2) |
| 72 | + } |
| 73 | + |
| 74 | + func test_hexists() throws { |
| 75 | + var exists = try connection.hexists(#function, field: "foo").wait() |
| 76 | + XCTAssertFalse(exists) |
| 77 | + _ = try connection.hset(#function, field: "foo", to: "\(#line)").wait() |
| 78 | + exists = try connection.hexists(#function, field: "foo").wait() |
| 79 | + XCTAssertTrue(exists) |
| 80 | + } |
| 81 | + |
| 82 | + func test_hlen() throws { |
| 83 | + var count = try connection.hlen(of: #function).wait() |
| 84 | + XCTAssertEqual(count, 0) |
| 85 | + _ = try connection.hset(#function, field: "first", to: "\(#line)").wait() |
| 86 | + count = try connection.hlen(of: #function).wait() |
| 87 | + XCTAssertEqual(count, 1) |
| 88 | + _ = try connection.hset(#function, field: "second", to: "\(#line)").wait() |
| 89 | + count = try connection.hlen(of: #function).wait() |
| 90 | + XCTAssertEqual(count, 2) |
| 91 | + } |
| 92 | + |
| 93 | + func test_hstrlen() throws { |
| 94 | + _ = try connection.hset(#function, field: "first", to: "foo").wait() |
| 95 | + var size = try connection.hstrlen(of: #function, field: "first").wait() |
| 96 | + XCTAssertEqual(size, 3) |
| 97 | + _ = try connection.hset(#function, field: "second", to: "300").wait() |
| 98 | + size = try connection.hstrlen(of: #function, field: "second").wait() |
| 99 | + XCTAssertEqual(size, 3) |
| 100 | + } |
| 101 | + |
| 102 | + func test_hkeys() throws { |
| 103 | + let dataset = [ |
| 104 | + "first": "3", |
| 105 | + "second": "foo" |
| 106 | + ] |
| 107 | + _ = try connection.hmset(#function, to: dataset).wait() |
| 108 | + let keys = try connection.hkeys(storedAt: #function).wait() |
| 109 | + XCTAssertEqual(Array(dataset.keys), keys) |
| 110 | + } |
| 111 | + |
| 112 | + func test_hvals() throws { |
| 113 | + let dataset = [ |
| 114 | + "first": "3", |
| 115 | + "second": "foo" |
| 116 | + ] |
| 117 | + _ = try connection.hmset(#function, to: dataset).wait() |
| 118 | + let values = try connection.hvals(storedAt: #function).wait() |
| 119 | + XCTAssertEqual(Array(dataset.values), values) |
| 120 | + } |
| 121 | + |
| 122 | + func test_hincrby() throws { |
| 123 | + _ = try connection.hset(#function, field: "first", to: "3").wait() |
| 124 | + var value = try connection.hincrby(#function, field: "first", by: 10).wait() |
| 125 | + XCTAssertEqual(value, 13) |
| 126 | + value = try connection.hincrby(#function, field: "first", by: -15).wait() |
| 127 | + XCTAssertEqual(value, -2) |
| 128 | + } |
| 129 | + |
| 130 | + func test_hincrbyfloat() throws { |
| 131 | + _ = try connection.hset(#function, field: "first", to: "3.14").wait() |
| 132 | + |
| 133 | + let double = try connection.hincrbyfloat(#function, field: "first", by: Double(3.14)).wait() |
| 134 | + XCTAssertEqual(double, 6.28) |
| 135 | + |
| 136 | + let float = try connection.hincrbyfloat(#function, field: "first", by: Float(-10.23523)).wait() |
| 137 | + XCTAssertEqual(float, -3.95523) |
| 138 | + } |
| 139 | + |
| 140 | + func test_hscan() throws { |
| 141 | + var dataset: [String: String] = [:] |
| 142 | + for index in 1...15 { |
| 143 | + let key = "key\(index)\(index % 2 == 0 ? "_even" : "_odd")" |
| 144 | + dataset[key] = "\(index)" |
| 145 | + } |
| 146 | + _ = try connection.hmset(#function, to: dataset).wait() |
| 147 | + |
| 148 | + var (cursor, fields) = try connection.hscan(#function, count: 5).wait() |
| 149 | + XCTAssertGreaterThanOrEqual(cursor, 0) |
| 150 | + XCTAssertGreaterThanOrEqual(fields.count, 5) |
| 151 | + |
| 152 | + (_, fields) = try connection.hscan(#function, atPosition: cursor, count: 8).wait() |
| 153 | + XCTAssertGreaterThanOrEqual(fields.count, 8) |
| 154 | + |
| 155 | + (cursor, fields) = try connection.hscan(#function, matching: "*_odd").wait() |
| 156 | + XCTAssertGreaterThanOrEqual(cursor, 0) |
| 157 | + XCTAssertGreaterThanOrEqual(fields.count, 1) |
| 158 | + XCTAssertLessThanOrEqual(fields.count, 8) |
| 159 | + |
| 160 | + (cursor, fields) = try connection.hscan(#function, matching: "*_ev*").wait() |
| 161 | + XCTAssertGreaterThanOrEqual(cursor, 0) |
| 162 | + XCTAssertGreaterThanOrEqual(fields.count, 1) |
| 163 | + XCTAssertLessThanOrEqual(fields.count, 7) |
| 164 | + } |
| 165 | + |
| 166 | + static var allTests = [ |
| 167 | + ("test_hset", test_hset), |
| 168 | + ("test_hmset", test_hmset), |
| 169 | + ("test_hsetnx", test_hsetnx), |
| 170 | + ("test_hget", test_hget), |
| 171 | + ("test_hmget", test_hmget), |
| 172 | + ("test_hgetall", test_hgetall), |
| 173 | + ("test_hdel", test_hdel), |
| 174 | + ("test_hexists", test_hexists), |
| 175 | + ("test_hlen", test_hlen), |
| 176 | + ("test_hstrlen", test_hstrlen), |
| 177 | + ("test_hkeys", test_hkeys), |
| 178 | + ("test_hvals", test_hvals), |
| 179 | + ("test_hincrby", test_hincrby), |
| 180 | + ("test_hincrbyfloat", test_hincrbyfloat), |
| 181 | + ("test_hscan", test_hscan), |
| 182 | + ] |
| 183 | +} |
0 commit comments