mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
#620 explorations
This commit is contained in:
1 parent
b2ea9e51d2
commit
3f3bdde10c
1 file changed
+110
@@ -330,4 +330,114 @@ class FTS4TableBuilderTests: GRDBTestCase {
|
||||
XCTAssertTrue(uncompressCalled)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 620
|
||||
func testObservation() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4())
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "INSERT INTO ft_documents VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["content":"foo"]]])
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 620
|
||||
func testObservationSynchronized1() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "documents") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("content", .text)
|
||||
}
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4()) { t in
|
||||
t.synchronize(withTable: "documents")
|
||||
t.column("content")
|
||||
}
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["id":1, "content":"foo"]]])
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 620
|
||||
func testObservationSynchronized2() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "documents") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("content", .text)
|
||||
}
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4()) { t in
|
||||
t.synchronize(withTable: "documents")
|
||||
t.column("content")
|
||||
}
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["content":"foo"]]])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user