Skip to content

Commit

Permalink
[LOOP-4788] Fix Unit Tests for iOS 17
Browse files Browse the repository at this point in the history
  • Loading branch information
Camji55 authored Jan 16, 2024
2 parents bcd2505 + 97d5e10 commit e2f4b50
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 54 deletions.
16 changes: 13 additions & 3 deletions Loop/Managers/Alerts/StoredAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@ import UIKit

extension StoredAlert {

static var encoder = JSONEncoder()
static var decoder = JSONDecoder()

static let encoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys]
encoder.dateEncodingStrategy = .iso8601
return encoder
}()

static let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()

convenience init(from alert: Alert, context: NSManagedObjectContext, issuedDate: Date = Date(), syncIdentifier: UUID = UUID()) {
do {
/// This code, using the `init(entity:insertInto:)` instead of the `init(context:)` avoids warnings during unit testing that look like this:
Expand Down
17 changes: 3 additions & 14 deletions LoopTests/Managers/Alerts/AlertManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,9 @@ class AlertManagerTests: XCTestCase {
}

wait(for: [testExpectation], timeout: 1)
if #available(iOS 15.0, *) {
XCTAssertNil(loopNotRunningRequests.first(where: { $0.content.interruptionLevel == .timeSensitive })?.content.sound)
if let request = loopNotRunningRequests.first(where: { $0.content.interruptionLevel == .critical }) {
XCTAssertEqual(request.content.sound, .defaultCriticalSound(withAudioVolume: 0))
}
} else if FeatureFlags.criticalAlertsEnabled {
for request in loopNotRunningRequests {
let sound = request.content.sound
XCTAssertTrue(sound == nil || sound == .defaultCriticalSound(withAudioVolume: 0.0))
}
} else {
for request in loopNotRunningRequests {
XCTAssertNil(request.content.sound)
}
XCTAssertNil(loopNotRunningRequests.first(where: { $0.content.interruptionLevel == .timeSensitive })?.content.sound)
if let request = loopNotRunningRequests.first(where: { $0.content.interruptionLevel == .critical }) {
XCTAssertEqual(request.content.sound, .defaultCriticalSound(withAudioVolume: 0))
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions LoopTests/Managers/Alerts/AlertStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class AlertStoreTests: XCTestCase {
let object = StoredAlert(from: alert2, context: alertStore.managedObjectContext, issuedDate: Self.historicDate)
XCTAssertNil(object.acknowledgedDate)
XCTAssertNil(object.retractedDate)
XCTAssertEqual("{\"title\":\"title\",\"acknowledgeActionButtonLabel\":\"label\",\"body\":\"body\"}", object.backgroundContent)
XCTAssertEqual("{\"title\":\"title\",\"acknowledgeActionButtonLabel\":\"label\",\"body\":\"body\"}", object.foregroundContent)
XCTAssertEqual("{\"acknowledgeActionButtonLabel\":\"label\",\"body\":\"body\",\"title\":\"title\"}", object.backgroundContent)
XCTAssertEqual("{\"acknowledgeActionButtonLabel\":\"label\",\"body\":\"body\",\"title\":\"title\"}", object.foregroundContent)
XCTAssertEqual("managerIdentifier2.alertIdentifier2", object.identifier.value)
XCTAssertEqual(Self.historicDate, object.issuedDate)
XCTAssertEqual(1, object.modificationCounter)
Expand Down Expand Up @@ -870,14 +870,13 @@ class AlertStoreLogCriticalEventLogTests: XCTestCase {
endDate: dateFormatter.date(from: "2100-01-02T03:09:00Z")!,
to: outputStream,
progress: progress))
XCTAssertEqual(outputStream.string, """
[
{"acknowledgedDate":"2100-01-02T03:08:00.000Z","alertIdentifier":"a1","backgroundContent":"{\\\"title\\\":\\\"BACKGROUND\\\",\\\"acknowledgeActionButtonLabel\\\":\\\"OK\\\",\\\"body\\\":\\\"background\\\"}","interruptionLevel":"timeSensitive","issuedDate":"2100-01-02T03:08:00.000Z","managerIdentifier":"m1","modificationCounter":1,"syncIdentifier":"52A046F7-F449-49B2-B003-7A378D0002DE","triggerType":0},
{"acknowledgedDate":"2100-01-02T03:04:00.000Z","alertIdentifier":"a3","backgroundContent":"{\\\"title\\\":\\\"BACKGROUND\\\",\\\"acknowledgeActionButtonLabel\\\":\\\"OK\\\",\\\"body\\\":\\\"background\\\"}","interruptionLevel":"timeSensitive","issuedDate":"2100-01-02T03:04:00.000Z","managerIdentifier":"m3","modificationCounter":3,"syncIdentifier":"285AEA4B-0DEE-41F4-8669-800E9582A6E7","triggerType":0},
{"acknowledgedDate":"2100-01-02T03:06:00.000Z","alertIdentifier":"a4","backgroundContent":"{\\\"title\\\":\\\"BACKGROUND\\\",\\\"acknowledgeActionButtonLabel\\\":\\\"OK\\\",\\\"body\\\":\\\"background\\\"}","interruptionLevel":"timeSensitive","issuedDate":"2100-01-02T03:06:00.000Z","managerIdentifier":"m4","modificationCounter":4,"syncIdentifier":"4B3109BD-DE11-42BD-A777-D4783459C483","triggerType":0}
]
"""
)
XCTAssertEqual(outputStream.string, #"""
[
{"acknowledgedDate":"2100-01-02T03:08:00.000Z","alertIdentifier":"a1","backgroundContent":"{\"acknowledgeActionButtonLabel\":\"OK\",\"body\":\"background\",\"title\":\"BACKGROUND\"}","interruptionLevel":"timeSensitive","issuedDate":"2100-01-02T03:08:00.000Z","managerIdentifier":"m1","modificationCounter":1,"syncIdentifier":"52A046F7-F449-49B2-B003-7A378D0002DE","triggerType":0},
{"acknowledgedDate":"2100-01-02T03:04:00.000Z","alertIdentifier":"a3","backgroundContent":"{\"acknowledgeActionButtonLabel\":\"OK\",\"body\":\"background\",\"title\":\"BACKGROUND\"}","interruptionLevel":"timeSensitive","issuedDate":"2100-01-02T03:04:00.000Z","managerIdentifier":"m3","modificationCounter":3,"syncIdentifier":"285AEA4B-0DEE-41F4-8669-800E9582A6E7","triggerType":0},
{"acknowledgedDate":"2100-01-02T03:06:00.000Z","alertIdentifier":"a4","backgroundContent":"{\"acknowledgeActionButtonLabel\":\"OK\",\"body\":\"background\",\"title\":\"BACKGROUND\"}","interruptionLevel":"timeSensitive","issuedDate":"2100-01-02T03:06:00.000Z","managerIdentifier":"m4","modificationCounter":4,"syncIdentifier":"4B3109BD-DE11-42BD-A777-D4783459C483","triggerType":0}
]
"""#)
XCTAssertEqual(progress.completedUnitCount, 3 * 1)
}

Expand Down
50 changes: 25 additions & 25 deletions LoopTests/Managers/Alerts/StoredAlertTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,34 @@ class StoredAlertEncodableTests: XCTestCase {
let storedAlert = StoredAlert(from: alert, context: managedObjectContext, syncIdentifier: UUID(uuidString: "A7073F28-0322-4506-A733-CF6E0687BAF7")!)
XCTAssertEqual(.active, storedAlert.interruptionLevel)
storedAlert.issuedDate = dateFormatter.date(from: "2020-05-14T21:00:12Z")!
try! assertStoredAlertEncodable(storedAlert, encodesJSON: """
{
"alertIdentifier" : "bar",
"backgroundContent" : "{\\\"title\\\":\\\"BACKGROUND\\\",\\\"acknowledgeActionButtonLabel\\\":\\\"OK\\\",\\\"body\\\":\\\"background\\\"}",
"interruptionLevel" : "active",
"issuedDate" : "2020-05-14T21:00:12Z",
"managerIdentifier" : "foo",
"modificationCounter" : 1,
"syncIdentifier" : "A7073F28-0322-4506-A733-CF6E0687BAF7",
"triggerType" : 0
}
"""
try! assertStoredAlertEncodable(storedAlert, encodesJSON: #"""
{
"alertIdentifier" : "bar",
"backgroundContent" : "{\"acknowledgeActionButtonLabel\":\"OK\",\"body\":\"background\",\"title\":\"BACKGROUND\"}",
"interruptionLevel" : "active",
"issuedDate" : "2020-05-14T21:00:12Z",
"managerIdentifier" : "foo",
"modificationCounter" : 1,
"syncIdentifier" : "A7073F28-0322-4506-A733-CF6E0687BAF7",
"triggerType" : 0
}
"""#
)

storedAlert.interruptionLevel = .critical
XCTAssertEqual(.critical, storedAlert.interruptionLevel)
try! assertStoredAlertEncodable(storedAlert, encodesJSON: """
{
"alertIdentifier" : "bar",
"backgroundContent" : "{\\\"title\\\":\\\"BACKGROUND\\\",\\\"acknowledgeActionButtonLabel\\\":\\\"OK\\\",\\\"body\\\":\\\"background\\\"}",
"interruptionLevel" : "critical",
"issuedDate" : "2020-05-14T21:00:12Z",
"managerIdentifier" : "foo",
"modificationCounter" : 1,
"syncIdentifier" : "A7073F28-0322-4506-A733-CF6E0687BAF7",
"triggerType" : 0
}
"""
try! assertStoredAlertEncodable(storedAlert, encodesJSON: #"""
{
"alertIdentifier" : "bar",
"backgroundContent" : "{\"acknowledgeActionButtonLabel\":\"OK\",\"body\":\"background\",\"title\":\"BACKGROUND\"}",
"interruptionLevel" : "critical",
"issuedDate" : "2020-05-14T21:00:12Z",
"managerIdentifier" : "foo",
"modificationCounter" : 1,
"syncIdentifier" : "A7073F28-0322-4506-A733-CF6E0687BAF7",
"triggerType" : 0
}
"""#
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions LoopTests/ViewModels/BolusEntryViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,14 @@ class BolusEntryViewModelTests: XCTestCase {
func testCarbEntryDateAndAbsorptionTimeString() async throws {
await setUpViewModel(originalCarbEntry: mockOriginalCarbEntry, potentialCarbEntry: mockPotentialCarbEntry)

XCTAssertEqual("12:00 PM + 0m", bolusEntryViewModel.carbEntryDateAndAbsorptionTimeString)
XCTAssertEqual("12:00PM + 0m", bolusEntryViewModel.carbEntryDateAndAbsorptionTimeString)
}

func testCarbEntryDateAndAbsorptionTimeString2() async throws {
let potentialCarbEntry = NewCarbEntry(quantity: BolusEntryViewModelTests.exampleCarbQuantity, startDate: Self.exampleStartDate, foodType: nil, absorptionTime: nil)
await setUpViewModel(originalCarbEntry: mockOriginalCarbEntry, potentialCarbEntry: potentialCarbEntry)

XCTAssertEqual("12:00 PM", bolusEntryViewModel.carbEntryDateAndAbsorptionTimeString)
XCTAssertEqual("12:00PM", bolusEntryViewModel.carbEntryDateAndAbsorptionTimeString)
}

func testIsManualGlucosePromptVisible() throws {
Expand Down

0 comments on commit e2f4b50

Please sign in to comment.