fix database

This commit is contained in:
AdrianLxM 2021-02-28 03:55:01 +01:00
parent 2d0d38f70f
commit 6dce871ed4
5 changed files with 38 additions and 11 deletions

View file

@ -36,19 +36,33 @@ class DashHistoryTest {
} }
@Test @Test
fun testInsertSomething() { // needs to be camel case as runs on Android fun testInsertionAndConverters() {
dashHistory.getRecords().test().apply { dashHistory.getRecords().test().apply {
assertValue { it.isEmpty() } assertValue { it.isEmpty() }
} }
dashHistory.createRecord(commandType = OmnipodCommandType.SET_BOLUS).test().apply { dashHistory.createRecord(commandType = OmnipodCommandType.CANCEL_BOLUS, 0L).test().apply {
assertValue { ULID.isValid(it) } assertValue { ULID.isValid(it) }
} }
dashHistory.getRecords().test().apply { dashHistory.getRecords().test().apply {
assertValue { it.size == 1 } assertValue { it.size == 1 }
} }
}
@Test
fun testExceptionOnBolusWithoutRecord() {
dashHistory.getRecords().test().apply {
assertValue { it.isEmpty() }
}
dashHistory.createRecord(commandType = OmnipodCommandType.SET_BOLUS, 0L).test().apply {
assertError(IllegalArgumentException::class.java)
}
dashHistory.getRecords().test().apply {
assertValue { it.isEmpty() }
}
} }
@After @After

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.history
import com.github.guepardoapps.kulid.ULID import com.github.guepardoapps.kulid.ULID
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_BOLUS
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_TEMPORARY_BASAL
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult
@ -20,12 +22,13 @@ class DashHistory @Inject constructor(
private val historyMapper: HistoryMapper private val historyMapper: HistoryMapper
) { ) {
fun markSuccess(id: String): Completable = dao.markResolved(id, ResolvedResult.SUCCESS, currentTimeMillis()) // TODO pass time fun markSuccess(id: String, date: Long): Completable = dao.markResolved(id, ResolvedResult.SUCCESS, currentTimeMillis())
fun markFailure(id: String): Completable = dao.markResolved(id, ResolvedResult.FAILURE, currentTimeMillis()) // TODO pass time fun markFailure(id: String, date: Long): Completable = dao.markResolved(id, ResolvedResult.FAILURE, currentTimeMillis())
fun createRecord( fun createRecord(
commandType: OmnipodCommandType, commandType: OmnipodCommandType,
date: Long,
initialResult: InitialResult = InitialResult.UNCONFIRMED, initialResult: InitialResult = InitialResult.UNCONFIRMED,
tempBasalRecord: TempBasalRecord? = null, tempBasalRecord: TempBasalRecord? = null,
bolusRecord: BolusRecord? = null, bolusRecord: BolusRecord? = null,
@ -34,13 +37,20 @@ class DashHistory @Inject constructor(
): Single<String> { ): Single<String> {
val id = ULID.random() val id = ULID.random()
// TODO: verify that on OmnipodCommandType.SET_BOLUS bolusRecord is not null? when {
// TODO: verify that on SET_TEMPORARY_BASAL tempBasalRecord is not null commandType == SET_BOLUS && bolusRecord == null ->
Single.error(IllegalArgumentException("bolusRecord missing on SET_BOLUS"))
commandType == SET_TEMPORARY_BASAL && tempBasalRecord == null ->
Single.error<String>(IllegalArgumentException("tempBasalRecord missing on SET_TEMPORARY_BASAL"))
else -> null
}?.let { return it }
return dao.save( return dao.save(
HistoryRecordEntity( HistoryRecordEntity(
id = id, id = id,
createdAt = currentTimeMillis(), // TODO pass time (as date, keep createdAt) date = date,
createdAt = currentTimeMillis(),
commandType = commandType, commandType = commandType,
tempBasalRecord = tempBasalRecord, tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord, bolusRecord = bolusRecord,

View file

@ -4,8 +4,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.Omnipod
data class HistoryRecord( data class HistoryRecord(
val id: String, // ULID val id: String, // ULID
// TODO add date val createdAt: Long, // creation date of the record
val createdAt: Long, val date: Long, // when event actually happened
val commandType: OmnipodCommandType, val commandType: OmnipodCommandType,
val initialResult: InitialResult, val initialResult: InitialResult,
val record: Record?, val record: Record?,

View file

@ -12,7 +12,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.TempBas
@Entity(tableName = "historyrecords") @Entity(tableName = "historyrecords")
data class HistoryRecordEntity( data class HistoryRecordEntity(
@PrimaryKey val id: String, // ULID @PrimaryKey val id: String, // ULID
val createdAt: Long, val createdAt: Long, // creation date of the record
val date: Long, // when event actually happened
val commandType: OmnipodCommandType, val commandType: OmnipodCommandType,
val initialResult: InitialResult, val initialResult: InitialResult,
@Embedded(prefix = "tempBasalRecord_") val tempBasalRecord: TempBasalRecord?, @Embedded(prefix = "tempBasalRecord_") val tempBasalRecord: TempBasalRecord?,

View file

@ -11,17 +11,19 @@ class HistoryMapper {
HistoryRecordEntity( HistoryRecordEntity(
id = historyRecord.id, id = historyRecord.id,
createdAt = historyRecord.createdAt, createdAt = historyRecord.createdAt,
date = historyRecord.date,
commandType = historyRecord.commandType, commandType = historyRecord.commandType,
initialResult = historyRecord.initialResult, initialResult = historyRecord.initialResult,
tempBasalRecord = historyRecord.record as? TempBasalRecord, tempBasalRecord = historyRecord.record as? TempBasalRecord,
bolusRecord = historyRecord.record as? BolusRecord, bolusRecord = historyRecord.record as? BolusRecord,
resolvedResult = historyRecord.resolvedResult, resolvedResult = historyRecord.resolvedResult,
resolvedAt = historyRecord.resolvedAt resolvedAt = historyRecord.resolvedAt,
) )
fun entityToDomain(entity: HistoryRecordEntity): HistoryRecord = fun entityToDomain(entity: HistoryRecordEntity): HistoryRecord =
HistoryRecord(id = entity.id, HistoryRecord(id = entity.id,
createdAt = entity.createdAt, createdAt = entity.createdAt,
date = entity.date,
initialResult = entity.initialResult, initialResult = entity.initialResult,
commandType = entity.commandType, commandType = entity.commandType,
record = entity.bolusRecord ?: entity.tempBasalRecord, record = entity.bolusRecord ?: entity.tempBasalRecord,