Skip to content

Commit ef9c07c

Browse files
committed
Simplify asynchronous testing
Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent 5794555 commit ef9c07c

File tree

2 files changed

+33
-44
lines changed

2 files changed

+33
-44
lines changed

SQLite Tests/DatabaseTests.swift

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -293,60 +293,52 @@ class DatabaseTests: SQLiteTestCase {
293293
}
294294

295295
func test_updateHook_setsUpdateHook() {
296-
let updateHook = expectationWithDescription("updateHook")
297-
db.updateHook { operation, db, table, rowid in
298-
XCTAssertEqual(.Insert, operation)
299-
XCTAssertEqual("main", db)
300-
XCTAssertEqual("users", table)
301-
XCTAssertEqual(1, rowid)
302-
updateHook.fulfill()
303-
}
304-
executeAndWait {
296+
async { done in
297+
db.updateHook { operation, db, table, rowid in
298+
XCTAssertEqual(.Insert, operation)
299+
XCTAssertEqual("main", db)
300+
XCTAssertEqual("users", table)
301+
XCTAssertEqual(1, rowid)
302+
done()
303+
}
305304
insertUser("alice")
306305
}
307306
}
308307

309308
func test_commitHook_setsCommitHook() {
310-
let commitHook = expectationWithDescription("commitHook")
311-
db.commitHook {
312-
commitHook.fulfill()
313-
return .Commit
314-
}
315-
executeAndWait {
316-
self.db.transaction { _ in
317-
self.insertUser("alice")
309+
async { done in
310+
db.commitHook {
311+
done()
318312
return .Commit
319313
}
320-
XCTAssertEqual(1, self.db.scalar("SELECT count(*) FROM users") as! Int64)
314+
db.transaction { _ in
315+
insertUser("alice")
316+
return .Commit
317+
}
318+
XCTAssertEqual(1, db.scalar("SELECT count(*) FROM users") as! Int64)
321319
}
322320
}
323321

324322
func test_rollbackHook_setsRollbackHook() {
325-
let rollbackHook = expectationWithDescription("commitHook")
326-
db.rollbackHook {
327-
rollbackHook.fulfill()
328-
}
329-
executeAndWait {
330-
self.db.transaction { _ in
331-
self.insertUser("alice")
323+
async { done in
324+
db.rollbackHook(done)
325+
db.transaction { _ in
326+
insertUser("alice")
332327
return .Rollback
333328
}
334-
XCTAssertEqual(0, self.db.scalar("SELECT count(*) FROM users") as! Int64)
329+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users") as! Int64)
335330
}
336331
}
337332

338333
func test_commitHook_withRollback_rollsBack() {
339-
let rollbackHook = expectationWithDescription("commitHook")
340-
db.commitHook { .Rollback }
341-
db.rollbackHook {
342-
rollbackHook.fulfill()
343-
}
344-
executeAndWait {
345-
self.db.transaction { _ in
346-
self.insertUser("alice")
334+
async { done in
335+
db.commitHook { .Rollback }
336+
db.rollbackHook(done)
337+
db.transaction { _ in
338+
insertUser("alice")
347339
return .Commit
348340
}
349-
XCTAssertEqual(0, self.db.scalar("SELECT count(*) FROM users") as! Int64)
341+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users") as! Int64)
350342
}
351343
}
352344

@@ -378,13 +370,4 @@ class DatabaseTests: SQLiteTestCase {
378370
XCTAssertEqual(1, db.scalar("SELECT ? = ? COLLATE \"NO DIACRITIC\"", "cafe", "café") as! Int64)
379371
}
380372

381-
func executeAndWait(block: () -> Void) {
382-
dispatch_async(dispatch_get_main_queue(), block)
383-
waitForExpectationsWithTimeout(5) { error in
384-
if let error = error {
385-
fatalError(error.localizedDescription)
386-
}
387-
}
388-
}
389-
390373
}

SQLite Tests/TestHelper.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@ class SQLiteTestCase: XCTestCase {
7474
if let count = trace[SQL] { trace[SQL] = count - 1 }
7575
}
7676

77+
func async(expect description: String = "async", timeout: Double = 5, @noescape block: (() -> Void) -> Void) {
78+
let expectation = expectationWithDescription(description)
79+
block(expectation.fulfill)
80+
waitForExpectationsWithTimeout(timeout, handler: nil)
81+
}
82+
7783
}

0 commit comments

Comments
 (0)