Skip to content

Commit 4f9f461

Browse files
committed
Updated test suite to use throws test APIs and removed useless assertion strings.
1 parent 4903247 commit 4f9f461

20 files changed

+2996
-3441
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ All notable changes to this project will be documented in this file.
3838

3939
- A new `TableLockPolicy` to handle table lock errors on `execute`, `prepare`, and `step` operations.
4040

41+
#### Updated
42+
43+
- The test suite by replacing `do-catch` implementations with `throws` test API variants and unhelpful assertion strings.
44+
4145
---
4246

4347
## [3.1.0](https://github.com/Nike-Inc/SQift/releases/tag/3.1.0)

Tests/Tests/Connection/ConnectionPoolTests.swift

+116-140
Original file line numberDiff line numberDiff line change
@@ -42,99 +42,83 @@ class ConnectionPoolTestCase: BaseTestCase {
4242
XCTFail("Execution should not reach this point")
4343
} catch let error as SQLiteError {
4444
// Then
45-
XCTAssertEqual(error.code, SQLITE_CANTOPEN, "error code should be `SQLITE_CANTOPEN`")
45+
XCTAssertEqual(error.code, SQLITE_CANTOPEN)
4646
} catch {
4747
XCTFail("Failed with an unknown error type: \(error)")
4848
}
4949
}
5050

51-
func testThatDequeueingConnectionCreatesNewConnection() {
52-
do {
53-
// Given
54-
let pool = ConnectionPool(storageLocation: storageLocation)
55-
pool.availableConnections.removeAll()
51+
func testThatDequeueingConnectionCreatesNewConnection() throws {
52+
// Given
53+
let pool = ConnectionPool(storageLocation: storageLocation)
54+
pool.availableConnections.removeAll()
5655

57-
// When
58-
let connection = try pool.dequeueConnectionForUse()
56+
// When
57+
let connection = try pool.dequeueConnectionForUse()
5958

60-
// Then
61-
XCTAssertFalse(pool.availableConnections.contains(connection), "available connections should not contain connection")
62-
XCTAssertTrue(pool.busyConnections.contains(connection), "busy connections should contain connection")
63-
} catch {
64-
XCTFail("Test encountered unexpected error: \(error)")
65-
}
59+
// Then
60+
XCTAssertFalse(pool.availableConnections.contains(connection))
61+
XCTAssertTrue(pool.busyConnections.contains(connection))
6662
}
6763

68-
func testThatDequeueingConnectionCreatesNewConnectionAndExecutesPreparationClosure() {
69-
do {
70-
// Given
71-
let pool = ConnectionPool(
72-
storageLocation: storageLocation,
73-
connectionPreparation: { connection in
74-
try connection.execute("PRAGMA synchronous = 1")
75-
}
76-
)
64+
func testThatDequeueingConnectionCreatesNewConnectionAndExecutesPreparationClosure() throws {
65+
// Given
66+
let pool = ConnectionPool(
67+
storageLocation: storageLocation,
68+
connectionPreparation: { connection in
69+
try connection.execute("PRAGMA synchronous = 1")
70+
}
71+
)
7772

78-
pool.availableConnections.removeAll()
73+
pool.availableConnections.removeAll()
7974

80-
var synchronous: Int?
75+
var synchronous: Int?
8176

82-
// When
83-
let connection = try pool.dequeueConnectionForUse()
84-
85-
try pool.execute { connection in
86-
synchronous = try connection.query("PRAGMA synchronous")
87-
}
77+
// When
78+
let connection = try pool.dequeueConnectionForUse()
8879

89-
// Then
90-
XCTAssertFalse(pool.availableConnections.contains(connection))
91-
XCTAssertTrue(pool.busyConnections.contains(connection))
92-
XCTAssertEqual(synchronous, 1)
93-
} catch {
94-
XCTFail("Test encountered unexpected error: \(error)")
80+
try pool.execute { connection in
81+
synchronous = try connection.query("PRAGMA synchronous")
9582
}
83+
84+
// Then
85+
XCTAssertFalse(pool.availableConnections.contains(connection))
86+
XCTAssertTrue(pool.busyConnections.contains(connection))
87+
XCTAssertEqual(synchronous, 1)
9688
}
9789

98-
func testThatEnqueueConnectionRemovesBusyConnectionAndInsertsAvailableConnection() {
99-
do {
100-
// Given
101-
let pool = ConnectionPool(storageLocation: storageLocation)
102-
let connection = try pool.dequeueConnectionForUse()
90+
func testThatEnqueueConnectionRemovesBusyConnectionAndInsertsAvailableConnection() throws {
91+
// Given
92+
let pool = ConnectionPool(storageLocation: storageLocation)
93+
let connection = try pool.dequeueConnectionForUse()
10394

104-
let beforeEnqueueAvailableConnections = pool.availableConnections
105-
let beforeEnqueueBusyConnections = pool.busyConnections
95+
let beforeEnqueueAvailableConnections = pool.availableConnections
96+
let beforeEnqueueBusyConnections = pool.busyConnections
10697

107-
// When
108-
pool.enqueueConnectionForReuse(connection)
98+
// When
99+
pool.enqueueConnectionForReuse(connection)
109100

110-
// Then
111-
XCTAssertFalse(beforeEnqueueAvailableConnections.contains(connection), "available connections should not contain connection before enqueue")
112-
XCTAssertTrue(beforeEnqueueBusyConnections.contains(connection), "busy connections should contain connection before enqueue")
101+
// Then
102+
XCTAssertFalse(beforeEnqueueAvailableConnections.contains(connection))
103+
XCTAssertTrue(beforeEnqueueBusyConnections.contains(connection))
113104

114-
XCTAssertTrue(pool.availableConnections.contains(connection), "available connections should contain connection after enqueue")
115-
XCTAssertFalse(pool.busyConnections.contains(connection), "busy connections should not contain connection after enqueue")
116-
} catch {
117-
XCTFail("Test encountered unexpected error: \(error)")
118-
}
105+
XCTAssertTrue(pool.availableConnections.contains(connection))
106+
XCTAssertFalse(pool.busyConnections.contains(connection))
119107
}
120108

121-
func testThatConnectionPoolCanExecuteReadOnlyClosure() {
122-
do {
123-
// Given
124-
let pool = ConnectionPool(storageLocation: storageLocation)
125-
126-
var count: Int?
109+
func testThatConnectionPoolCanExecuteReadOnlyClosure() throws {
110+
// Given
111+
let pool = ConnectionPool(storageLocation: storageLocation)
127112

128-
// When
129-
try pool.execute { connection in
130-
count = try connection.query("SELECT count(*) FROM sqlite_master where type = 'table'")
131-
}
113+
var count: Int?
132114

133-
// Then
134-
XCTAssertEqual(count, 0)
135-
} catch {
136-
XCTFail("Test encountered unexpected error: \(error)")
115+
// When
116+
try pool.execute { connection in
117+
count = try connection.query("SELECT count(*) FROM sqlite_master where type = 'table'")
137118
}
119+
120+
// Then
121+
XCTAssertEqual(count, 0)
138122
}
139123

140124
func testThatConnectionPoolFailsToExecuteWriteClosure() {
@@ -150,99 +134,91 @@ class ConnectionPoolTestCase: BaseTestCase {
150134
XCTFail("Execution should not reach this point")
151135
} catch let error as SQLiteError {
152136
// Then
153-
XCTAssertEqual(error.code, SQLITE_READONLY, "error code should equal SQLITE_READONLY")
137+
XCTAssertEqual(error.code, SQLITE_READONLY)
154138
} catch {
155139
XCTFail("Failed with an unknown error type: \(error)")
156140
}
157141
}
158142

159-
func testThatConnectionPoolCanExecuteMultipleClosuresInParallel() {
160-
do {
161-
// Given
162-
let connection = try Connection(storageLocation: storageLocation)
163-
try TestTables.createAndPopulateAgentsTable(using: connection)
164-
let pool = ConnectionPool(storageLocation: storageLocation)
165-
166-
let range = 0..<128
167-
let expectations: [XCTestExpectation] = range.map { expectation(description: "read: \($0)") }
168-
var counts: [Int] = []
169-
170-
let queue = DispatchQueue(label: "test_serial_queue")
171-
let utilityQueue = DispatchQueue.global(qos: .utility)
172-
173-
// When
174-
range.forEach { index in
175-
utilityQueue.async {
176-
do {
177-
try pool.execute { connection in
178-
guard let count: Int = try connection.query("SELECT count(*) FROM agents") else { return }
179-
queue.sync { counts.append(count) }
180-
}
181-
} catch {
182-
// No-op
143+
func testThatConnectionPoolCanExecuteMultipleClosuresInParallel() throws {
144+
// Given
145+
let connection = try Connection(storageLocation: storageLocation)
146+
try TestTables.createAndPopulateAgentsTable(using: connection)
147+
let pool = ConnectionPool(storageLocation: storageLocation)
148+
149+
let range = 0..<128
150+
let expectations: [XCTestExpectation] = range.map { expectation(description: "read: \($0)") }
151+
var counts: [Int] = []
152+
153+
let queue = DispatchQueue(label: "test_serial_queue")
154+
let utilityQueue = DispatchQueue.global(qos: .utility)
155+
156+
// When
157+
range.forEach { index in
158+
utilityQueue.async {
159+
do {
160+
try pool.execute { connection in
161+
guard let count: Int = try connection.query("SELECT count(*) FROM agents") else { return }
162+
queue.sync { counts.append(count) }
183163
}
184-
185-
expectations[index].fulfill()
164+
} catch {
165+
// No-op
186166
}
187-
}
188-
189-
waitForExpectations(timeout: 10.0, handler: nil)
190167

191-
// Then
192-
XCTAssertEqual(counts.count, range.count)
193-
194-
for count in counts {
195-
XCTAssertEqual(count, 2)
168+
expectations[index].fulfill()
196169
}
197-
} catch {
198-
XCTFail("Test encountered unexpected error: \(error)")
199170
}
200-
}
201171

202-
func testThatConnectionPoolDrainDelayWorksAsExpected() {
203-
do {
204-
// Given
205-
let connection = try Connection(storageLocation: storageLocation)
206-
try TestTables.createAndPopulateAgentsTable(using: connection)
207-
let pool = ConnectionPool(storageLocation: storageLocation, availableConnectionDrainDelay: 0.1)
172+
waitForExpectations(timeout: 10.0, handler: nil)
208173

209-
let range = 0..<10
210-
let expectations: [XCTestExpectation] = range.map { expectation(description: "read: \($0)") }
211-
var counts: [Int] = []
174+
// Then
175+
XCTAssertEqual(counts.count, range.count)
212176

213-
let queue = DispatchQueue(label: "test_serial_queue")
214-
let utilityQueue = DispatchQueue.global(qos: .utility)
177+
for count in counts {
178+
XCTAssertEqual(count, 2)
179+
}
180+
}
215181

216-
// When
217-
range.forEach { index in
218-
utilityQueue.async {
219-
do {
220-
try pool.execute { connection in
221-
guard let count: Int = try connection.query("SELECT count(*) FROM agents") else { return }
222-
queue.sync { counts.append(count) }
223-
}
224-
} catch {
225-
// No-op
182+
func testThatConnectionPoolDrainDelayWorksAsExpected() throws {
183+
// Given
184+
let connection = try Connection(storageLocation: storageLocation)
185+
try TestTables.createAndPopulateAgentsTable(using: connection)
186+
let pool = ConnectionPool(storageLocation: storageLocation, availableConnectionDrainDelay: 0.1)
187+
188+
let range = 0..<10
189+
let expectations: [XCTestExpectation] = range.map { expectation(description: "read: \($0)") }
190+
var counts: [Int] = []
191+
192+
let queue = DispatchQueue(label: "test_serial_queue")
193+
let utilityQueue = DispatchQueue.global(qos: .utility)
194+
195+
// When
196+
range.forEach { index in
197+
utilityQueue.async {
198+
do {
199+
try pool.execute { connection in
200+
guard let count: Int = try connection.query("SELECT count(*) FROM agents") else { return }
201+
queue.sync { counts.append(count) }
226202
}
227-
228-
expectations[index].fulfill()
203+
} catch {
204+
// No-op
229205
}
206+
207+
expectations[index].fulfill()
230208
}
209+
}
231210

232-
let drainExpectation = expectation(description: "drain timer should drain available connections")
233-
DispatchQueue.main.asyncAfter(seconds: 0.2) { drainExpectation.fulfill() }
211+
let drainExpectation = expectation(description: "drain timer should drain available connections")
212+
DispatchQueue.main.asyncAfter(seconds: 0.2) { drainExpectation.fulfill() }
234213

235-
waitForExpectations(timeout: 10.0, handler: nil)
214+
waitForExpectations(timeout: 10.0, handler: nil)
236215

237-
// Then
238-
XCTAssertEqual(pool.availableConnections.count, 1, "available connections count should be back down to 1")
239-
XCTAssertEqual(counts.count, range.count, "counts array should have equal number of items as range")
216+
// Then
217+
XCTAssertEqual(pool.availableConnections.count, 1, "available connections count should be back down to 1")
218+
XCTAssertEqual(counts.count, range.count, "counts array should have equal number of items as range")
240219

241-
for (index, count) in counts.enumerated() {
242-
XCTAssertEqual(count, 2, "count should be equal to 2 at index: \(index)")
243-
}
244-
} catch {
245-
XCTFail("Test encountered unexpected error: \(error)")
220+
for (index, count) in counts.enumerated() {
221+
XCTAssertEqual(count, 2, "count should be equal to 2 at index: \(index)")
246222
}
247223
}
248224
}

0 commit comments

Comments
 (0)