Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

[UPDATE/OPTIMIZE] Add tests for PlantDetailViewModel and adjust add() #349

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

XinyueZ
Copy link
Contributor

@XinyueZ XinyueZ commented Feb 23, 2019

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

@XinyueZ XinyueZ force-pushed the optimize/enhance-PlantDetailViewModelTest-coroutines branch 3 times, most recently from 629ce43 to 4272e92 Compare February 23, 2019 17:01
@XinyueZ XinyueZ force-pushed the optimize/enhance-PlantDetailViewModelTest-coroutines branch from 4272e92 to 85d03c1 Compare February 23, 2019 18:38
@nic0lette
Copy link
Contributor

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.'```

@XinyueZ
Copy link
Contributor Author

XinyueZ commented Mar 13, 2019

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

@nic0lette
Copy link
Contributor

@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.

@codingjeremy codingjeremy changed the base branch from master to main September 29, 2020 19:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants