diff --git a/gradle/test_dependencies.gradle b/gradle/test_dependencies.gradle index 3a484c419c..881e535d9d 100644 --- a/gradle/test_dependencies.gradle +++ b/gradle/test_dependencies.gradle @@ -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' } diff --git a/omnipod-dash/src/androidTest/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistoryTest.kt b/omnipod-dash/src/androidTest/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistoryTest.kt new file mode 100644 index 0000000000..28467f7a59 --- /dev/null +++ b/omnipod-dash/src/androidTest/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistoryTest.kt @@ -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() + 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() + } +} \ No newline at end of file diff --git a/omnipod-dash/src/androidTest/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/RxSchedulerRule.kt b/omnipod-dash/src/androidTest/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/RxSchedulerRule.kt new file mode 100644 index 0000000000..6073957e5f --- /dev/null +++ b/omnipod-dash/src/androidTest/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/RxSchedulerRule.kt @@ -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() + } + + } + } +} \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt index 98319550ed..e2ebf8f94d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt @@ -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 { 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, diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/data/HistoryRecord.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/data/HistoryRecord.kt index 5229248a7e..3993d8de73 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/data/HistoryRecord.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/data/HistoryRecord.kt @@ -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, diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/Converters.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/Converters.kt index b37d71bf3f..2395695f1e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/Converters.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/Converters.kt @@ -21,10 +21,10 @@ class Converters { fun fromInitialResult(initialResult: InitialResult) = initialResult.name @TypeConverter - fun toResolvedResult(s: String) = enumValueOf(s) + fun toResolvedResult(s: String?) = s?.let { enumValueOf(it) } @TypeConverter - fun fromResolvedResult(resolvedResult: ResolvedResult) = resolvedResult.name + fun fromResolvedResult(resolvedResult: ResolvedResult?) = resolvedResult?.name @TypeConverter fun toOmnipodCommandType(s: String) = enumValueOf(s) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/DashHistoryDatabase.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/DashHistoryDatabase.kt index 259447afb0..75b21716ba 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/DashHistoryDatabase.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/database/DashHistoryDatabase.kt @@ -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() - } } \ No newline at end of file