From 509d97b462363a3eb68555ef2eb8e033ebee8a2a Mon Sep 17 00:00:00 2001 From: Vishnu Ravi Date: Sat, 18 Nov 2023 11:06:46 -0500 Subject: [PATCH] Improve test coverage --- .../HKWorkoutActivityType+String.swift | 108 +++++++++--------- .../HealthKitOnFHIRTests/HKWorkoutTests.swift | 105 +++++++++++++++-- 2 files changed, 150 insertions(+), 63 deletions(-) diff --git a/Sources/HealthKitOnFHIR/HealthKit Extensions/HKWorkoutActivityType+String.swift b/Sources/HealthKitOnFHIR/HealthKit Extensions/HKWorkoutActivityType+String.swift index c525436..68af623 100644 --- a/Sources/HealthKitOnFHIR/HealthKit Extensions/HKWorkoutActivityType+String.swift +++ b/Sources/HealthKitOnFHIR/HealthKit Extensions/HKWorkoutActivityType+String.swift @@ -36,6 +36,8 @@ extension HKWorkoutActivityType: HKWorkoutActivityTypeDescription { return "australianFootball" case .badminton: return "badminton" + case .barre: + return "barre" case .baseball: return "baseball" case .basketball: @@ -44,10 +46,18 @@ extension HKWorkoutActivityType: HKWorkoutActivityTypeDescription { return "bowling" case .boxing: return "boxing" + case .cardioDance: + return "cardioDance" case .climbing: return "climbing" + case .cooldown: + return "coolDown" + case .coreTraining: + return "coreTraining" case .cricket: return "cricket" + case .crossCountrySkiing: + return "crossCountrySkiing" case .crossTraining: return "crossTraining" case .curling: @@ -58,6 +68,10 @@ extension HKWorkoutActivityType: HKWorkoutActivityTypeDescription { return "dance" case .danceInspiredTraining: return "danceInspiredTraining" + case .discSports: + return "discSports" + case .downhillSkiing: + return "downhillSkiing" case .elliptical: return "elliptical" case .equestrianSports: @@ -66,30 +80,50 @@ extension HKWorkoutActivityType: HKWorkoutActivityTypeDescription { return "fencing" case .fishing: return "fishing" + case .fitnessGaming: + return "fitnessGaming" + case .flexibility: + return "flexibility" case .functionalStrengthTraining: return "functionalStrengthTraining" case .golf: return "golf" case .gymnastics: return "gymnastics" + case .handCycling: + return "handCycling" case .handball: return "handball" + case .highIntensityIntervalTraining: + return "highIntensityIntervalTraining" case .hiking: return "hiking" case .hockey: return "hockey" case .hunting: return "hunting" + case .jumpRope: + return "jumpRope" + case .kickboxing: + return "kickboxing" case .lacrosse: return "lacrosse" case .martialArts: return "martialArts" case .mindAndBody: return "mindAndBody" + case .mixedCardio: + return "mixedCardio" case .mixedMetabolicCardioTraining: return "mixedMetabolicCardioTraining" + case .other: + return "other" case .paddleSports: return "paddleSports" + case .pickleball: + return "pickleball" + case .pilates: + return "pilates" case .play: return "play" case .preparationAndRecovery: @@ -106,28 +140,44 @@ extension HKWorkoutActivityType: HKWorkoutActivityTypeDescription { return "sailing" case .skatingSports: return "skatingSports" + case .snowboarding: + return "snowboarding" case .snowSports: return "snowSports" case .soccer: return "soccer" + case .socialDance: + return "socialDance" case .softball: return "softball" case .squash: return "squash" case .stairClimbing: return "stairClimbing" + case .stairs: + return "stairs" + case .stepTraining: + return "stepTraining" case .surfingSports: return "surfingSports" + case .swimBikeRun: + return "swimBikeRun" case .swimming: return "swimming" case .tableTennis: return "tableTennis" + case .taiChi: + return "taiChi" case .tennis: return "tennis" case .trackAndField: return "trackAndField" case .traditionalStrengthTraining: return "traditionalStrengthTraining" + case .transition: + return "transition" + case .underwaterDiving: + return "underwaterDiving" case .volleyball: return "volleyball" case .walking: @@ -138,64 +188,14 @@ extension HKWorkoutActivityType: HKWorkoutActivityTypeDescription { return "waterPolo" case .waterSports: return "waterSports" + case .wheelchairRunPace: + return "wheelchairRunPace" + case .wheelchairWalkPace: + return "wheelchairWalkPace" case .wrestling: return "wrestling" case .yoga: return "yoga" - case .barre: - return "barre" - case .coreTraining: - return "coreTraining" - case .crossCountrySkiing: - return "crossCountrySkiing" - case .downhillSkiing: - return "downhillSkiing" - case .flexibility: - return "flexibility" - case .highIntensityIntervalTraining: - return "highIntensityIntervalTraining" - case .jumpRope: - return "jumpRope" - case .kickboxing: - return "kickboxing" - case .pilates: - return "pilates" - case .snowboarding: - return "snowboarding" - case .stairs: - return "stairs" - case .stepTraining: - return "stepTraining" - case .wheelchairWalkPace: - return "wheelchairWalkPace" - case .wheelchairRunPace: - return "wheelchairRunPace" - case .taiChi: - return "taiChi" - case .mixedCardio: - return "mixedCardio" - case .handCycling: - return "handCycling" - case .discSports: - return "discSports" - case .fitnessGaming: - return "fitnessGaming" - case .cardioDance: - return "cardioDance" - case .socialDance: - return "socialDance" - case .pickleball: - return "pickleball" - case .cooldown: - return "coolDown" - case .swimBikeRun: - return "swimBikeRun" - case .transition: - return "transition" - case .underwaterDiving: - return "underwaterDiving" - case .other: - return "other" @unknown default: throw HealthKitOnFHIRError.invalidValue } diff --git a/Tests/HealthKitOnFHIRTests/HKWorkoutTests.swift b/Tests/HealthKitOnFHIRTests/HKWorkoutTests.swift index 64947a8..8c909b8 100644 --- a/Tests/HealthKitOnFHIRTests/HKWorkoutTests.swift +++ b/Tests/HealthKitOnFHIRTests/HKWorkoutTests.swift @@ -27,6 +27,88 @@ class HKWorkoutTests: XCTestCase { } } + let supportedWorkoutActivityTypes: [HKWorkoutActivityType] = [ + .americanFootball, + .archery, + .australianFootball, + .badminton, + .barre, + .baseball, + .basketball, + .bowling, + .boxing, + .cardioDance, + .climbing, + .cooldown, + .coreTraining, + .cricket, + .crossCountrySkiing, + .crossTraining, + .curling, + .cycling, + .discSports, + .downhillSkiing, + .elliptical, + .equestrianSports, + .fencing, + .fishing, + .fitnessGaming, + .flexibility, + .functionalStrengthTraining, + .golf, + .gymnastics, + .handCycling, + .handball, + .highIntensityIntervalTraining, + .hiking, + .hockey, + .hunting, + .jumpRope, + .kickboxing, + .lacrosse, + .martialArts, + .mindAndBody, + .mixedCardio, + .other, + .paddleSports, + .pickleball, + .pilates, + .play, + .preparationAndRecovery, + .racquetball, + .rowing, + .rugby, + .running, + .sailing, + .skatingSports, + .snowboarding, + .snowSports, + .soccer, + .socialDance, + .softball, + .squash, + .stairClimbing, + .stepTraining, + .surfingSports, + .swimBikeRun, + .swimming, + .tableTennis, + .taiChi, + .tennis, + .trackAndField, + .traditionalStrengthTraining, + .transition, + .volleyball, + .walking, + .waterFitness, + .waterPolo, + .waterSports, + .wheelchairRunPace, + .wheelchairWalkPace, + .wrestling, + .yoga + ] + func createCodeableConcept( code: String, system: String @@ -41,18 +123,23 @@ class HKWorkoutTests: XCTestCase { ) } - func testSingleWorkout() throws { + func testHKWorkoutToObservation() throws { // The HKWorkout initializers are deprecated as of iOS 17 in favor of using `HKWorkoutBuilder`, but there // is currently no mechanism to use `HKWorkoutBuilder` inside unit tests without an authenticated // `HKHealthStore`, so we use this approach. - let workoutSample = HKWorkout( - activityType: .americanFootball, - start: try startDate, - end: try endDate - ) + for activityType in supportedWorkoutActivityTypes { + let workoutSample = HKWorkout( + activityType: activityType, + start: try startDate, + end: try endDate + ) - let observation = try XCTUnwrap(workoutSample.resource.get(if: Observation.self)) - let expectedValue = createCodeableConcept(code: "americanFootball", system: "http://developer.apple.com/documentation/healthkit") - XCTAssertEqual(observation.value, .codeableConcept(expectedValue)) + let observation = try XCTUnwrap(workoutSample.resource.get(if: Observation.self)) + let expectedValue = createCodeableConcept( + code: try activityType.workoutTypeDescription, + system: "http://developer.apple.com/documentation/healthkit" + ) + XCTAssertEqual(observation.value, .codeableConcept(expectedValue)) + } } }