mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
More ValueObseervation tests: extent
This commit is contained in:
1 parent
a519a4ab86
commit
087989fc89
1 file changed
+130
@@ -13,4 +13,134 @@ import XCTest
|
||||
#endif
|
||||
|
||||
class ValueObservationExtentTests: GRDBTestCase {
|
||||
func testExtentDatabaseLifetime() throws {
|
||||
// We need something to change
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer proceess
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 3
|
||||
|
||||
// A reducer
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in },
|
||||
value: { $0 })
|
||||
|
||||
// Create an observation
|
||||
let request = SQLRequest<Void>("SELECT * FROM t")
|
||||
var observation = ValueObservation.observing(request, reducer: reducer)
|
||||
observation.extent = .databaseLifetime
|
||||
|
||||
// Start observation
|
||||
_ = try dbQueue.add(
|
||||
observation: observation,
|
||||
onError: nil,
|
||||
onChange: { notificationExpectation.fulfill() })
|
||||
|
||||
// notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute("INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
// notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute("INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
func testExtentObserverLifetime() throws {
|
||||
// We need something to change
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer proceess
|
||||
var changesCount = 0
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
// A reducer
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in },
|
||||
value: { $0 })
|
||||
|
||||
// Create an observation
|
||||
let request = SQLRequest<Void>("SELECT * FROM t")
|
||||
var observation = ValueObservation.observing(request, reducer: reducer)
|
||||
observation.extent = .observerLifetime
|
||||
|
||||
// Start observation and deallocate observer after second change
|
||||
var observer: TransactionObserver?
|
||||
observer = try dbQueue.add(
|
||||
observation: observation,
|
||||
onError: nil,
|
||||
onChange: {
|
||||
changesCount += 1
|
||||
if changesCount == 2 {
|
||||
observer = nil
|
||||
}
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
|
||||
// notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute("INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
// not notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute("INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
// Avoid "Variable 'observer' was written to, but never read" warning
|
||||
_ = observer
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(changesCount, 2)
|
||||
}
|
||||
|
||||
func testExtentNextTransaction() throws {
|
||||
// We need something to change
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer proceess
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
// A reducer
|
||||
let reducer = AnyValueReducer(
|
||||
fetch: { _ in },
|
||||
value: { $0 })
|
||||
|
||||
// Create an observation
|
||||
let request = SQLRequest<Void>("SELECT * FROM t")
|
||||
var observation = ValueObservation.observing(request, reducer: reducer)
|
||||
observation.extent = .nextTransaction
|
||||
|
||||
// Start observation
|
||||
let observer = try dbQueue.add(
|
||||
observation: observation,
|
||||
onError: nil,
|
||||
onChange: { notificationExpectation.fulfill() })
|
||||
|
||||
try withExtendedLifetime(observer) {
|
||||
// notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute("INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
// not notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute("INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user