Skip to content

Commit 16dc4ac

Browse files
committed
Updated unit tests to use new syntax and to remove a bunch of optional crud
1 parent f4e646c commit 16dc4ac

7 files changed

+393
-365
lines changed

Tests/NIORedisTests/Commands/BasicCommandsTests.swift

Lines changed: 34 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ final class BasicCommandsTests: XCTestCase {
55
private let redis = RedisDriver(ownershipModel: .internal(threadCount: 1))
66
deinit { try? redis.terminate() }
77

8-
private var connection: RedisConnection?
8+
private var connection: RedisConnection!
99

1010
override func setUp() {
1111
do {
@@ -16,222 +16,113 @@ final class BasicCommandsTests: XCTestCase {
1616
}
1717

1818
override func tearDown() {
19-
_ = try? connection?.send(command: "FLUSHALL").wait()
20-
connection?.close()
19+
_ = try? connection.send(command: "FLUSHALL").wait()
20+
connection.close()
2121
connection = nil
2222
}
2323

2424
func test_select() {
25-
XCTAssertNoThrow(try connection?.select(database: 3).wait())
25+
XCTAssertNoThrow(try connection.select(database: 3).wait())
2626
}
2727

2828
func test_delete() throws {
2929
let keys = [ #function + "1", #function + "2", #function + "3" ]
30-
try connection?.set(keys[0], to: "value").wait()
31-
try connection?.set(keys[1], to: "value").wait()
32-
try connection?.set(keys[2], to: "value").wait()
30+
try connection.set(keys[0], to: "value").wait()
31+
try connection.set(keys[1], to: "value").wait()
32+
try connection.set(keys[2], to: "value").wait()
3333

34-
let first = try connection?.delete(keys[0]).wait()
34+
let first = try connection.delete([keys[0]]).wait()
3535
XCTAssertEqual(first, 1)
3636

37-
let second = try connection?.delete(keys[0]).wait()
37+
let second = try connection.delete([keys[0]]).wait()
3838
XCTAssertEqual(second, 0)
3939

40-
let third = try connection?.delete(keys[1], keys[2]).wait()
40+
let third = try connection.delete([keys[1], keys[2]]).wait()
4141
XCTAssertEqual(third, 2)
4242
}
4343

44-
func test_set() {
45-
XCTAssertNoThrow(try connection?.set(#function, to: "value").wait())
46-
}
47-
48-
func test_get() throws {
49-
try connection?.set(#function, to: "value").wait()
50-
let result = try connection?.get(#function).wait()
51-
XCTAssertEqual(result, "value")
52-
}
53-
5444
func test_expire() throws {
55-
try connection?.set(#function, to: "value").wait()
56-
let before = try connection?.get(#function).wait()
45+
try connection.set(#function, to: "value").wait()
46+
let before = try connection.get(#function).wait()
5747
XCTAssertNotNil(before)
5848

59-
let result = try connection?.expire(#function, after: 0).wait()
49+
let result = try connection.expire(#function, after: .nanoseconds(1)).wait()
6050
XCTAssertEqual(result, true)
6151

62-
let after = try connection?.get(#function).wait()
52+
let after = try connection.get(#function).wait()
6353
XCTAssertNil(after)
6454
}
6555

6656
func test_ping() throws {
67-
let first = try connection?.ping().wait()
57+
let first = try connection.ping().wait()
6858
XCTAssertEqual(first, "PONG")
6959

70-
let second = try connection?.ping(with: "My message").wait()
60+
let second = try connection.ping(with: "My message").wait()
7161
XCTAssertEqual(second, "My message")
7262
}
7363

7464
func test_echo() throws {
75-
let response = try connection?.echo("FIZZ_BUZZ").wait()
65+
let response = try connection.echo("FIZZ_BUZZ").wait()
7666
XCTAssertEqual(response, "FIZZ_BUZZ")
7767
}
7868

79-
func test_swapdb() throws {
80-
try connection?.set("first", to: "3").wait()
81-
var first = try connection?.get("first").wait()
69+
func test_swapDatabase() throws {
70+
try connection.set("first", to: "3").wait()
71+
var first = try connection.get("first").wait()
8272
XCTAssertEqual(first, "3")
8373

84-
try connection?.select(database: 1).wait()
85-
var second = try connection?.get("first").wait()
74+
try connection.select(database: 1).wait()
75+
var second = try connection.get("first").wait()
8676
XCTAssertEqual(second, nil)
8777

88-
try connection?.set("second", to: "100").wait()
89-
second = try connection?.get("second").wait()
78+
try connection.set("second", to: "100").wait()
79+
second = try connection.get("second").wait()
9080
XCTAssertEqual(second, "100")
9181

92-
let success = try connection?.swapdb(firstIndex: 0, secondIndex: 1).wait()
82+
let success = try connection.swapDatabase(0, with: 1).wait()
9383
XCTAssertEqual(success, true)
9484

95-
second = try connection?.get("first").wait()
85+
second = try connection.get("first").wait()
9686
XCTAssertEqual(second, "3")
9787

98-
try connection?.select(database: 0).wait()
99-
first = try connection?.get("second").wait()
88+
try connection.select(database: 0).wait()
89+
first = try connection.get("second").wait()
10090
XCTAssertEqual(first, "100")
10191
}
10292

103-
func test_increment() throws {
104-
var result = try connection?.increment(#function).wait()
105-
XCTAssertEqual(result, 1)
106-
result = try connection?.increment(#function).wait()
107-
XCTAssertEqual(result, 2)
108-
}
109-
110-
func test_incrementBy() throws {
111-
var result = try connection?.increment(#function, by: 10).wait()
112-
XCTAssertEqual(result, 10)
113-
result = try connection?.increment(#function, by: -3).wait()
114-
XCTAssertEqual(result, 7)
115-
result = try connection?.increment(#function, by: 0).wait()
116-
XCTAssertEqual(result, 7)
117-
}
118-
119-
func test_incrementByFloat() throws {
120-
var float = try connection?.increment(#function, by: Float(3.0)).wait()
121-
XCTAssertEqual(float, 3.0)
122-
float = try connection?.increment(#function, by: Float(-10.135901)).wait()
123-
XCTAssertEqual(float, -7.135901)
124-
125-
var double = try connection?.increment(#function, by: Double(10.2839)).wait()
126-
XCTAssertEqual(double, 3.147999)
127-
double = try connection?.increment(#function, by: Double(15.2938)).wait()
128-
XCTAssertEqual(double, 18.441799)
129-
}
130-
131-
func test_decrement() throws {
132-
var result = try connection?.decrement(#function).wait()
133-
XCTAssertEqual(result, -1)
134-
result = try connection?.decrement(#function).wait()
135-
XCTAssertEqual(result, -2)
136-
}
137-
138-
func test_decrementBy() throws {
139-
var result = try connection?.decrement(#function, by: -10).wait()
140-
XCTAssertEqual(result, 10)
141-
result = try connection?.decrement(#function, by: 3).wait()
142-
XCTAssertEqual(result, 7)
143-
result = try connection?.decrement(#function, by: 0).wait()
144-
XCTAssertEqual(result, 7)
145-
}
146-
147-
func test_mget() throws {
148-
let keys = ["one", "two"]
149-
try keys.forEach { _ = try connection?.set($0, to: $0).wait() }
150-
151-
let values = try connection?.mget(keys + ["empty"]).wait()
152-
XCTAssertEqual(values?.count, 3)
153-
XCTAssertEqual(values?[0].string, "one")
154-
XCTAssertEqual(values?[1].string, "two")
155-
XCTAssertEqual(values?[2].isNull, true)
156-
157-
XCTAssertEqual(try connection?.mget(["empty", #function]).wait().count, 2)
158-
}
159-
160-
func test_mset() throws {
161-
let data = [
162-
"first": 1,
163-
"second": 2
164-
]
165-
XCTAssertNoThrow(try connection?.mset(data).wait())
166-
let values = try connection?.mget(["first", "second"]).wait().compactMap { $0.string }
167-
XCTAssertEqual(values?.count, 2)
168-
XCTAssertEqual(values?[0], "1")
169-
XCTAssertEqual(values?[1], "2")
170-
171-
XCTAssertNoThrow(try connection?.mset(["first": 10]).wait())
172-
let val = try connection?.get("first").wait()
173-
XCTAssertEqual(val, "10")
174-
}
175-
176-
func test_msetnx() throws {
177-
let data = [
178-
"first": 1,
179-
"second": 2
180-
]
181-
var success = try connection?.msetnx(data).wait()
182-
XCTAssertEqual(success, true)
183-
184-
success = try connection?.msetnx(["first": 10, "second": 20]).wait()
185-
XCTAssertEqual(success, false)
186-
187-
let values = try connection?.mget(["first", "second"]).wait().compactMap { $0.string }
188-
XCTAssertEqual(values?[0], "1")
189-
XCTAssertEqual(values?[1], "2")
190-
}
191-
19293
func test_scan() throws {
19394
var dataset: [String] = .init(repeating: "", count: 10)
19495
for index in 1...15 {
19596
let key = "key\(index)\(index % 2 == 0 ? "_even" : "_odd")"
19697
dataset.append(key)
197-
_ = try connection?.set(key, to: "\(index)").wait()
98+
_ = try connection.set(key, to: "\(index)").wait()
19899
}
199100

200-
var (cursor, keys) = try connection?.scan(count: 5).wait() ?? (0, [])
101+
var (cursor, keys) = try connection.scan(count: 5).wait()
201102
XCTAssertGreaterThanOrEqual(cursor, 0)
202103
XCTAssertGreaterThanOrEqual(keys.count, 5)
203104

204-
(_, keys) = try connection?.scan(startingFrom: cursor, count: 8).wait() ?? (0, [])
105+
(_, keys) = try connection.scan(startingFrom: cursor, count: 8).wait()
205106
XCTAssertGreaterThanOrEqual(keys.count, 8)
206107

207-
(cursor, keys) = try connection?.scan(matching: "*_odd").wait() ?? (0, [])
108+
(cursor, keys) = try connection.scan(matching: "*_odd").wait()
208109
XCTAssertGreaterThanOrEqual(cursor, 0)
209110
XCTAssertGreaterThanOrEqual(keys.count, 1)
210111
XCTAssertLessThanOrEqual(keys.count, 7)
211112

212-
(cursor, keys) = try connection?.scan(matching: "*_even*").wait() ?? (0, [])
113+
(cursor, keys) = try connection.scan(matching: "*_even*").wait()
213114
XCTAssertGreaterThanOrEqual(cursor, 0)
214115
XCTAssertGreaterThanOrEqual(keys.count, 1)
215116
XCTAssertLessThanOrEqual(keys.count, 7)
216117
}
217118

218119
static var allTests = [
219120
("test_select", test_select),
220-
("test_set", test_set),
221-
("test_get", test_get),
222121
("test_expire", test_expire),
223122
("test_delete", test_delete),
224123
("test_ping", test_ping),
225124
("test_echo", test_echo),
226-
("test_swapdb", test_swapdb),
227-
("test_increment", test_increment),
228-
("test_incrementBy", test_incrementBy),
229-
("test_incrementByFloat", test_incrementByFloat),
230-
("test_decrement", test_decrement),
231-
("test_decrementBy", test_decrementBy),
232-
("test_mget", test_mget),
233-
("test_mset", test_mset),
234-
("test_msetnx", test_msetnx),
125+
("test_swapDatabase", test_swapDatabase),
235126
("test_scan", test_scan),
236127
]
237128
}

0 commit comments

Comments
 (0)