assertValueObservationRecordingMatch

This commit is contained in:
Gwendal Roué committed 2020-03-21 14:36:48 +01:00
1 parent ed6673f2f5
commit 4ffced0b7b
4 files changed
+207 -31

No files matched your search

@@ -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 { }
@@ -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<Name>(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<ValueReducers.AllValues<SQLRequest<Name>.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<Name>(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)
}
}
@@ -247,6 +247,65 @@ extension XCTestCase {
wait(for: [expectation], timeout: timeout)
return try valueObservationExpectation.get()
}
/// See testAssertValueObservationRecordingMatch()
public func assertValueObservationRecordingMatch<Value>(
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<R, E>(
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
@@ -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)
}
}
}
}