database integration test working

This commit is contained in:
AdrianLxM 2021-02-28 03:30:13 +01:00
parent 86d965a7f1
commit 2d0d38f70f
7 changed files with 111 additions and 10 deletions

View file

@ -13,9 +13,16 @@ dependencies {
testImplementation "org.skyscreamer:jsonassert:1.5.0"
testImplementation "org.hamcrest:hamcrest-all:1.3"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha04'
androidTestImplementation "androidx.test.ext:junit:$androidx_junit"
androidTestImplementation "androidx.test:rules:$androidx_rules"
// newer integration test libraries might not work
// https://stackoverflow.com/questions/64700104/attribute-androidforcequeryable-not-found-in-android-studio-when-running-espres
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}

View file

@ -0,0 +1,58 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.history
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.guepardoapps.kulid.ULID
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.DashHistoryDatabase
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordDao
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.mapper.HistoryMapper
import io.reactivex.schedulers.Schedulers
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class DashHistoryTest {
private lateinit var dao: HistoryRecordDao
private lateinit var database: DashHistoryDatabase
private lateinit var dashHistory: DashHistory
@get:Rule
val schedulerRule = RxSchedulerRule(Schedulers.trampoline())
@Before
fun setUp() {
val context = ApplicationProvider.getApplicationContext<Context>()
database = Room.inMemoryDatabaseBuilder(
context, DashHistoryDatabase::class.java).build()
dao = database.historyRecordDao()
dashHistory = DashHistory(dao, HistoryMapper())
}
@Test
fun testInsertSomething() { // needs to be camel case as runs on Android
dashHistory.getRecords().test().apply {
assertValue { it.isEmpty() }
}
dashHistory.createRecord(commandType = OmnipodCommandType.SET_BOLUS).test().apply {
assertValue { ULID.isValid(it) }
}
dashHistory.getRecords().test().apply {
assertValue { it.size == 1 }
}
}
@After
fun tearDown() {
database.close()
}
}

View file

@ -0,0 +1,32 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.history
import io.reactivex.Scheduler
import io.reactivex.android.plugins.RxAndroidPlugins
import io.reactivex.plugins.RxJavaPlugins
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
// TODO: move to core before the big merge
class RxSchedulerRule(val scheduler: Scheduler) : TestRule {
override fun apply(base: Statement, description: Description) =
object : Statement() {
override fun evaluate() {
RxAndroidPlugins.reset()
RxAndroidPlugins.setInitMainThreadSchedulerHandler { scheduler }
RxJavaPlugins.reset()
RxJavaPlugins.setIoSchedulerHandler { scheduler }
RxJavaPlugins.setNewThreadSchedulerHandler { scheduler }
RxJavaPlugins.setComputationSchedulerHandler { scheduler }
try {
base.evaluate()
} finally {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
}
}
}
}

View file

@ -20,9 +20,9 @@ class DashHistory @Inject constructor(
private val historyMapper: HistoryMapper
) {
fun markSuccess(id: String): Completable = dao.markResolved(id, ResolvedResult.SUCCESS, currentTimeMillis())
fun markSuccess(id: String): Completable = dao.markResolved(id, ResolvedResult.SUCCESS, currentTimeMillis()) // TODO pass time
fun markFailure(id: String): Completable = dao.markResolved(id, ResolvedResult.FAILURE, currentTimeMillis())
fun markFailure(id: String): Completable = dao.markResolved(id, ResolvedResult.FAILURE, currentTimeMillis()) // TODO pass time
fun createRecord(
commandType: OmnipodCommandType,
@ -33,10 +33,14 @@ class DashHistory @Inject constructor(
resolvedAt: Long? = null
): 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
return dao.save(
HistoryRecordEntity(
id = id,
createdAt = currentTimeMillis(),
createdAt = currentTimeMillis(), // TODO pass time (as date, keep createdAt)
commandType = commandType,
tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord,

View file

@ -4,6 +4,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.Omnipod
data class HistoryRecord(
val id: String, // ULID
// TODO add date
val createdAt: Long,
val commandType: OmnipodCommandType,
val initialResult: InitialResult,

View file

@ -21,10 +21,10 @@ class Converters {
fun fromInitialResult(initialResult: InitialResult) = initialResult.name
@TypeConverter
fun toResolvedResult(s: String) = enumValueOf<ResolvedResult>(s)
fun toResolvedResult(s: String?) = s?.let { enumValueOf<ResolvedResult>(it) }
@TypeConverter
fun fromResolvedResult(resolvedResult: ResolvedResult) = resolvedResult.name
fun fromResolvedResult(resolvedResult: ResolvedResult?) = resolvedResult?.name
@TypeConverter
fun toOmnipodCommandType(s: String) = enumValueOf<OmnipodCommandType>(s)

View file

@ -5,6 +5,7 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
@Database(
entities = [HistoryRecordEntity::class],
@ -20,12 +21,10 @@ abstract class DashHistoryDatabase : RoomDatabase() {
const val VERSION = 1
@Synchronized
fun build(context: Context) =
Room.databaseBuilder(context.applicationContext, DashHistoryDatabase::class.java, "omnipod_dash_history_database.db")
.fallbackToDestructiveMigration()
.build()
}
}