EventDataTest

This commit is contained in:
Milos Kozak 2023-08-30 20:32:09 +02:00
parent 5c34184723
commit fc49e41670
3 changed files with 238 additions and 2 deletions

View file

@ -17,12 +17,15 @@ sealed class EventData : Event() {
@ExperimentalSerializationApi
fun serializeByte() = ProtoBuf.encodeToByteArray(serializer(), this)
companion object {
fun deserialize(json: String) = try {
Json.decodeFromString(serializer(), json)
} catch (ignored: Exception) {
Error(System.currentTimeMillis())
}
@ExperimentalSerializationApi
fun deserializeByte(byteArray: ByteArray) = try {
ProtoBuf.decodeFromByteArray(serializer(), byteArray)
@ -105,7 +108,9 @@ sealed class EventData : Event() {
val duration: Long,
val timestamp: Long,
val beatsPerMinute: Double,
val device: String): EventData() {
val device: String
) : EventData() {
override fun toString() =
"HR ${beatsPerMinute.toInt()} at ${DateTime(timestamp)} for ${duration / 1000.0}sec $device"
}
@ -167,7 +172,7 @@ sealed class EventData : Event() {
data class BolusProgress(val percent: Int, val status: String) : EventData()
@Serializable
data class SingleBg @JvmOverloads constructor(
data class SingleBg(
var timeStamp: Long,
val sgvString: String = "---",
val glucoseUnits: String = "-",
@ -282,6 +287,7 @@ sealed class EventData : Event() {
val validTo: Int
) : EventData()
}
@Serializable
data class ActionSetCustomWatchface(
val customWatchfaceData: CwfData

View file

@ -0,0 +1,37 @@
package info.nightscout.androidaps
import info.nightscout.rx.AapsSchedulers
import info.nightscout.rx.TestAapsSchedulers
import info.nightscout.rx.logging.AAPSLoggerTest
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mockito
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.quality.Strictness
import java.util.Locale
@ExtendWith(MockitoExtension::class)
@MockitoSettings(strictness = Strictness.LENIENT)
open class TestBase {
val aapsLogger = AAPSLoggerTest()
val aapsSchedulers: AapsSchedulers = TestAapsSchedulers()
@BeforeEach
fun setupLocale() {
Locale.setDefault(Locale.ENGLISH)
System.setProperty("disableFirebase", "true")
}
// Workaround for Kotlin nullability.
// https://medium.com/@elye.project/befriending-kotlin-and-mockito-1c2e7b0ef791
// https://stackoverflow.com/questions/30305217/is-it-possible-to-use-mockito-in-kotlin
fun <T> anyObject(): T {
Mockito.any<T>()
return uninitialized()
}
@Suppress("Unchecked_Cast")
fun <T> uninitialized(): T = null as T
}

View file

@ -0,0 +1,193 @@
package info.nightscout.rx.weardata
import info.nightscout.androidaps.TestBase
import kotlinx.serialization.ExperimentalSerializationApi
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@ExperimentalSerializationApi
class EventDataTest : TestBase() {
@BeforeEach
fun setUp() {
}
@Test
fun serializationTest() {
EventData.ActionPong(1, 2).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.WearException(1, byteArrayOf(0xAA.toByte()), "board", "fingerprint", "sdk", "model", "manufacturer", "product").let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.Error(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.CancelBolus(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionResendData("data").let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionPumpStatus(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionLoopStatus(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionTddStatus(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionECarbsPreCheck(1, 2, 3).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionBolusPreCheck(1.0, 2).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionFillPreCheck(1.0).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionFillPresetPreCheck(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionProfileSwitchSendInitialData(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionProfileSwitchPreCheck(1, 2).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionWizardPreCheck(1, 2).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionQuickWizardPreCheck("guid").let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionHeartRate(1, 2, 3.0, "device").let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionTempTargetPreCheck(EventData.ActionTempTargetPreCheck.TempTargetCommand.CANCEL).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionWizardConfirmed(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionTempTargetConfirmed(false).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionBolusConfirmed(1.0, 2).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionECarbsConfirmed(1, 2, 3).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionFillConfirmed(1.0).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionProfileSwitchConfirmed(1, 2).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.OpenLoopRequestConfirmed(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.CancelNotification(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
// EventData.ActionGetCustomWatchface(EventData.ActionSetCustomWatchface(CwfData())).let {
// Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
// Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
// }
EventData.ActionPing(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.OpenSettings(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.BolusProgress(1, "status").let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.SingleBg(1, sgv = 2.0, high = 3.0, low = 4.0).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.GraphData(arrayListOf(EventData.SingleBg(1, sgv = 2.0, high = 3.0, low = 4.0))).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.TreatmentData(
arrayListOf(EventData.TreatmentData.TempBasal(1, 2.0, 3, 4.0, 5.0)),
arrayListOf(EventData.TreatmentData.Basal(1, 2, 3.0)),
arrayListOf(EventData.TreatmentData.Treatment(1, 2.0, 3.0, true, true)),
arrayListOf(EventData.SingleBg(1, sgv = 2.0, high = 3.0, low = 4.0))
).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.Preferences(1, true, true, 2, 3, 4.0, 5.0, 6.0, 7, 8).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.QuickWizard(arrayListOf(EventData.QuickWizard.QuickWizardEntry("1", "2", 3, 4, 5))).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
// EventData.ActionSetCustomWatchface().let {
// Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
// Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
// }
EventData.ActionrequestCustomWatchface(true).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionrequestSetDefaultWatchface(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ActionProfileSwitchOpenActivity(1, 2).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.OpenLoopRequest("1", "2", null).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.ConfirmAction("1", "2", null).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
EventData.SnoozeAlert(1).let {
Assertions.assertEquals(it, EventData.deserializeByte(it.serializeByte()))
Assertions.assertEquals(it, EventData.deserialize(it.serialize()))
}
}
}