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

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Had to migrate to runBlocking {} due to
Kotlin/kotlinx.coroutines#1204
  • Loading branch information
chrisbanes committed Feb 10, 2020
1 parent a960ac5 commit ec2f69a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
22 changes: 10 additions & 12 deletions data-android/src/test/java/app/tivi/data/TestDatabaseInject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import androidx.test.core.app.ApplicationProvider
import app.tivi.data.dao.EpisodeWatchEntryTest
import app.tivi.data.dao.EpisodesTest
import app.tivi.data.dao.SeasonsTest
import app.tivi.data.daos.EntityInserter
import app.tivi.data.repositories.FollowedShowRepositoryTest
import app.tivi.data.repositories.SeasonsEpisodesRepositoryTest
import app.tivi.data.repositories.episodes.EpisodeDataSource
import app.tivi.data.repositories.episodes.SeasonsEpisodesDataSource
import app.tivi.data.repositories.followedshows.TraktFollowedShowsDataSource
import app.tivi.data.repositories.shows.ShowDataSource
import app.tivi.data.repositories.shows.ShowImagesDataSource
import app.tivi.data.repositories.shows.ShowStoreModule
import app.tivi.inject.Trakt
import app.tivi.trakt.TraktAuthState
import app.tivi.trakt.TraktServiceModule
Expand All @@ -37,7 +38,6 @@ import app.tivi.utils.TestTransactionRunner
import app.tivi.utils.TiviTestDatabase
import com.uwetrottmann.tmdb2.Tmdb
import com.uwetrottmann.trakt5.TraktV2
import dagger.Binds
import dagger.Component
import dagger.Module
import dagger.Provides
Expand All @@ -64,7 +64,8 @@ class TestDataSourceModule(
private val tmdbEpisodeDataSource: EpisodeDataSource = mockk(),
private val seasonsDataSource: SeasonsEpisodesDataSource = mockk(),
private val traktShowDataSource: ShowDataSource = mockk(),
private val tmdbShowDataSource: ShowDataSource = mockk()
private val tmdbShowDataSource: ShowDataSource = mockk(),
private val tmdbShowImagesDataSource: ShowImagesDataSource = mockk()
) {
@Provides
fun provideTraktFollowedShowsDataSource() = traktFollowedShowsDataSource
Expand All @@ -87,13 +88,17 @@ class TestDataSourceModule(
@Provides
@app.tivi.inject.Tmdb
fun provideTmdbShowDataSource(): ShowDataSource = tmdbShowDataSource

@Provides
@app.tivi.inject.Tmdb
fun provideTmdbShowImagesDataSource(): ShowImagesDataSource = tmdbShowImagesDataSource
}

@Module(includes = [
TestRoomDatabaseModule::class,
TestDatabaseModuleBinds::class,
DatabaseDaoModule::class,
TraktServiceModule::class
TraktServiceModule::class,
ShowStoreModule::class
])
class TestDatabaseModule {
@Provides
Expand Down Expand Up @@ -127,10 +132,3 @@ class TestRoomDatabaseModule {
@Provides
fun provideDatabaseTransactionRunner(): DatabaseTransactionRunner = TestTransactionRunner
}

@Module
abstract class TestDatabaseModuleBinds {
@Singleton
@Binds
abstract fun provideEntityInserter(inserter: TiviEntityInserter): EntityInserter
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import app.tivi.data.entities.Success
import app.tivi.data.repositories.followedshows.FollowedShowsRepository
import app.tivi.data.repositories.followedshows.TraktFollowedShowsDataSource
import app.tivi.utils.SuccessFakeShowDataSource
import app.tivi.utils.SuccessFakeShowImagesDataSource
import app.tivi.utils.followedShow1Local
import app.tivi.utils.followedShow1Network
import app.tivi.utils.followedShow1PendingDelete
Expand All @@ -38,7 +39,7 @@ import app.tivi.utils.show
import app.tivi.utils.show2
import io.mockk.coEvery
import javax.inject.Inject
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.runBlocking
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.junit.Before
Expand All @@ -59,26 +60,25 @@ class FollowedShowRepositoryTest {

@Before
fun setup() {
val fakeShowDataSource = SuccessFakeShowDataSource()

DaggerTestComponent.builder()
.testDataSourceModule(
TestDataSourceModule(
traktShowDataSource = fakeShowDataSource,
tmdbShowDataSource = fakeShowDataSource
traktShowDataSource = SuccessFakeShowDataSource,
tmdbShowDataSource = SuccessFakeShowDataSource,
tmdbShowImagesDataSource = SuccessFakeShowImagesDataSource
)
)
.build()
.inject(this)

runBlockingTest {
runBlocking {
// We'll assume that there's a show in the db
insertShow(database)
}
}

@Test
fun testSync() = runBlockingTest {
fun testSync() = runBlocking {
coEvery { traktDataSource.getFollowedListId() } returns Success(0)
coEvery { traktDataSource.getListShows(0) } returns Success(listOf(followedShow1Network to show))

Expand All @@ -89,7 +89,7 @@ class FollowedShowRepositoryTest {
}

@Test
fun testSync_emptyResponse() = runBlockingTest {
fun testSync_emptyResponse() = runBlocking {
insertFollowedShow(database)

coEvery { traktDataSource.getFollowedListId() } returns Success(0)
Expand All @@ -102,7 +102,7 @@ class FollowedShowRepositoryTest {
}

@Test
fun testSync_responseDifferentShow() = runBlockingTest {
fun testSync_responseDifferentShow() = runBlocking {
insertFollowedShow(database)

coEvery { traktDataSource.getFollowedListId() } returns Success(0)
Expand All @@ -115,7 +115,7 @@ class FollowedShowRepositoryTest {
}

@Test
fun testSync_pendingDelete() = runBlockingTest {
fun testSync_pendingDelete() = runBlocking {
followShowsDao.insert(followedShow1PendingDelete)

// Return error for the list ID so that we disable syncing
Expand All @@ -128,7 +128,7 @@ class FollowedShowRepositoryTest {
}

@Test
fun testSync_pendingAdd() = runBlockingTest {
fun testSync_pendingAdd() = runBlocking {
followShowsDao.insert(followedShow1PendingUpload)

// Return an error for the list ID so that we disable syncing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import app.tivi.utils.showId
import io.mockk.coEvery
import javax.inject.Inject
import kotlinx.coroutines.flow.produceIn
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
Expand Down Expand Up @@ -75,14 +75,14 @@ class SeasonsEpisodesRepositoryTest {
.build()
.inject(this)

runBlockingTest {
runBlocking {
// We'll assume that there's a show in the db
insertShow(database)
}
}

@Test
fun testSyncEpisodeWatches() = runBlockingTest {
fun testSyncEpisodeWatches() = runBlocking {
seasonsDao.insert(s1)
episodesDao.insertAll(s1_episodes)

Expand All @@ -96,7 +96,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testEpisodeWatches_sameEntries() = runBlockingTest {
fun testEpisodeWatches_sameEntries() = runBlocking {
seasonsDao.insert(s1)
episodesDao.insertAll(s1_episodes)

Expand All @@ -112,7 +112,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testEpisodeWatches_deletesMissing() = runBlockingTest {
fun testEpisodeWatches_deletesMissing() = runBlocking {
seasonsDao.insert(s1)
episodesDao.insertAll(s1_episodes)

Expand All @@ -128,7 +128,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testEpisodeWatches_emptyResponse() = runBlockingTest {
fun testEpisodeWatches_emptyResponse() = runBlocking {
seasonsDao.insert(s1)
episodesDao.insertAll(s1_episodes)

Expand All @@ -144,7 +144,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testSyncSeasonsEpisodes() = runBlockingTest {
fun testSyncSeasonsEpisodes() = runBlocking {
// Return a response with 2 items
coEvery { seasonsDataSource.getSeasonsEpisodes(showId) } returns
Success(listOf(s1 to s1_episodes))
Expand All @@ -156,7 +156,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testSyncSeasonsEpisodes_sameEntries() = runBlockingTest {
fun testSyncSeasonsEpisodes_sameEntries() = runBlocking {
seasonsDao.insert(s1)
episodesDao.insertAll(s1_episodes)

Expand All @@ -171,7 +171,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testSyncSeasonsEpisodes_emptyResponse() = runBlockingTest {
fun testSyncSeasonsEpisodes_emptyResponse() = runBlocking {
seasonsDao.insert(s1)
episodesDao.insertAll(s1_episodes)

Expand All @@ -186,7 +186,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testSyncSeasonsEpisodes_deletesMissingSeasons() = runBlockingTest {
fun testSyncSeasonsEpisodes_deletesMissingSeasons() = runBlocking {
seasonsDao.insertAll(s1, s2)
episodesDao.insertAll(s1_episodes)
episodesDao.insertAll(s2_episodes)
Expand All @@ -202,7 +202,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testSyncSeasonsEpisodes_deletesMissingEpisodes() = runBlockingTest {
fun testSyncSeasonsEpisodes_deletesMissingEpisodes() = runBlocking {
seasonsDao.insertAll(s1, s2)
episodesDao.insertAll(s1_episodes)
episodesDao.insertAll(s2_episodes)
Expand All @@ -219,7 +219,7 @@ class SeasonsEpisodesRepositoryTest {
}

@Test
fun testObserveNextEpisodeToWatch_singleFlow() = runBlockingTest {
fun testObserveNextEpisodeToWatch_singleFlow() = runBlocking {
seasonsDao.insertAll(s1)
episodesDao.insertAll(s1_episodes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@
package app.tivi.utils

import app.tivi.data.entities.Result
import app.tivi.data.entities.ShowTmdbImage
import app.tivi.data.entities.Success
import app.tivi.data.entities.TiviShow
import app.tivi.data.repositories.shows.ShowDataSource
import app.tivi.data.repositories.shows.ShowImagesDataSource

class SuccessFakeShowDataSource : ShowDataSource {
object SuccessFakeShowDataSource : ShowDataSource {
override suspend fun getShow(show: TiviShow): Result<TiviShow> {
return Success(show)
}
}

object SuccessFakeShowImagesDataSource : ShowImagesDataSource {
override suspend fun getShowImages(show: TiviShow): Result<List<ShowTmdbImage>> {
return Success(emptyList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import javax.inject.Singleton
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope

@Module(includes = [ShowsModuleBinds::class, ShowStoreModule::class])
class ShowsModule

@Module
internal abstract class ShowsModuleBinds {
@Binds
Expand All @@ -46,8 +49,8 @@ internal abstract class ShowsModuleBinds {
abstract fun bindTmdbShowImagesDataSource(source: TmdbShowImagesDataSource): ShowImagesDataSource
}

@Module(includes = [ShowsModuleBinds::class])
class ShowsModule {
@Module
class ShowStoreModule {
@Provides
@Singleton
fun provideShowStore(
Expand Down

0 comments on commit ec2f69a

Please sign in to comment.