NSBolus unit tests
This commit is contained in:
parent
dbc73f22f4
commit
fb265f959e
|
@ -21,7 +21,8 @@ data class NSBolus(
|
||||||
override val pumpSerial: String?,
|
override val pumpSerial: String?,
|
||||||
override var app: String? = null,
|
override var app: String? = null,
|
||||||
val insulin: Double,
|
val insulin: Double,
|
||||||
val type: BolusType
|
val type: BolusType,
|
||||||
|
val isBasalInsulin: Boolean
|
||||||
|
|
||||||
) : NSTreatment {
|
) : NSTreatment {
|
||||||
enum class BolusType {
|
enum class BolusType {
|
||||||
|
|
|
@ -17,10 +17,21 @@ import info.nightscout.sdk.remotemodel.RemoteTreatment
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert to [RemoteTreatment] and back to [NSTreatment]
|
||||||
|
* testing purpose only
|
||||||
|
*
|
||||||
|
* @param treatment original
|
||||||
|
*
|
||||||
|
* @return treatment after double conversion
|
||||||
|
*/
|
||||||
|
fun NSTreatment.convertToRemoteAndBack(): NSTreatment? =
|
||||||
|
toRemoteTreatment()?.toTreatment()
|
||||||
|
|
||||||
internal fun RemoteTreatment.toTreatment(): NSTreatment? {
|
internal fun RemoteTreatment.toTreatment(): NSTreatment? {
|
||||||
val treatmentTimestamp = timestamp()
|
val treatmentTimestamp = timestamp()
|
||||||
when {
|
when {
|
||||||
insulin != null && insulin > 0 ->
|
insulin != null && insulin > 0 ->
|
||||||
return NSBolus(
|
return NSBolus(
|
||||||
date = treatmentTimestamp,
|
date = treatmentTimestamp,
|
||||||
device = this.device,
|
device = this.device,
|
||||||
|
@ -40,6 +51,7 @@ internal fun RemoteTreatment.toTreatment(): NSTreatment? {
|
||||||
pumpSerial = this.pumpSerial,
|
pumpSerial = this.pumpSerial,
|
||||||
insulin = this.insulin,
|
insulin = this.insulin,
|
||||||
type = NSBolus.BolusType.fromString(this.type),
|
type = NSBolus.BolusType.fromString(this.type),
|
||||||
|
isBasalInsulin = isBasalInsulin ?: false
|
||||||
)
|
)
|
||||||
|
|
||||||
carbs != null && carbs > 0 ->
|
carbs != null && carbs > 0 ->
|
||||||
|
@ -355,7 +367,8 @@ internal fun NSTreatment.toRemoteTreatment(): RemoteTreatment? =
|
||||||
pumpType = pumpType,
|
pumpType = pumpType,
|
||||||
pumpSerial = pumpSerial,
|
pumpSerial = pumpSerial,
|
||||||
insulin = insulin,
|
insulin = insulin,
|
||||||
type = type.name
|
type = type.name,
|
||||||
|
isBasalInsulin = isBasalInsulin
|
||||||
)
|
)
|
||||||
|
|
||||||
is NSCarbs -> RemoteTreatment(
|
is NSCarbs -> RemoteTreatment(
|
||||||
|
|
|
@ -76,7 +76,8 @@ internal data class RemoteTreatment(
|
||||||
@SerializedName("rate") val rate: Double? = null, // Double "Temp Basal" absolute rate (could be calculated with percent and profile information...)
|
@SerializedName("rate") val rate: Double? = null, // Double "Temp Basal" absolute rate (could be calculated with percent and profile information...)
|
||||||
@SerializedName("extendedEmulated") val extendedEmulated: RemoteTreatment? = null, // Gson of emulated EB
|
@SerializedName("extendedEmulated") val extendedEmulated: RemoteTreatment? = null, // Gson of emulated EB
|
||||||
@SerializedName("timeshift") val timeshift: Long? = null, // integer "Profile Switch"
|
@SerializedName("timeshift") val timeshift: Long? = null, // integer "Profile Switch"
|
||||||
@SerializedName("percentage") val percentage: Int? = null // integer "Profile Switch"
|
@SerializedName("percentage") val percentage: Int? = null, // integer "Profile Switch"
|
||||||
|
@SerializedName("isBasalInsulin") val isBasalInsulin: Boolean? = null // boolean "Bolus"
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun timestamp(): Long {
|
fun timestamp(): Long {
|
||||||
|
|
|
@ -49,7 +49,7 @@ data class Bolus(
|
||||||
var insulinConfiguration: InsulinConfiguration? = null
|
var insulinConfiguration: InsulinConfiguration? = null
|
||||||
) : TraceableDBEntry, DBEntryWithTime {
|
) : TraceableDBEntry, DBEntryWithTime {
|
||||||
|
|
||||||
private fun contentEqualsTo(other: Bolus): Boolean =
|
fun contentEqualsTo(other: Bolus): Boolean =
|
||||||
isValid == other.isValid &&
|
isValid == other.isValid &&
|
||||||
timestamp == other.timestamp &&
|
timestamp == other.timestamp &&
|
||||||
utcOffset == other.utcOffset &&
|
utcOffset == other.utcOffset &&
|
||||||
|
@ -58,6 +58,16 @@ data class Bolus(
|
||||||
notes == other.notes &&
|
notes == other.notes &&
|
||||||
isBasalInsulin == other.isBasalInsulin
|
isBasalInsulin == other.isBasalInsulin
|
||||||
|
|
||||||
|
fun interfaceIdsEqualsTo(other: Bolus): Boolean =
|
||||||
|
interfaceIDs.nightscoutId == interfaceIDs.nightscoutId &&
|
||||||
|
interfaceIDs.nightscoutSystemId == interfaceIDs.nightscoutSystemId &&
|
||||||
|
interfaceIDs.pumpType == interfaceIDs.pumpType &&
|
||||||
|
interfaceIDs.pumpSerial == interfaceIDs.pumpSerial &&
|
||||||
|
interfaceIDs.temporaryId == interfaceIDs.temporaryId &&
|
||||||
|
interfaceIDs.pumpId == interfaceIDs.pumpId &&
|
||||||
|
interfaceIDs.startId == interfaceIDs.startId &&
|
||||||
|
interfaceIDs.endId == interfaceIDs.endId
|
||||||
|
|
||||||
fun onlyNsIdAdded(previous: Bolus): Boolean =
|
fun onlyNsIdAdded(previous: Bolus): Boolean =
|
||||||
previous.id != id &&
|
previous.id != id &&
|
||||||
contentEqualsTo(previous) &&
|
contentEqualsTo(previous) &&
|
||||||
|
|
|
@ -14,6 +14,7 @@ fun NSBolus.toBolus(): Bolus =
|
||||||
amount = insulin,
|
amount = insulin,
|
||||||
type = type.toBolusType(),
|
type = type.toBolusType(),
|
||||||
notes = notes,
|
notes = notes,
|
||||||
|
isBasalInsulin = isBasalInsulin,
|
||||||
interfaceIDs_backing = InterfaceIDs(nightscoutId = identifier, pumpId = pumpId, pumpType = InterfaceIDs.PumpType.fromString(pumpType), pumpSerial = pumpSerial, endId = endId)
|
interfaceIDs_backing = InterfaceIDs(nightscoutId = identifier, pumpId = pumpId, pumpType = InterfaceIDs.PumpType.fromString(pumpType), pumpSerial = pumpSerial, endId = endId)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ fun Bolus.toNSBolus(): NSBolus =
|
||||||
insulin = amount,
|
insulin = amount,
|
||||||
type = type.toBolusType(),
|
type = type.toBolusType(),
|
||||||
notes = notes,
|
notes = notes,
|
||||||
|
isBasalInsulin = isBasalInsulin,
|
||||||
identifier = interfaceIDs.nightscoutId,
|
identifier = interfaceIDs.nightscoutId,
|
||||||
pumpId = interfaceIDs.pumpId,
|
pumpId = interfaceIDs.pumpId,
|
||||||
pumpType = interfaceIDs.pumpType?.name,
|
pumpType = interfaceIDs.pumpType?.name,
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package info.nightscout.plugins.sync.nsclientV3.extensions
|
||||||
|
|
||||||
|
import info.nightscout.database.entities.Bolus
|
||||||
|
import info.nightscout.database.entities.embedments.InterfaceIDs
|
||||||
|
import info.nightscout.sdk.localmodel.treatment.NSBolus
|
||||||
|
import info.nightscout.sdk.mapper.convertToRemoteAndBack
|
||||||
|
import org.junit.jupiter.api.Assertions
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
@Suppress("SpellCheckingInspection")
|
||||||
|
internal class BolusExtensionKtTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun toBolus() {
|
||||||
|
var bolus = Bolus(
|
||||||
|
timestamp = 10000,
|
||||||
|
isValid = true,
|
||||||
|
amount = 1.0,
|
||||||
|
type = Bolus.Type.SMB,
|
||||||
|
notes = "aaaa",
|
||||||
|
isBasalInsulin = false,
|
||||||
|
interfaceIDs_backing = InterfaceIDs(
|
||||||
|
nightscoutId = "nightscoutId",
|
||||||
|
pumpId = 11000,
|
||||||
|
pumpType = InterfaceIDs.PumpType.DANA_I,
|
||||||
|
pumpSerial = "bbbb"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
var bolus2 = (bolus.toNSBolus().convertToRemoteAndBack() as NSBolus).toBolus()
|
||||||
|
Assertions.assertTrue(bolus.contentEqualsTo(bolus2))
|
||||||
|
Assertions.assertTrue(bolus.interfaceIdsEqualsTo(bolus2))
|
||||||
|
|
||||||
|
bolus = Bolus(
|
||||||
|
timestamp = 10000,
|
||||||
|
isValid = false,
|
||||||
|
amount = 1.0,
|
||||||
|
type = Bolus.Type.NORMAL,
|
||||||
|
notes = "aaaa",
|
||||||
|
isBasalInsulin = true,
|
||||||
|
interfaceIDs_backing = InterfaceIDs(
|
||||||
|
nightscoutId = "nightscoutId",
|
||||||
|
pumpId = 11000,
|
||||||
|
pumpType = InterfaceIDs.PumpType.DANA_I,
|
||||||
|
pumpSerial = "bbbb"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
bolus2 = (bolus.toNSBolus().convertToRemoteAndBack() as NSBolus).toBolus()
|
||||||
|
Assertions.assertTrue(bolus.contentEqualsTo(bolus2))
|
||||||
|
Assertions.assertTrue(bolus.interfaceIdsEqualsTo(bolus2))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue