Skip to content

Commit

Permalink
refactor: safely unwrap register device BQ tasks in migration code
Browse files Browse the repository at this point in the history
As a continuation of the PR, #702, this commit safely unwraps all of the optional values in the Track BQ to CDP migration code.

I do not expect that register device attributes string to be null because the attributes string will contain at least the device token. In PR discussion (#702 (comment)), we were not sure a bug could happen from this code but we still wanted to safety unwrap the optional to avoid any possible crashes.

commit-id:24cb8fd5
  • Loading branch information
levibostian committed Apr 16, 2024
1 parent 394bb86 commit 06d5526
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Migration/DataPipelineMigrationAssistant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public class DataPipelineMigrationAssistant {
guard let registerPushTaskData: RegisterPushNotificationQueueTaskData = jsonAdapter.fromJson(taskData) else {
return false
}
guard let allAttributes: [String: Any] = jsonAdapter.fromJsonString(registerPushTaskData.attributesJsonString!) else {
guard let attributesJsonString = registerPushTaskData.attributesJsonString, let allAttributes: [String: Any] = jsonAdapter.fromJsonString(attributesJsonString) else {
return false
}
guard let device = allAttributes["device"] as? [String: Any] else {
Expand Down
12 changes: 12 additions & 0 deletions Tests/Migration/DataPipelineMigrationAssistantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,18 @@ class DataPipelineMigrationAssistantTests: UnitTest {
XCTAssertNotNil(migrationAssistant.getAndProcessTask(for: givenCreatedTask, siteId: testSiteId))
XCTAssertEqual(backgroundQueueMock.deleteProcessedTaskCallsCount, 0)

// When data is found but attributes are null
let pushTokenTaskDataWithNilAttributesString = RegisterPushNotificationQueueTaskData(profileIdentifier: String.random, attributesJsonString: nil)

guard let jsonData = try? JSONEncoder().encode(pushTokenTaskDataWithNilAttributesString) else {
XCTFail("Failed to create task data")
return
}
backgroundQueueMock.getTaskDetailReturnValue = TaskDetail(data: jsonData, taskType: givenType, timestamp: dateUtilStub.now)

XCTAssertNotNil(migrationAssistant.getAndProcessTask(for: givenCreatedTask, siteId: testSiteId))
XCTAssertEqual(backgroundQueueMock.deleteProcessedTaskCallsCount, 0)

// When data is found but attributes are invalid
let pushTokenTaskDataWithEmptyJsonString = RegisterPushNotificationQueueTaskData(profileIdentifier: String.random, attributesJsonString: "")

Expand Down

0 comments on commit 06d5526

Please sign in to comment.