mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Tests for asyncRead, asyncWrite, asyncWriteWithoutTransaction
This commit is contained in:
1 parent
b715ca5eb8
commit
0ad819902d
3 files changed
+154
-1
No files matched your search
@@ -187,7 +187,9 @@ extension DatabaseQueue {
|
||||
///
|
||||
/// - parameter block: A block that accesses the database.
|
||||
public func asyncRead(_ block: @escaping (Result<Database, Error>) -> Void) {
|
||||
writer.async { block(.success($0)) }
|
||||
writer.async { db in
|
||||
db.readOnly { block(.success(db)) }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -72,4 +72,56 @@ class DatabaseReaderTests : GRDBTestCase {
|
||||
let reader: DatabaseReader = DatabaseQueue()
|
||||
let _: DatabaseReader = AnyDatabaseReader(reader)
|
||||
}
|
||||
|
||||
func testAsyncRead() throws {
|
||||
func test(_ dbReader: DatabaseReader) throws {
|
||||
let expectation = self.expectation(description: "updates")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
var count: Int?
|
||||
dbReader.asyncRead { db in
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
do {
|
||||
count = try Int.fetchOne(db.get(), sql: "SELECT COUNT(*) FROM sqlite_master")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
semaphore.signal()
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertNotNil(count)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
try test(makeDatabasePool().makeSnapshot())
|
||||
}
|
||||
|
||||
func testAsyncReadPreventsDatabaseModification() throws {
|
||||
func test(_ dbReader: DatabaseReader) throws {
|
||||
let expectation = self.expectation(description: "updates")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
dbReader.asyncRead { db in
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
do {
|
||||
try db.get().execute(sql: "CREATE TABLE testAsyncReadPreventsDatabaseModification (a)")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_READONLY)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
semaphore.signal()
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
try test(makeDatabasePool().makeSnapshot())
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,105 @@ class DatabaseWriterTests : GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testAsyncWriteWithoutTransactionSuccess() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
let expectation = self.expectation(description: "updates")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
dbWriter.asyncWriteWithoutTransaction { db in
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
do {
|
||||
try db.execute(sql: "CREATE TABLE testAsyncWriteWithoutTransaction (a)")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
semaphore.signal()
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
let tableExists = try dbWriter.read { try $0.tableExists("testAsyncWriteWithoutTransaction") }
|
||||
XCTAssertTrue(tableExists)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
}
|
||||
|
||||
func testAsyncWriteWithoutTransactionError() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
let expectation = self.expectation(description: "updates")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
dbWriter.asyncWriteWithoutTransaction { db in
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
do {
|
||||
try db.execute(sql: "This is not SQL")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.sql, "This is not SQL")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
semaphore.signal()
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
}
|
||||
|
||||
func testAsyncWriteSuccess() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
let expectation = self.expectation(description: "updates")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
dbWriter.asyncWrite({ db in
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
try db.execute(sql: "CREATE TABLE testAsyncWrite (a)")
|
||||
}, completion: { error in
|
||||
if let error = error {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
expectation.fulfill()
|
||||
})
|
||||
semaphore.signal()
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
let tableExists = try dbWriter.read { try $0.tableExists("testAsyncWrite") }
|
||||
XCTAssertTrue(tableExists)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
}
|
||||
|
||||
func testAsyncWriteError() throws {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
let expectation = self.expectation(description: "updates")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
dbWriter.asyncWrite({ db in
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
try db.execute(sql: "This is not SQL")
|
||||
}, completion: { error in
|
||||
guard let error = error as? DatabaseError else {
|
||||
XCTFail("Unexpected error")
|
||||
return
|
||||
}
|
||||
XCTAssertEqual(error.sql, "This is not SQL")
|
||||
expectation.fulfill()
|
||||
})
|
||||
semaphore.signal()
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
try test(makeDatabasePool())
|
||||
}
|
||||
|
||||
func testAnyDatabaseWriter() {
|
||||
// This test passes if this code compiles.
|
||||
let writer: DatabaseWriter = DatabaseQueue()
|
||||
|
||||
Reference in new issue
Block a user