generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flag to check if authorizations to collect HealthKit data are already…
… present (#7) Co-authored-by: Paul Schmiedmayer <[email protected]>
- Loading branch information
1 parent
7ae7c34
commit 0a26f41
Showing
9 changed files
with
152 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import HealthKit | ||
import Spezi | ||
import SpeziHealthKit | ||
import XCTSpezi | ||
|
||
actor MockAdapterActor: Adapter { | ||
typealias InputElement = HKSample | ||
typealias InputRemovalContext = HKSampleRemovalContext | ||
typealias OutputElement = TestAppStandard.BaseType | ||
typealias OutputRemovalContext = TestAppStandard.RemovalContext | ||
|
||
|
||
func transform( | ||
_ asyncSequence: some TypedAsyncSequence<DataChange<InputElement, InputRemovalContext>> | ||
) async -> any TypedAsyncSequence<DataChange<OutputElement, OutputRemovalContext>> { | ||
asyncSequence.map { element in | ||
element.map( | ||
element: { OutputElement(id: String(describing: $0.id)) }, | ||
removalContext: { OutputRemovalContext(id: $0.id.uuidString) } | ||
) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import HealthKit | ||
@testable import SpeziHealthKit | ||
import XCTest | ||
import XCTSpezi | ||
|
||
final class SpeziHealthKitTests: XCTestCase { | ||
static let collectedSamples: Set<HKSampleType> = [ | ||
HKQuantityType(.stepCount), | ||
HKQuantityType(.distanceWalkingRunning) | ||
] | ||
|
||
let healthKitComponent: HealthKit<TestAppStandard> = HealthKit { | ||
CollectSamples( | ||
collectedSamples, | ||
deliverySetting: .anchorQuery(.afterAuthorizationAndApplicationWillLaunch) | ||
) | ||
} adapter: { | ||
MockAdapterActor() | ||
} | ||
|
||
override func tearDown() { | ||
// Clean up UserDefaults | ||
UserDefaults.standard.removeObject(forKey: UserDefaults.Keys.healthKitRequestedSampleTypes) | ||
} | ||
|
||
/// No authorizations for HealthKit data are given in the ``UserDefaults`` | ||
func testSpeziHealthKitCollectionNotAuthorized1() { | ||
XCTAssert(!healthKitComponent.authorized) | ||
} | ||
|
||
/// Not enough authorizations for HealthKit data given in the ``UserDefaults`` | ||
func testSpeziHealthKitCollectionNotAuthorized2() { | ||
// Set up UserDefaults | ||
UserDefaults.standard.set( | ||
Array(Self.collectedSamples.map { $0.identifier }.dropLast()), // Drop one of the required authorizations | ||
forKey: UserDefaults.Keys.healthKitRequestedSampleTypes | ||
) | ||
|
||
XCTAssert(!healthKitComponent.authorized) | ||
} | ||
|
||
/// Authorization for HealthKit data are given in the ``UserDefaults`` | ||
func testSpeziHealthKitCollectionAlreadyAuthorized() { | ||
// Set up UserDefaults | ||
UserDefaults.standard.set( | ||
Self.collectedSamples.map { $0.identifier }, | ||
forKey: UserDefaults.Keys.healthKitRequestedSampleTypes | ||
) | ||
|
||
XCTAssert(healthKitComponent.authorized) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters