Skip to content

Commit

Permalink
Adapt dao test to work with flows and recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abelgardep committed Mar 22, 2023
1 parent bb2b1d5 commit 5636453
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions owncloudData/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dependencies {

// Dependencies for instrumented tests
androidTestImplementation project(":owncloudTestUtil")
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$orgJetbrainsKotlinx"
androidTestImplementation "androidx.test:runner:$androidxTest"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxTestEspresso"
androidTestImplementation "androidx.test.ext:junit:$androidxTestExt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ import androidx.test.platform.app.InstrumentationRegistry
import com.owncloud.android.data.OwncloudDatabase
import com.owncloud.android.data.spaces.datasources.implementation.OCLocalSpacesDataSource.Companion.toEntity
import com.owncloud.android.data.spaces.datasources.implementation.OCLocalSpacesDataSource.Companion.toModel
import com.owncloud.android.domain.spaces.model.OCSpace.Companion.DRIVE_TYPE_PROJECT
import com.owncloud.android.testutil.OC_SPACE_PROJECT_WITHOUT_IMAGE
import com.owncloud.android.testutil.OC_SPACE_PROJECT_WITH_IMAGE
import com.owncloud.android.testutil.OC_SPACE_SPECIAL_IMAGE
import com.owncloud.android.testutil.OC_SPACE_SPECIAL_README
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
@MediumTest
class SpacesDaoTest {
@Rule
Expand All @@ -53,7 +58,7 @@ class SpacesDaoTest {
}

@Test
fun insertOrDeleteSpacesWithEmptyDatabase() {
fun insertOrDeleteSpacesWithEmptyDatabase() = runTest {
val accountName = OC_SPACE_PROJECT_WITHOUT_IMAGE.accountName

val specialsToInsert = listOf(
Expand All @@ -68,15 +73,18 @@ class SpacesDaoTest {

spacesDao.insertOrDeleteSpaces(spacesToInsert, specialsToInsert)

val spacesInDatabase = spacesDao.getProjectSpacesWithSpecialsForAccount(accountName).map { it.toModel() }
val spacesInDatabase = spacesDao.getSpacesByDriveTypeWithSpecialsForAccountAsFlow(
accountName = accountName,
filterDriveTypes = setOf(DRIVE_TYPE_PROJECT),
).first().map { it.toModel() }

assertNotNull(spacesInDatabase)
assertEquals(1, spacesInDatabase.size)
assertEquals(spacesToInsertModel, spacesInDatabase)
}

@Test
fun insertOrDeleteSpacesWithSpacesAlreadyInDatabaseNotAttachedToAccountAnymore() {
fun insertOrDeleteSpacesWithSpacesAlreadyInDatabaseNotAttachedToAccountAnymore() = runTest {
val accountName = OC_SPACE_PROJECT_WITHOUT_IMAGE.accountName

val specialsAlreadyInDatabaseToInsert = listOf(
Expand All @@ -90,7 +98,10 @@ class SpacesDaoTest {

spacesDao.insertOrDeleteSpaces(spacesAlreadyInDatabaseToInsert, specialsAlreadyInDatabaseToInsert)

val spacesAlreadyInDatabase = spacesDao.getProjectSpacesWithSpecialsForAccount(accountName)
val spacesAlreadyInDatabase = spacesDao.getSpacesByDriveTypeWithSpecialsForAccountAsFlow(
accountName = accountName,
filterDriveTypes = setOf(DRIVE_TYPE_PROJECT),
).first()

assertEquals(1, spacesAlreadyInDatabase.size)
assertEquals(2, spacesAlreadyInDatabase[0].specials.size)
Expand All @@ -107,7 +118,11 @@ class SpacesDaoTest {

spacesDao.insertOrDeleteSpaces(newSpacesToInsert, newSpecialsToInsert)

val spacesInDatabaseEntity = spacesDao.getProjectSpacesWithSpecialsForAccount(accountName)
val spacesInDatabaseEntity = spacesDao.getSpacesByDriveTypeWithSpecialsForAccountAsFlow(
accountName = accountName,
filterDriveTypes = setOf(DRIVE_TYPE_PROJECT),
).first()

val spacesInDatabase = spacesInDatabaseEntity.map { it.toModel() }
val specialsInDatabase = spacesInDatabaseEntity.flatMap { it.specials }

Expand All @@ -118,7 +133,7 @@ class SpacesDaoTest {
}

@Test
fun insertOrDeleteSpacesWithSpacesAlreadyInDatabaseStillAttachedToAccount() {
fun insertOrDeleteSpacesWithSpacesAlreadyInDatabaseStillAttachedToAccount() = runTest {
val accountName = OC_SPACE_PROJECT_WITHOUT_IMAGE.accountName

val specialsAlreadyInDatabaseToInsert = listOf(
Expand All @@ -132,7 +147,10 @@ class SpacesDaoTest {

spacesDao.insertOrDeleteSpaces(spacesAlreadyInDatabaseToInsert, specialsAlreadyInDatabaseToInsert)

val spacesAlreadyInDatabase = spacesDao.getProjectSpacesWithSpecialsForAccount(accountName)
val spacesAlreadyInDatabase = spacesDao.getSpacesByDriveTypeWithSpecialsForAccountAsFlow(
accountName = accountName,
filterDriveTypes = setOf(DRIVE_TYPE_PROJECT),
).first()

assertEquals(1, spacesAlreadyInDatabase.size)
assertEquals(2, spacesAlreadyInDatabase[0].specials.size)
Expand All @@ -151,7 +169,10 @@ class SpacesDaoTest {

spacesDao.insertOrDeleteSpaces(newSpacesToInsert, newSpecialsToInsert)

val spacesInDatabase = spacesDao.getProjectSpacesWithSpecialsForAccount(accountName)
val spacesInDatabase = spacesDao.getSpacesByDriveTypeWithSpecialsForAccountAsFlow(
accountName = accountName,
filterDriveTypes = setOf(DRIVE_TYPE_PROJECT),
).first()
val specialsInDatabase = spacesInDatabase.flatMap { it.specials }

assertNotNull(spacesInDatabase)
Expand Down

0 comments on commit 5636453

Please sign in to comment.