From ed6673f2f5bf994f9b6a330a462f7010024fcf3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 15 Mar 2020 17:09:40 +0100 Subject: [PATCH] ValueObservation test refactoring WIP --- .../ValueObservationCountTests.swift | 67 ++++++------------- 1 file changed, 19 insertions(+), 48 deletions(-) diff --git a/Tests/GRDBTests/ValueObservationCountTests.swift b/Tests/GRDBTests/ValueObservationCountTests.swift index 48cc1701e..1f7d5df9a 100644 --- a/Tests/GRDBTests/ValueObservationCountTests.swift +++ b/Tests/GRDBTests/ValueObservationCountTests.swift @@ -12,12 +12,9 @@ import XCTest class ValueObservationCountTests: GRDBTestCase { func testCount() throws { - func test(writer: DatabaseWriter, scheduling: ValueObservationScheduling) throws { + func test(writer: DatabaseWriter, observation: ValueObservation>>) throws { try writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } - struct T: TableRecord { } - var observation = T.all().observationForCount() - observation.scheduling = scheduling let recorder = observation.record(in: writer) try writer.writeWithoutTransaction { db in @@ -36,7 +33,7 @@ class ValueObservationCountTests: GRDBTestCase { // 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)), \(scheduling)" + 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( @@ -45,52 +42,26 @@ class ValueObservationCountTests: GRDBTestCase { context) } } - let schedulings: [ValueObservationScheduling] = [ - .mainQueue, - .async(onQueue: .main), - .unsafe - ] - for scheduling in schedulings { - try test(writer: DatabaseQueue(), scheduling: scheduling) - try test(writer: makeDatabaseQueue(), scheduling: scheduling) - try test(writer: makeDatabasePool(), scheduling: scheduling) - } - } - - func testTableRecordStaticCount() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } - - var counts: [Int] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 5 struct T: TableRecord { } - let observation = T.observationForCount() - let observer = observation.start( - in: dbQueue, - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { count in - counts.append(count) - notificationExpectation.fulfill() - }) - try withExtendedLifetime(observer) { - try dbQueue.inDatabase { db in - try db.execute(sql: "INSERT INTO t DEFAULT VALUES") // +1 - try db.execute(sql: "UPDATE t SET id = id") // = - try db.execute(sql: "INSERT INTO t DEFAULT VALUES") // +1 - try db.inTransaction { // +1 - try db.execute(sql: "INSERT INTO t DEFAULT VALUES") - try db.execute(sql: "INSERT INTO t DEFAULT VALUES") - try db.execute(sql: "DELETE FROM t WHERE id = 1") - return .commit - } - try db.execute(sql: "DELETE FROM t WHERE id = 2") // -1 - } + let observations = [ + T.all().observationForCount(), + T.observationForCount()] + + for var observation in observations { + let schedulings: [ValueObservationScheduling] = [ + .mainQueue, + .async(onQueue: .main), + .unsafe + ] - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(counts, [0, 1, 2, 3, 2]) + for scheduling in schedulings { + observation.scheduling = scheduling + + try test(writer: DatabaseQueue(), observation: observation) + try test(writer: makeDatabaseQueue(), observation: observation) + try test(writer: makeDatabasePool(), observation: observation) + } } } }