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
fun testInsertSomething() { // needs to be camel case as runs on Android
fun testInsertionAndConverters() {
dashHistory.getRecords().test().apply {
assertValue { it.isEmpty() }
}
dashHistory.createRecord(commandType = OmnipodCommandType.SET_BOLUS).test().apply {
dashHistory.createRecord(commandType = OmnipodCommandType.CANCEL_BOLUS, 0L).test().apply {
assertValue { ULID.isValid(it) }
}
dashHistory.getRecords().test().apply {
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

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.history
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.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.HistoryRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult
@ -20,12 +22,13 @@ class DashHistory @Inject constructor(
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(
commandType: OmnipodCommandType,
date: Long,
initialResult: InitialResult = InitialResult.UNCONFIRMED,
tempBasalRecord: TempBasalRecord? = null,
bolusRecord: BolusRecord? = null,
@ -34,13 +37,20 @@ class DashHistory @Inject constructor(
): Single<String> {
val id = ULID.random()
// TODO: verify that on OmnipodCommandType.SET_BOLUS bolusRecord is not null?
// TODO: verify that on SET_TEMPORARY_BASAL tempBasalRecord is not null
when {
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(
HistoryRecordEntity(
id = id,
createdAt = currentTimeMillis(), // TODO pass time (as date, keep createdAt)
date = date,
createdAt = currentTimeMillis(),
commandType = commandType,
tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord,

View file

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

View file

@ -12,7 +12,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.TempBas
@Entity(tableName = "historyrecords")
data class HistoryRecordEntity(
@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 initialResult: InitialResult,
@Embedded(prefix = "tempBasalRecord_") val tempBasalRecord: TempBasalRecord?,

View file

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