From 6ea1e6aeaf8e2b8ad9dcd07d2e0f382dd669f5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 23 Dec 2020 10:41:03 +0100 Subject: [PATCH] Test that pragma tables are not tracked --- Tests/GRDBTests/ValueObservationTests.swift | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index b3a60cbf6..6e7cd95ec 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -108,6 +108,39 @@ class ValueObservationTests: GRDBTestCase { } } + func testPragmaTableOptimization() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { + try $0.execute(sql: """ + CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT); + """) + } + + struct T: TableRecord { } + + // A request that requires a pragma introspection query + let request = T.filter(key: 1).asRequest(of: Row.self) + + // Test that no pragma table is included in the observed region. + // This optimization helps observation that feed from a single table. + var region: DatabaseRegion? + let expectation = self.expectation(description: "") + let observation = ValueObservation + .trackingConstantRegion(request.fetchAll) + .handleEvents(willTrackRegion: { + region = $0 + expectation.fulfill() + }) + let observer = observation.start( + in: dbQueue, + onError: { error in XCTFail("Unexpected error: \(error)") }, + onChange: { _ in }) + withExtendedLifetime(observer) { + waitForExpectations(timeout: 2, handler: nil) + XCTAssertEqual(region!.description, "t(id,name)[1]") // pragma_table_xinfo is NOT tracked + } + } + // MARK: - Snapshot Optimization func testDisallowedSnapshotOptimizationWithAsyncScheduler() throws {