Skip to content

Commit

Permalink
Fix Dao tests to using Kotlin flow
Browse files Browse the repository at this point in the history
  • Loading branch information
s12u authored and tiembo committed Jan 25, 2021
1 parent ceba43e commit 8df9697
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.room.Room
import androidx.test.espresso.matcher.ViewMatchers.assertThat
import androidx.test.platform.app.InstrumentationRegistry
import com.google.samples.apps.sunflower.utilities.getValue
import com.google.samples.apps.sunflower.utilities.testCalendar
import com.google.samples.apps.sunflower.utilities.testGardenPlanting
import com.google.samples.apps.sunflower.utilities.testPlant
import com.google.samples.apps.sunflower.utilities.testPlants
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.equalTo
import org.junit.After
Expand Down Expand Up @@ -62,7 +62,7 @@ class GardenPlantingDaoTest {
testCalendar
).also { it.gardenPlantingId = 2 }
gardenPlantingDao.insertGardenPlanting(gardenPlanting2)
assertThat(getValue(gardenPlantingDao.getGardenPlantings()).size, equalTo(2))
assertThat(gardenPlantingDao.getGardenPlantings().first().size, equalTo(2))
}

@Test fun testDeleteGardenPlanting() = runBlocking {
Expand All @@ -72,21 +72,21 @@ class GardenPlantingDaoTest {
testCalendar
).also { it.gardenPlantingId = 2 }
gardenPlantingDao.insertGardenPlanting(gardenPlanting2)
assertThat(getValue(gardenPlantingDao.getGardenPlantings()).size, equalTo(2))
assertThat(gardenPlantingDao.getGardenPlantings().first().size, equalTo(2))
gardenPlantingDao.deleteGardenPlanting(gardenPlanting2)
assertThat(getValue(gardenPlantingDao.getGardenPlantings()).size, equalTo(1))
assertThat(gardenPlantingDao.getGardenPlantings().first().size, equalTo(1))
}

@Test fun testGetGardenPlantingForPlant() {
assertTrue(getValue(gardenPlantingDao.isPlanted(testPlant.plantId)))
@Test fun testGetGardenPlantingForPlant() = runBlocking {
assertTrue(gardenPlantingDao.isPlanted(testPlant.plantId).first())
}

@Test fun testGetGardenPlantingForPlant_notFound() {
assertFalse(getValue(gardenPlantingDao.isPlanted(testPlants[2].plantId)))
@Test fun testGetGardenPlantingForPlant_notFound() = runBlocking {
assertFalse(gardenPlantingDao.isPlanted(testPlants[2].plantId).first())
}

@Test fun testGetPlantAndGardenPlantings() {
val plantAndGardenPlantings = getValue(gardenPlantingDao.getPlantedGardens())
@Test fun testGetPlantAndGardenPlantings() = runBlocking {
val plantAndGardenPlantings = gardenPlantingDao.getPlantedGardens().first()
assertThat(plantAndGardenPlantings.size, equalTo(1))

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.samples.apps.sunflower.utilities.getValue
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.equalTo
import org.junit.After
Expand Down Expand Up @@ -54,8 +54,8 @@ class PlantDaoTest {
database.close()
}

@Test fun testGetPlants() {
val plantList = getValue(plantDao.getPlants())
@Test fun testGetPlants() = runBlocking {
val plantList = plantDao.getPlants().first()
assertThat(plantList.size, equalTo(3))

// Ensure plant list is sorted by name
Expand All @@ -64,18 +64,18 @@ class PlantDaoTest {
assertThat(plantList[2], equalTo(plantC))
}

@Test fun testGetPlantsWithGrowZoneNumber() {
val plantList = getValue(plantDao.getPlantsWithGrowZoneNumber(1))
@Test fun testGetPlantsWithGrowZoneNumber() = runBlocking {
val plantList = plantDao.getPlantsWithGrowZoneNumber(1).first()
assertThat(plantList.size, equalTo(2))
assertThat(getValue(plantDao.getPlantsWithGrowZoneNumber(2)).size, equalTo(1))
assertThat(getValue(plantDao.getPlantsWithGrowZoneNumber(3)).size, equalTo(0))
assertThat(plantDao.getPlantsWithGrowZoneNumber(2).first().size, equalTo(1))
assertThat(plantDao.getPlantsWithGrowZoneNumber(3).first().size, equalTo(0))

// Ensure plant list is sorted by name
assertThat(plantList[0], equalTo(plantA))
assertThat(plantList[1], equalTo(plantB))
}

@Test fun testGetPlant() {
assertThat(getValue(plantDao.getPlant(plantA.plantId)), equalTo(plantA))
@Test fun testGetPlant() = runBlocking {
assertThat(plantDao.getPlant(plantA.plantId).first(), equalTo(plantA))
}
}

0 comments on commit 8df9697

Please sign in to comment.