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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,35 @@
package com.google.samples.apps.sunflower.viewmodels

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.viewModelScope
import androidx.room.Room
import androidx.test.InstrumentationRegistry
import com.google.samples.apps.sunflower.data.AppDatabase
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
import com.google.samples.apps.sunflower.data.PlantRepository
import com.google.samples.apps.sunflower.utilities.getValue
import com.google.samples.apps.sunflower.utilities.testPlant
import com.google.samples.apps.sunflower.utilities.testPlants
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.TimeUnit

class PlantDetailViewModelTest {

private lateinit var appDatabase: AppDatabase
private lateinit var viewModel: PlantDetailViewModel
private lateinit var plantRepo: PlantRepository
private lateinit var gardenPlantingRepo: GardenPlantingRepository

@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
Expand All @@ -43,9 +55,9 @@ class PlantDetailViewModelTest {
val context = InstrumentationRegistry.getTargetContext()
appDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()

val plantRepo = PlantRepository.getInstance(appDatabase.plantDao())
val gardenPlantingRepo = GardenPlantingRepository.getInstance(
appDatabase.gardenPlantingDao())
plantRepo = PlantRepository.getInstance(appDatabase.plantDao())
gardenPlantingRepo = GardenPlantingRepository.getInstance(appDatabase.gardenPlantingDao())
appDatabase.plantDao().insertAll(testPlants)
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, testPlant.plantId)
}

Expand All @@ -59,4 +71,59 @@ class PlantDetailViewModelTest {
fun testDefaultValues() {
assertFalse(getValue(viewModel.isPlanted))
}

@Test
fun shouldAddPlantToGarden() = runBlocking {
val plantToAdd = testPlants[1]
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, plantToAdd.plantId)

launch {
viewModel.addPlantToGarden()
// DB needs time to update itself.
delay(TimeUnit.SECONDS.toMillis(1))
}.join()

val plantAdded =
getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
assertNotNull(plantAdded)
}

@Test
fun shouldCancelAddingPlantToGarden() = runBlocking {
val plantToAdd = testPlants[2]
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, plantToAdd.plantId)

launch {
viewModel.addPlantToGarden().cancel()

// Manually wait 1 sec, the adding should not happen due to call the cancel().
delay(TimeUnit.SECONDS.toMillis(1))
}.join()

val plantJustAdded =
getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
assertNull(plantJustAdded)
}

@ExperimentalCoroutinesApi
@Test
fun shouldCancelAddingPlantToGardenByViewModel() = runBlocking {
val plantToAdd = testPlants[2]
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, plantToAdd.plantId)

launch {
viewModel.addPlantToGarden()
}

launch {
viewModel.viewModelScope.cancel()

// Manually wait 1 sec, the adding should not happen due to call the cancel().
delay(TimeUnit.SECONDS.toMillis(1))
}.join()

val plantJustAdded =
getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
assertNull(plantJustAdded)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.google.samples.apps.sunflower.data.GardenPlantingRepository
import com.google.samples.apps.sunflower.data.Plant
import com.google.samples.apps.sunflower.data.PlantRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -61,8 +62,8 @@ class PlantDetailViewModel(
plant = plantRepository.getPlant(plantId)
}

fun addPlantToGarden() {
viewModelScope.launch {
fun addPlantToGarden(): Job {
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved
return viewModelScope.launch {
gardenPlantingRepository.createGardenPlanting(plantId)
}
}
Expand Down