Skip to content

Commit

Permalink
Add additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuravi committed May 23, 2024
1 parent 43a9688 commit 77b05b4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun <T: Record> T.createObservation(
return observation
}

inline fun StepsRecord.toObservation(): Observation {
fun StepsRecord.toObservation(): Observation {
return this.createObservation(
categories = listOf(
Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("activity").setDisplay("Activity")
Expand All @@ -65,7 +65,7 @@ inline fun StepsRecord.toObservation(): Observation {
)
}

inline fun WeightRecord.toObservation(): Observation {
fun WeightRecord.toObservation(): Observation {
return this.createObservation(
categories = listOf(
Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("vital-signs").setDisplay("Vital Signs")
Expand All @@ -79,7 +79,7 @@ inline fun WeightRecord.toObservation(): Observation {
)
}

inline fun HeightRecord.toObservation(): Observation {
fun HeightRecord.toObservation(): Observation {
return this.createObservation(
categories = listOf(
Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("vital-signs").setDisplay("Vital Signs")
Expand All @@ -93,7 +93,7 @@ inline fun HeightRecord.toObservation(): Observation {
)
}

inline fun ActiveCaloriesBurnedRecord.toObservation(): Observation {
fun ActiveCaloriesBurnedRecord.toObservation(): Observation {
return this.createObservation(
categories = listOf(
Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("activity").setDisplay("Activity")
Expand All @@ -107,7 +107,7 @@ inline fun ActiveCaloriesBurnedRecord.toObservation(): Observation {
)
}

inline fun BodyTemperatureRecord.toObservation(): Observation {
fun BodyTemperatureRecord.toObservation(): Observation {
return createObservation(
categories = listOf(
Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("vital-signs").setDisplay("Vital Signs")
Expand All @@ -121,7 +121,7 @@ inline fun BodyTemperatureRecord.toObservation(): Observation {
)
}

inline fun BloodPressureRecord.toObservation(): Observation {
fun BloodPressureRecord.toObservation(): Observation {
val observation = Observation()
observation.status = Observation.ObservationStatus.FINAL

Expand Down Expand Up @@ -167,7 +167,7 @@ inline fun BloodPressureRecord.toObservation(): Observation {
return observation
}

inline fun HeartRateRecord.toObservations(): List<Observation> {
fun HeartRateRecord.toObservations(): List<Observation> {
return samples.map { sample ->
val observation = Observation()
observation.status = Observation.ObservationStatus.FINAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.health.connect.client.records.BodyTemperatureRecord
import androidx.health.connect.client.records.HeightRecord
import androidx.health.connect.client.records.StepsRecord
import androidx.health.connect.client.records.WeightRecord
import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.units.Energy
import androidx.health.connect.client.units.Length
import androidx.health.connect.client.units.Mass
Expand Down Expand Up @@ -205,4 +206,55 @@ class CreateObservationTests {
assertEquals(37.5, (observation.value as Quantity).value.toDouble(), 0.0)
assertEquals("°C", (observation.value as Quantity).unit)
}

@Test
fun heartRateRecord_toObservations_isCorrect() {
val heartRateRecord = HeartRateRecord(
samples = listOf(
HeartRateRecord.Sample(
time = Instant.parse("2023-05-18T10:15:30.00Z"),
beatsPerMinute = 72L
),
HeartRateRecord.Sample(
time = Instant.parse("2023-05-18T10:16:30.00Z"),
beatsPerMinute = 75L
)
),
startTime = Instant.parse("2023-05-18T10:15:30.00Z"),
endTime = Instant.parse("2023-05-18T10:17:30.00Z"),
startZoneOffset = ZoneOffset.UTC,
endZoneOffset = ZoneOffset.UTC
)

val observations = heartRateRecord.toObservations()

observations.forEach { observation ->
printObservationAsJson(observation)
assertEquals(Observation.ObservationStatus.FINAL, observation.status)
assertEquals("8867-4", observation.code.codingFirstRep.code)
assertEquals("vital-signs", observation.categoryFirstRep.codingFirstRep.code)
}

assertEquals(
Date.from(Instant.parse("2023-05-18T10:15:30.00Z")),
(observations[0].effective as Period).start
)
assertEquals(
Date.from(Instant.parse("2023-05-18T10:15:30.00Z")),
(observations[0].effective as Period).end
)
assertEquals(72.0, (observations[0].value as Quantity).value.toDouble(), 0.0)
assertEquals("beats/minute", (observations[0].value as Quantity).unit)

assertEquals(
Date.from(Instant.parse("2023-05-18T10:16:30.00Z")),
(observations[1].effective as Period).start
)
assertEquals(
Date.from(Instant.parse("2023-05-18T10:16:30.00Z")),
(observations[1].effective as Period).end
)
assertEquals(75.0, (observations[1].value as Quantity).value.toDouble(), 0.0)
assertEquals("beats/minute", (observations[1].value as Quantity).unit)
}
}

0 comments on commit 77b05b4

Please sign in to comment.