Files
GRDB.swift/Tests/GRDBTests/ValueObservationRecordTests.swift
T

100 lines
3.9 KiB
Swift

import XCTest
#if GRDBCUSTOMSQLITE
import GRDBCustomSQLite
#else
#if SWIFT_PACKAGE
import CSQLite
#else
import SQLite3
#endif
import GRDB
#endif
private struct Player: FetchableRecord {
var id: Int64
var name: String
init(row: Row) {
self.id = row["id"]
self.name = row["name"]
}
var row: Row {
return ["id": id, "name": name]
}
}
class ValueObservationRecordTests: GRDBTestCase {
func testAll() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }
var results: [[Player]] = []
let notificationExpectation = expectation(description: "notification")
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
let observation = ValueObservation.trackingAll(SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id"))
let observer = try observation.start(in: dbQueue) { players in
results.append(players)
notificationExpectation.fulfill()
}
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.row }}, [
[],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"], ["id":2, "name":"bar"]],
[["id":2, "name":"bar"]]])
}
}
func testOne() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }
var results: [Player?] = []
let notificationExpectation = expectation(description: "notification")
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
let observation = ValueObservation.trackingOne(SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id DESC"))
let observer = try observation.start(in: dbQueue) { player in
results.append(player)
notificationExpectation.fulfill()
}
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t") // -1
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.row }}, [
nil,
["id":1, "name":"foo"],
["id":2, "name":"bar"],
nil])
}
}
}