From 4ffced0b7b6d63a43e6b19cdb50dc73ab3310fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 15 Mar 2020 20:16:15 +0100 Subject: [PATCH] assertValueObservationRecordingMatch --- .../ValueObservationCountTests.swift | 13 +- ...rvationDatabaseValueConvertibleTests.swift | 52 ++++---- .../GRDBTests/ValueObservationRecorder.swift | 59 +++++++++ .../ValueObservationRecorderTests.swift | 114 ++++++++++++++++++ 4 files changed, 207 insertions(+), 31 deletions(-) diff --git a/Tests/GRDBTests/ValueObservationCountTests.swift b/Tests/GRDBTests/ValueObservationCountTests.swift index 1f7d5df9a..9b85be278 100644 --- a/Tests/GRDBTests/ValueObservationCountTests.swift +++ b/Tests/GRDBTests/ValueObservationCountTests.swift @@ -30,17 +30,12 @@ class ValueObservationCountTests: GRDBTestCase { try db.execute(sql: "DELETE FROM t WHERE id = 2") // -1 } - // We don't expect more than five values: [0, 1, 2, 3, 2] let expectedValues = [0, 1, 2, 3, 2] let values = try wait(for: recorder.prefix(expectedValues.count + 1).inverted, timeout: 0.5) - let context = "\(type(of: writer)), \(observation.scheduling)" - XCTAssert(!values.isEmpty, context) - for count in 1...max(expectedValues.count, values.count) where count <= values.count { - XCTAssertEqual( - expectedValues.suffix(count), - values.suffix(count), - context) - } + try assertValueObservationRecordingMatch( + recorded: values, + expected: expectedValues, + "\(type(of: writer)), \(observation.scheduling)") } struct T: TableRecord { } diff --git a/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift b/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift index 14ead2dbf..74b4fd57a 100644 --- a/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift +++ b/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift @@ -12,7 +12,7 @@ import XCTest @testable import GRDB #endif -private struct Name: DatabaseValueConvertible { +private struct Name: DatabaseValueConvertible, Equatable { var rawValue: String var databaseValue: DatabaseValue { rawValue.databaseValue } @@ -27,24 +27,12 @@ private struct Name: DatabaseValueConvertible { class ValueObservationDatabaseValueConvertibleTests: 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: [[Name]] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 - - let observation = SQLRequest(sql: "SELECT name FROM t ORDER BY id").observationForAll() - let observer = observation.start( - in: dbQueue, - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { names in - results.append(names) - notificationExpectation.fulfill() - }) - try withExtendedLifetime(observer) { - try dbQueue.inDatabase { db in + func test(writer: DatabaseWriter, observation: ValueObservation.RowDecoder>>) throws { + try writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } + + let recorder = observation.record(in: writer) + + try writer.writeWithoutTransaction { 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 @@ -56,12 +44,32 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1 } - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map(\.rawValue)}, [ + let expectedValues = [ [], ["foo"], ["foo", "bar"], - ["bar"]]) + ["bar"]] + let values = try wait(for: recorder.prefix(expectedValues.count + 1).inverted, timeout: 0.5) + .map { $0.map(\.rawValue) } + try assertValueObservationRecordingMatch( + recorded: values, + expected: expectedValues, + "\(type(of: writer)), \(observation.scheduling)") + } + + let schedulings: [ValueObservationScheduling] = [ + .mainQueue, + .async(onQueue: .main), + .unsafe + ] + + for scheduling in schedulings { + var observation = SQLRequest(sql: "SELECT name FROM t ORDER BY id").observationForAll() + observation.scheduling = scheduling + + try test(writer: DatabaseQueue(), observation: observation) + try test(writer: makeDatabaseQueue(), observation: observation) + try test(writer: makeDatabasePool(), observation: observation) } } diff --git a/Tests/GRDBTests/ValueObservationRecorder.swift b/Tests/GRDBTests/ValueObservationRecorder.swift index 596f9de67..c5a4d654a 100644 --- a/Tests/GRDBTests/ValueObservationRecorder.swift +++ b/Tests/GRDBTests/ValueObservationRecorder.swift @@ -247,6 +247,65 @@ extension XCTestCase { wait(for: [expectation], timeout: timeout) return try valueObservationExpectation.get() } + + /// See testAssertValueObservationRecordingMatch() + public func assertValueObservationRecordingMatch( + recorded recordedValues: [Value], + expected expectedValues: [Value], + _ message: @autoclosure () -> String = "", + file: StaticString = #file, + line: UInt = #line) + throws + where Value: Equatable + { + try assertValueObservationRecordingMatch( + recorded: recordedValues, + expected: expectedValues, + // Last value can't be missed, this is the most important of all! + allowMissingLastValue: false, + message(), file: file, line: line) + } + + private func assertValueObservationRecordingMatch( + recorded recordedValues: R, + expected expectedValues: E, + allowMissingLastValue: Bool, + _ message: @autoclosure () -> String = "", + file: StaticString = #file, + line: UInt = #line) + throws + where + R: BidirectionalCollection, + E: BidirectionalCollection, + R.Element == E.Element, + R.Element: Equatable + { + guard let value = expectedValues.last else { + if !recordedValues.isEmpty { + XCTFail("unexpected recorded prefix \(Array(recordedValues)) - \(message())", file: file, line: line) + } + return + } + + let recordedSuffix = recordedValues.reversed().prefix(while: { $0 == value }) + let expectedSuffix = expectedValues.reversed().prefix(while: { $0 == value }) + if !allowMissingLastValue { + // Both missing and duplicated values are allowed in the recorded values. + // This is because of asynchronous DatabasePool observations. + if recordedSuffix.isEmpty { + XCTFail("missing expected value \(value) - \(message())", file: file, line: line) + } + } + + let remainingRecordedValues = recordedValues.prefix(recordedValues.count - recordedSuffix.count) + let remainingExpectedValues = expectedValues.prefix(expectedValues.count - expectedSuffix.count) + try assertValueObservationRecordingMatch( + recorded: remainingRecordedValues, + expected: remainingExpectedValues, + // Other values can be missed + allowMissingLastValue: true, + message(), file: file, line: line) + } } // MARK: - ValueObservationExpectations diff --git a/Tests/GRDBTests/ValueObservationRecorderTests.swift b/Tests/GRDBTests/ValueObservationRecorderTests.swift index 6243fb350..d7e1d1075 100644 --- a/Tests/GRDBTests/ValueObservationRecorderTests.swift +++ b/Tests/GRDBTests/ValueObservationRecorderTests.swift @@ -589,4 +589,118 @@ class ValueObservationRecorderTests: FailureTestCase { XCTFail("Expected error") } catch ValueRecordingError.notFailed { } } + + // MARK: - assertValueObservationRecordingMatch + + func testAssertValueObservationRecordingMatch() throws { + do { + let expected = [3] + try assertValueObservationRecordingMatch(recorded: [3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [3, 3], expected: expected) + try assertFailure("failed - missing expected value 3") { + try assertValueObservationRecordingMatch(recorded: [], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [2]", "failed - missing expected value 3") { + try assertValueObservationRecordingMatch(recorded: [2], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [2]") { + try assertValueObservationRecordingMatch(recorded: [2, 3], expected: expected) + } + } + do { + let expected = [2, 3] + try assertValueObservationRecordingMatch(recorded: [3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [3, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [3, 3, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [2, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [2, 2, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [2, 2, 3, 3], expected: expected) + try assertFailure("failed - missing expected value 3") { + try assertValueObservationRecordingMatch(recorded: [], expected: expected) + } + try assertFailure("failed - missing expected value 3") { + try assertValueObservationRecordingMatch(recorded: [2], expected: expected) + } + try assertFailure("failed - missing expected value 3", "failed - unexpected recorded prefix [3]") { + try assertValueObservationRecordingMatch(recorded: [3, 2], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [1]") { + try assertValueObservationRecordingMatch(recorded: [1, 3], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [1]") { + try assertValueObservationRecordingMatch(recorded: [1, 2, 3], expected: expected) + } + } + do { + let expected = [3, 3] + try assertValueObservationRecordingMatch(recorded: [3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [3, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [3, 3, 3], expected: expected) + try assertFailure("failed - missing expected value 3") { + try assertValueObservationRecordingMatch(recorded: [], expected: expected) + } + try assertFailure("failed - missing expected value 3", "failed - unexpected recorded prefix [2]") { + try assertValueObservationRecordingMatch(recorded: [2], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [2]") { + try assertValueObservationRecordingMatch(recorded: [2, 3], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [2, 2]") { + try assertValueObservationRecordingMatch(recorded: [2, 2, 3], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [1, 2]") { + try assertValueObservationRecordingMatch(recorded: [1, 2, 3], expected: expected) + } + } + do { + let expected = [1, 2, 2, 3] + try assertValueObservationRecordingMatch(recorded: [3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [3, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 3, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 1, 3, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 2, 2, 3], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 2, 3], expected: expected) + try assertFailure("failed - missing expected value 3") { + try assertValueObservationRecordingMatch(recorded: [], expected: expected) + } + try assertFailure("failed - missing expected value 3") { + try assertValueObservationRecordingMatch(recorded: [2], expected: expected) + } + try assertFailure("failed - missing expected value 3", "failed - unexpected recorded prefix [3]") { + try assertValueObservationRecordingMatch(recorded: [3, 2], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [2]") { + try assertValueObservationRecordingMatch(recorded: [2, 1, 3], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [0]") { + try assertValueObservationRecordingMatch(recorded: [0, 3], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [0]") { + try assertValueObservationRecordingMatch(recorded: [0, 1, 2, 3], expected: expected) + } + } + do { + let expected = [1, 2, 1] + try assertValueObservationRecordingMatch(recorded: [1], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 1], expected: expected) + try assertValueObservationRecordingMatch(recorded: [2, 1], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 2, 1], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 1, 2, 1], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 2, 1, 1], expected: expected) + try assertValueObservationRecordingMatch(recorded: [1, 2, 2, 1], expected: expected) + try assertFailure("failed - missing expected value 1") { + try assertValueObservationRecordingMatch(recorded: [], expected: expected) + } + try assertFailure("failed - missing expected value 1") { + try assertValueObservationRecordingMatch(recorded: [2], expected: expected) + } + try assertFailure("failed - missing expected value 1") { + try assertValueObservationRecordingMatch(recorded: [1, 2], expected: expected) + } + try assertFailure("failed - unexpected recorded prefix [0]") { + try assertValueObservationRecordingMatch(recorded: [0, 1], expected: expected) + } + } + } }