-
-
Notifications
You must be signed in to change notification settings - Fork 732
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #61: test TransactionObserver with a simple savepoint
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
Tests/Public/Core/TransactionObserver/TransactionObserverSavepointsTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import XCTest | ||
#if SQLITE_HAS_CODEC | ||
import GRDBCipher | ||
#else | ||
import GRDB | ||
#endif | ||
|
||
private struct ObservedDatabaseEvent { | ||
let tableName: String | ||
let rowID: Int64 | ||
let kind: DatabaseEvent.Kind | ||
|
||
init(rawEvent event: DatabaseEvent) { | ||
tableName = event.tableName | ||
rowID = event.rowID | ||
kind = event.kind | ||
} | ||
} | ||
|
||
private class TransactionObserver : TransactionObserverType { | ||
var lastCommittedEvents: [ObservedDatabaseEvent] = [] | ||
var events: [ObservedDatabaseEvent] = [] | ||
|
||
func databaseDidChangeWithEvent(event: DatabaseEvent) { | ||
events.append(ObservedDatabaseEvent(rawEvent: event)) | ||
} | ||
|
||
func databaseWillCommit() throws { | ||
} | ||
|
||
func databaseDidCommit(db: Database) { | ||
lastCommittedEvents = events | ||
events = [] | ||
} | ||
|
||
func databaseDidRollback(db: Database) { | ||
lastCommittedEvents = [] | ||
events = [] | ||
} | ||
} | ||
|
||
class TransactionObserverSavepointsTests: GRDBTestCase { | ||
|
||
private func match(event event: ObservedDatabaseEvent, kind: DatabaseEvent.Kind, tableName: String, rowId: Int64) -> Bool { | ||
return (event.tableName == tableName) && (event.rowID == rowId) && (event.kind == kind) | ||
} | ||
|
||
|
||
// MARK: - Events | ||
|
||
func testSavepointAsTransactionEvent() { | ||
assertNoError { | ||
let dbQueue = try makeDatabaseQueue() | ||
let observer = TransactionObserver() | ||
dbQueue.addTransactionObserver(observer) | ||
|
||
try dbQueue.inDatabase { db in | ||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") | ||
try db.execute("SAVEPOINT sp1") | ||
try db.execute("INSERT INTO items (id) VALUES (NULL)") | ||
try db.execute("INSERT INTO items (id) VALUES (NULL)") | ||
try db.execute("RELEASE SAVEPOINT sp1") | ||
} | ||
|
||
XCTAssertEqual(observer.lastCommittedEvents.count, 2) | ||
XCTAssertTrue(match(event: observer.lastCommittedEvents[0], kind: .Insert, tableName: "items", rowId: 1)) | ||
XCTAssertTrue(match(event: observer.lastCommittedEvents[1], kind: .Insert, tableName: "items", rowId: 2)) | ||
} | ||
} | ||
|
||
} |