diff --git a/Tests/GRDBTests/TransactionDateTests.swift b/Tests/GRDBTests/TransactionDateTests.swift index 44ffce110..5a9383b20 100644 --- a/Tests/GRDBTests/TransactionDateTests.swift +++ b/Tests/GRDBTests/TransactionDateTests.swift @@ -22,13 +22,10 @@ class TransactionDateTests: GRDBTestCase { XCTAssertEqual(collectedDates, dates) } - func testTransactionDateInsideTransaction() throws { + func testTransactionDateInsideTransaction_commit() throws { let dates = [ Date.distantPast, - Date(timeIntervalSince1970: 0), - Date(timeIntervalSinceReferenceDate: 0), Date(), - Date.distantPast, Date.distantFuture, ] var dateIterator = dates.makeIterator() @@ -36,53 +33,65 @@ class TransactionDateTests: GRDBTestCase { dateIterator.next()! } + var collectedDates: [Date] = [] try makeDatabaseQueue().inDatabase { db in - do { - // Dates are constant within a transaction block - var collectedDates: [Date] = [] - try db.inTransaction { - try collectedDates.append(db.transactionDate) - try collectedDates.append(db.transactionDate) - return .commit - } - XCTAssertEqual(collectedDates, [dates[0], dates[0]]) - } - - do { - // Dates are no longer constant when transaction has completed - try XCTAssertEqual(db.transactionDate, dates[1]) - } - - do { - // Dates are constant within a transaction - var collectedDates: [Date] = [] - try db.beginTransaction() - try collectedDates.append(db.transactionDate) - try collectedDates.append(db.transactionDate) - try db.rollback() - XCTAssertEqual(collectedDates, [dates[2], dates[2]]) - } - - do { - // Dates are no longer constant when transaction has completed - try XCTAssertEqual(db.transactionDate, dates[3]) - } - - do { - // Dates are constant within a transaction - var collectedDates: [Date] = [] - try db.execute(sql: "BEGIN") - try collectedDates.append(db.transactionDate) - try collectedDates.append(db.transactionDate) - try db.execute(sql: "COMMIT") - XCTAssertEqual(collectedDates, [dates[4], dates[4]]) - } - - do { - // Dates are no longer constant when transaction has completed - try XCTAssertEqual(db.transactionDate, dates[5]) - } + try collectedDates.append(db.transactionDate) + try db.execute(sql: "BEGIN") + try collectedDates.append(db.transactionDate) + try collectedDates.append(db.transactionDate) + try db.execute(sql: "COMMIT") + try collectedDates.append(db.transactionDate) } + XCTAssertEqual(collectedDates, [dates[0], dates[1], dates[1], dates[2]]) + } + + func testTransactionDateInsideTransaction_rollback() throws { + let dates = [ + Date.distantPast, + Date(), + Date.distantFuture, + ] + var dateIterator = dates.makeIterator() + dbConfiguration.transactionClock = .custom { _ in + dateIterator.next()! + } + + var collectedDates: [Date] = [] + try makeDatabaseQueue().inDatabase { db in + try collectedDates.append(db.transactionDate) + try db.execute(sql: "BEGIN") + try collectedDates.append(db.transactionDate) + try collectedDates.append(db.transactionDate) + try db.execute(sql: "ROLLBACK") + try collectedDates.append(db.transactionDate) + } + XCTAssertEqual(collectedDates, [dates[0], dates[1], dates[1], dates[2]]) + } + + func testTransactionDateInsideTransaction_rollbackingError() throws { + let dates = [ + Date.distantPast, + Date(), + Date.distantFuture, + ] + var dateIterator = dates.makeIterator() + dbConfiguration.transactionClock = .custom { _ in + dateIterator.next()! + } + + var collectedDates: [Date] = [] + try makeDatabaseQueue().inDatabase { db in + try collectedDates.append(db.transactionDate) + try db.execute(sql: "BEGIN") + try collectedDates.append(db.transactionDate) + try collectedDates.append(db.transactionDate) + try? db.execute(sql: """ + CREATE TABLE t(id INTEGER PRIMARY KEY ON CONFLICT ROLLBACK); + INSERT INTO t VALUES (1); + INSERT INTO t VALUES (1); -- fails and rollbacks + """) + try collectedDates.append(db.transactionDate) + } + XCTAssertEqual(collectedDates, [dates[0], dates[1], dates[1], dates[2]]) } } -