generated from StanfordSpezi/SpeziTemplateApplication
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tests to HealthKit Visualizations (#49)
# *Add Tests to HealthKit Visualizations* ## ♻️ Current situation & Problem Currently, we are missing test coverages for HealthKit visualizations, especially for the codes requiring access to the HealthKit data. This PR aims to cover those codes by (1) grouping codes parsing the HealthKit data as single functions to be tested with unit tests, and (2) using mock data for testing. Currently, we are still missing code coverage for reading HKStatistics as we are not able to manually initialize this class, and some auxiliary codes such as setting the states with parsed HealthKit, but the majority of the codes are covered. Overall, 402 out of 472 (~85%) lines for HealthKit visualization are now covered. ## ⚙️ Release Notes - Refactor part of the codes to group codes parsing the HealthKit data as single functions - Add unit tests for functions that parse the HKQuantity(s) - Add a feature flag `--mockTestData` to define whether we need to use the mock data for testing - Update the UI test to use the mock test data - Add codes to tap on screens to trigger and verify lollipops and details showings for the visualizations during UI tests. ## 📚 Documentation Related comments are added. ## ✅ Testing Unit tests and UI tests are added. ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/CS342/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/CS342/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/CS342/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/CS342/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
Showing
8 changed files
with
213 additions
and
64 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
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,69 @@ | ||
// | ||
// This source file is part of the PICS based on the Stanford Spezi Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import HealthKit | ||
@testable import PICS | ||
import XCTest | ||
|
||
|
||
class HKVizUnitTests: XCTestCase { | ||
// Test the parseValue function by manually creating HKQuantity and parse it. | ||
func testHKParseValue() throws { | ||
let stepQuantity = HKQuantity(unit: .count(), doubleValue: 10) | ||
XCTAssertEqual(parseValue(quantity: stepQuantity, quantityTypeIDF: .stepCount), 10) | ||
|
||
let osQuantity = HKQuantity(unit: .percent(), doubleValue: 0.9) | ||
XCTAssertEqual(parseValue(quantity: osQuantity, quantityTypeIDF: .oxygenSaturation), 90) | ||
|
||
let hrQuantity = HKQuantity(unit: HKUnit(from: "count/min"), doubleValue: 10) | ||
XCTAssertEqual(parseValue(quantity: hrQuantity, quantityTypeIDF: .heartRate), 10) | ||
|
||
// Unexpected quantity returns -1. | ||
XCTAssertEqual(parseValue(quantity: hrQuantity, quantityTypeIDF: .bodyMass), -1) | ||
} | ||
|
||
// Test updateSampleQueryData function by manually creating HKSample and parse it. | ||
func testHKUpdateSampleQueryData() throws { | ||
// This function only support oxygen saturation and heart rate so we only need to test them. | ||
let date = Date() | ||
// Testing parsing the oxygen saturation samples. | ||
let osQuantityType = HKQuantityType(.oxygenSaturation) | ||
var osSamples: [HKSample] = [] | ||
let pcts = [0.9, 0.95, 0.98] | ||
for val in pcts { | ||
let osQuantity = HKQuantity(unit: .percent(), doubleValue: val) | ||
let osQS: HKSample = HKQuantitySample(type: osQuantityType, quantity: osQuantity, start: date, end: date) | ||
osSamples.append(osQS) | ||
} | ||
var dataParsed = parseSampleQueryData(results: osSamples, quantityTypeIDF: .oxygenSaturation) | ||
for (parsed, pct) in zip(dataParsed, pcts) { | ||
XCTAssertEqual(parsed.date, date) | ||
XCTAssertEqual(parsed.sumValue, pct * 100) | ||
XCTAssertEqual(parsed.avgValue, -1) | ||
XCTAssertEqual(parsed.minValue, -1) | ||
XCTAssertEqual(parsed.minValue, -1) | ||
} | ||
// Testing parsing the heart rate samples. | ||
let hrQuantityType = HKQuantityType(.heartRate) | ||
var hrSamples: [HKSample] = [] | ||
let hrs = [50, 55, 60] | ||
for val in hrs { | ||
let hrQuantity = HKQuantity(unit: HKUnit(from: "count/min"), doubleValue: Double(val)) | ||
let hrQS: HKSample = HKQuantitySample(type: hrQuantityType, quantity: hrQuantity, start: date, end: date) | ||
hrSamples.append(hrQS) | ||
} | ||
dataParsed = parseSampleQueryData(results: hrSamples, quantityTypeIDF: .heartRate) | ||
for (parsed, val) in zip(dataParsed, hrs) { | ||
XCTAssertEqual(parsed.date, date) | ||
XCTAssertEqual(parsed.sumValue, Double(val)) | ||
XCTAssertEqual(parsed.avgValue, -1) | ||
XCTAssertEqual(parsed.minValue, -1) | ||
XCTAssertEqual(parsed.minValue, -1) | ||
} | ||
} | ||
} |
Oops, something went wrong.