#620 explorations

This commit is contained in:
Gwendal Roué committed 2019-09-25 17:08:02 +02:00
1 parent b2ea9e51d2
commit 3f3bdde10c
1 file changed
+110
+110
View File
@@ -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"]]])
}
}
}