Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: convert some test code from crashable to failable #4125

Merged
merged 8 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Tests/SentryProfilerTests/SentryTraceProfilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@
}
}

func printTimestamps(entries: [[String: Any]]) -> [NSString] {
entries.reduce(into: [NSString](), { partialResult, entry in
partialResult.append(entry["elapsed_since_start_ns"] as! NSString)
func printTimestamps(entries: [[String: Any]]) throws -> [NSString] {
try entries.reduce(into: [NSString](), { partialResult, entry in
partialResult.append(try XCTUnwrap(entry["elapsed_since_start_ns"] as? NSString))
})
}

Expand All @@ -441,7 +441,7 @@
let sortedExpectedEntries = try sortedByTimestamps(expectedEntries)

guard actualEntries.count == expectedEntries.count else {
XCTFail("Wrong number of values under \(key). expected: \(printTimestamps(entries: sortedExpectedEntries)); actual: \(printTimestamps(entries: sortedActualEntries)); transaction start time: \(transaction.startSystemTime)")
try XCTFail("Wrong number of values under \(key). expected: \(printTimestamps(entries: sortedExpectedEntries)); actual: \(printTimestamps(entries: sortedActualEntries)); transaction start time: \(transaction.startSystemTime)")

Check warning on line 444 in Tests/SentryProfilerTests/SentryTraceProfilerTests.swift

View check run for this annotation

Codecov / codecov/patch

Tests/SentryProfilerTests/SentryTraceProfilerTests.swift#L444

Added line #L444 was not covered by tests
return
}

Expand Down
38 changes: 22 additions & 16 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SentryFileManagerTests: XCTestCase {
var delegate: TestFileManagerDelegate!
// swiftlint:enable weak_delegate

init() {
init() throws {
currentDateProvider = TestCurrentDateProvider()
dispatchQueueWrapper = TestSentryDispatchQueueWrapper()

Expand All @@ -39,7 +39,7 @@ class SentryFileManagerTests: XCTestCase {

sessionEnvelope = SentryEnvelope(session: session)

let sessionCopy = session.copy() as! SentrySession
let sessionCopy = try XCTUnwrap(session.copy() as? SentrySession)
sessionCopy.incrementErrors()
// We need to serialize in order to set the timestamp and the duration
sessionUpdate = SentrySession(jsonObject: sessionCopy.serialize())!
Expand All @@ -48,7 +48,7 @@ class SentryFileManagerTests: XCTestCase {
let items = [SentryEnvelopeItem(session: sessionUpdate), SentryEnvelopeItem(event: event)]
sessionUpdateEnvelope = SentryEnvelope(id: event.eventId, items: items)

let sessionUpdateCopy = sessionUpdate.copy() as! SentrySession
let sessionUpdateCopy = try XCTUnwrap(sessionUpdate.copy() as? SentrySession)
// We need to serialize in order to set the timestamp and the duration
expectedSessionUpdate = SentrySession(jsonObject: sessionUpdateCopy.serialize())!
// We can only set the init flag after serialize, because the duration is not set if the init flag is set
Expand All @@ -75,9 +75,9 @@ class SentryFileManagerTests: XCTestCase {
private var fixture: Fixture!
private var sut: SentryFileManager!

override func setUp() {
super.setUp()
fixture = Fixture()
override func setUpWithError() throws {
try super.setUpWithError()
fixture = try Fixture()
SentryDependencyContainer.sharedInstance().dateProvider = fixture.currentDateProvider

sut = fixture.getSut()
Expand Down Expand Up @@ -665,7 +665,7 @@ class SentryFileManagerTests: XCTestCase {

#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

func testReadPreviousBreadcrumbs() {
func testReadPreviousBreadcrumbs() throws {
let observer = SentryWatchdogTerminationScopeObserver(maxBreadcrumbs: 2, fileManager: sut)

for count in 0..<3 {
Expand All @@ -677,14 +677,17 @@ class SentryFileManagerTests: XCTestCase {
}

sut.moveBreadcrumbsToPreviousBreadcrumbs()
let result = sut.readPreviousBreadcrumbs()
var result = sut.readPreviousBreadcrumbs()

XCTAssertEqual(result.count, 3)
XCTAssertEqual((result[0] as! NSDictionary)["message"] as! String, "0")
XCTAssertEqual((result[1] as! NSDictionary)["message"] as! String, "1")
XCTAssertEqual((result[2] as! NSDictionary)["message"] as! String, "2")
XCTAssertEqual(try XCTUnwrap(result.first as? NSDictionary)["message"] as? String, "0")
result = [Any](result.dropFirst())
XCTAssertEqual(try XCTUnwrap(result.first as? NSDictionary)["message"] as? String, "1")
result = [Any](result.dropFirst())
XCTAssertEqual(try XCTUnwrap(result.first as? NSDictionary)["message"] as? String, "2")
}

func testReadPreviousBreadcrumbsCorrectOrderWhenFileTwoHasMoreCrumbs() {
func testReadPreviousBreadcrumbsCorrectOrderWhenFileTwoHasMoreCrumbs() throws {
let observer = SentryWatchdogTerminationScopeObserver(maxBreadcrumbs: 2, fileManager: sut)

for count in 0..<5 {
Expand All @@ -696,11 +699,14 @@ class SentryFileManagerTests: XCTestCase {
}

sut.moveBreadcrumbsToPreviousBreadcrumbs()
let result = sut.readPreviousBreadcrumbs()
var result = sut.readPreviousBreadcrumbs()

XCTAssertEqual(result.count, 3)
XCTAssertEqual((result[0] as! NSDictionary)["message"] as! String, "2")
XCTAssertEqual((result[1] as! NSDictionary)["message"] as! String, "3")
XCTAssertEqual((result[2] as! NSDictionary)["message"] as! String, "4")
XCTAssertEqual(try XCTUnwrap(result.first as? NSDictionary)["message"] as? String, "2")
result = [Any](result.dropFirst())
XCTAssertEqual(try XCTUnwrap(result.first as? NSDictionary)["message"] as? String, "3")
result = [Any](result.dropFirst())
XCTAssertEqual(try XCTUnwrap(result.first as? NSDictionary)["message"] as? String, "4")
}

#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ class SentryCoreDataTrackerTests: XCTestCase {
try assertSave("INSERTED 2 items, UPDATED 2 items, DELETED 2 items")
}

func test_Operation_InData() {
func test_Operation_InData() throws {
fixture.context.inserted = [fixture.testEntity(), fixture.testEntity(), fixture.secondTestEntity()]
fixture.context.updated = [fixture.testEntity(), fixture.secondTestEntity(), fixture.secondTestEntity()]
fixture.context.deleted = [fixture.testEntity(), fixture.testEntity(), fixture.secondTestEntity(), fixture.secondTestEntity(), fixture.secondTestEntity()]

let sut = fixture.getSut()

let transaction = startTransaction()
let transaction = try startTransaction()

XCTAssertNoThrow(try sut.managedObjectContext(fixture.context) { _ in
return true
Expand Down Expand Up @@ -223,10 +223,10 @@ class SentryCoreDataTrackerTests: XCTestCase {
XCTAssertEqual(updated["SecondTestEntity"] as? Int, 2)
}

func test_Request_with_Error() {
func test_Request_with_Error() throws {
let fetch = NSFetchRequest<TestEntity>(entityName: "TestEntity")

let transaction = startTransaction()
let transaction = try startTransaction()
let sut = fixture.getSut()

let context = fixture.context
Expand All @@ -239,10 +239,10 @@ class SentryCoreDataTrackerTests: XCTestCase {
XCTAssertEqual(transaction.children[0].status, .internalError)
}

func test_Request_with_Error_is_nil() {
func test_Request_with_Error_is_nil() throws {
let fetch = NSFetchRequest<TestEntity>(entityName: "TestEntity")

let transaction = startTransaction()
let transaction = try startTransaction()
let sut = fixture.getSut()

let context = fixture.context
Expand All @@ -255,8 +255,8 @@ class SentryCoreDataTrackerTests: XCTestCase {
XCTAssertEqual(transaction.children[0].status, .internalError)
}

func test_save_with_Error() {
let transaction = startTransaction()
func test_save_with_Error() throws {
let transaction = try startTransaction()
let sut = fixture.getSut()
fixture.context.inserted = [fixture.testEntity()]
XCTAssertThrowsError(try sut.managedObjectContext(fixture.context) { _ in
Expand All @@ -267,8 +267,8 @@ class SentryCoreDataTrackerTests: XCTestCase {
XCTAssertEqual(transaction.children[0].status, .internalError)
}

func test_save_with_error_is_nil() {
let transaction = startTransaction()
func test_save_with_error_is_nil() throws {
let transaction = try startTransaction()
let sut = fixture.getSut()
fixture.context.inserted = [fixture.testEntity()]

Expand All @@ -280,10 +280,10 @@ class SentryCoreDataTrackerTests: XCTestCase {
XCTAssertEqual(transaction.children[0].status, .internalError)
}

func test_Save_NoChanges() {
func test_Save_NoChanges() throws {
let sut = fixture.getSut()

let transaction = startTransaction()
let transaction = try startTransaction()

XCTAssertNoThrow(try sut.managedObjectContext(fixture.context) { _ in
return true
Expand All @@ -299,7 +299,7 @@ private extension SentryCoreDataTrackerTests {
func assertSave(_ expectedDescription: String, mainThread: Bool = true) throws {
let sut = fixture.getSut()

let transaction = startTransaction()
let transaction = try startTransaction()

XCTAssertNoThrow(try sut.managedObjectContext(fixture.context) { _ in
return true
Expand All @@ -311,7 +311,7 @@ private extension SentryCoreDataTrackerTests {
}

func assertRequest(_ fetch: NSFetchRequest<TestEntity>, expectedDescription: String, mainThread: Bool = true) throws {
let transaction = startTransaction()
let transaction = try startTransaction()
let sut = fixture.getSut()

let context = fixture.context
Expand Down Expand Up @@ -350,8 +350,8 @@ private extension SentryCoreDataTrackerTests {
}
}

private func startTransaction() -> SentryTracer {
return SentrySDK.startTransaction(name: "TestTransaction", operation: "TestTransaction", bindToScope: true) as! SentryTracer
private func startTransaction() throws -> SentryTracer {
return try XCTUnwrap(SentrySDK.startTransaction(name: "TestTransaction", operation: "TestTransaction", bindToScope: true) as? SentryTracer)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ class SentryCoreDataTrackingIntegrationTests: XCTestCase {
assert_DontInstall { $0.enableCoreDataTracing = false }
}

func test_Fetch() {
func test_Fetch() throws {
SentrySDK.start(options: fixture.options)
let stack = fixture.coreDataStack
let fetch = NSFetchRequest<TestEntity>(entityName: "TestEntity")
let transaction = startTransaction()
let transaction = try startTransaction()
var _ = try? stack.managedObjectContext.fetch(fetch)
XCTAssertEqual(transaction.children.count, 1)
}

func test_Save() {
func test_Save() throws {
SentrySDK.start(options: fixture.options)
let stack = fixture.coreDataStack
let transaction = startTransaction()
let transaction = try startTransaction()
let newEntity: TestEntity = stack.getEntity()
newEntity.field1 = "Some Update"
try? stack.managedObjectContext.save()
Expand All @@ -77,30 +77,30 @@ class SentryCoreDataTrackingIntegrationTests: XCTestCase {
XCTAssertEqual(transaction.children[0].operation, "db.sql.transaction")
}

func test_Save_noChanges() {
func test_Save_noChanges() throws {
SentrySDK.start(options: fixture.options)
let stack = fixture.coreDataStack
let transaction = startTransaction()
let transaction = try startTransaction()

try? stack.managedObjectContext.save()

XCTAssertEqual(transaction.children.count, 0)
}

func test_Fetch_StoppedSwizzling() {
func test_Fetch_StoppedSwizzling() throws {
SentrySDK.start(options: fixture.options)
let stack = fixture.coreDataStack
let fetch = NSFetchRequest<TestEntity>(entityName: "TestEntity")
let transaction = startTransaction()
let transaction = try startTransaction()
SentryCoreDataSwizzling.sharedInstance.stop()
var _ = try? stack.managedObjectContext.fetch(fetch)
XCTAssertEqual(transaction.children.count, 0)
}

func test_Save_StoppedSwizzling() {
func test_Save_StoppedSwizzling() throws {
SentrySDK.start(options: fixture.options)
let stack = fixture.coreDataStack
let transaction = startTransaction()
let transaction = try startTransaction()
let newEntity: TestEntity = stack.getEntity()
newEntity.field1 = "Some Update"
SentryCoreDataSwizzling.sharedInstance.stop()
Expand All @@ -116,7 +116,7 @@ class SentryCoreDataTrackingIntegrationTests: XCTestCase {
XCTAssertNil(SentryCoreDataSwizzling.sharedInstance.coreDataTracker)
}

private func startTransaction() -> SentryTracer {
return SentrySDK.startTransaction(name: "TestTransaction", operation: "TestTransaction", bindToScope: true) as! SentryTracer
private func startTransaction() throws -> SentryTracer {
return try XCTUnwrap(SentrySDK.startTransaction(name: "TestTransaction", operation: "TestTransaction", bindToScope: true) as? SentryTracer)
}
}
Loading
Loading