diff --git a/Tests/GRDBTests/FTS4TableBuilderTests.swift b/Tests/GRDBTests/FTS4TableBuilderTests.swift index 4069ca0a1..d5b962052 100644 --- a/Tests/GRDBTests/FTS4TableBuilderTests.swift +++ b/Tests/GRDBTests/FTS4TableBuilderTests.swift @@ -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(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(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(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"]]]) + } + } }