[UPDATE/OPTIMIZE] Add tests for PlantDetailViewModel and adjust add()
The PR is for the enhancement on the unit-tests of PlantDetailViewModel since we added the coroutines feature there.
TEST update:
shouldAddPlantToGarden(): Happy-Path to add one plant.
shouldCancelAddingPlantToGarden(): Cancel adding of one plant with calling cancel on the return handler of addPlantToGarden .
shouldCancelAddingPlantToGardenByViewModel(): Cancel adding of one plant with calling cancel on the view-model.
Based on this TEST update:
Refactoring, addPlantToGarden() which can return Job handler regard of the reason that the client codes can cancel adding action separately and flexibly instead only calling cancel of view-model.
Whether you agree? @tiembo @nic0lette @objcode
Hmmm... I'm getting an error when I try to run this?
$ adb shell am instrument -w -r -e debug false -e class 'com.google.samples.apps.sunflower.viewmodels.PlantDetailViewModelTest' com.google.samples.apps.sunflower.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:796)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:51)
at androidx.room.EntityInsertionAdapter.insertAndReturnId(EntityInsertionAdapter.java:114)
at com.google.samples.apps.sunflower.data.GardenPlantingDao_Impl.insertGardenPlanting(GardenPlantingDao_Impl.java:76)
at com.google.samples.apps.sunflower.data.GardenPlantingRepository$createGardenPlanting$2.invokeSuspend(GardenPlantingRepository.kt:29)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
Test running failed: Instrumentation run failed due to 'Process crashed.'```
adb shell am instrument -w -r -e debug false -e class 'com.google.samples.apps.sunflower.viewmodels.PlantDetailViewModelTest' com.google.samples.apps.sunflower.test/androidx.test.runner.AndroidJUnitRunner
@nic0lette 😢 but it's really strange, I try this ./gradlew check connectedCheck(can test with multi devices) eachtime for whole testing stuff, everything fine at my side. Have you pulled the latest codebase?
I tried running, however, the output is failed:
adb shell am instrument -w -r -e debug false -e class 'com.google.samples.apps.sunflower.viewmodels.PlantDetailViewModelTest' com.google.samples.apps.sunflower.test/androidx.test.runner.AndroidJUnitRunner
onError: commandError=true message=INSTRUMENTATION_FAILED: com.google.samples.apps.sunflower.test/androidx.test.runner.AndroidJUnitRunner
android.util.AndroidException: INSTRUMENTATION_FAILED: com.google.samples.apps.sunflower.test/androidx.test.runner.AndroidJUnitRunner
at com.android.commands.am.Instrument.run(Instrument.java:486)
at com.android.commands.am.Am.runInstrument(Am.java:194)
at com.android.commands.am.Am.onRun(Am.java:80)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:54)
at com.android.commands.am.Am.main(Am.java:50)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:340)
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.google.samples.apps.sunflower.test/androidx.test.runner.AndroidJUnitRunner}
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS_CODE: -1
@XinyueZ Yeah =(
I tried with a fresh copy of the code in a separate directory and applied this PR. When I run all of the tests, it works great! Thank you!
If, however, I only run the tests in PlantDetailViewModelTest, then it fails with the android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY) exception above.
I kept digging, and if I only run shouldAddPlantToGarden, it also passes.