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

Commit ec2f69a

Browse files
committed
Fix tests
Had to migrate to runBlocking {} due to Kotlin/kotlinx.coroutines#1204
1 parent a960ac5 commit ec2f69a

File tree

5 files changed

+47
-38
lines changed

5 files changed

+47
-38
lines changed

data-android/src/test/java/app/tivi/data/TestDatabaseInject.kt

+10-12
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ import androidx.test.core.app.ApplicationProvider
2222
import app.tivi.data.dao.EpisodeWatchEntryTest
2323
import app.tivi.data.dao.EpisodesTest
2424
import app.tivi.data.dao.SeasonsTest
25-
import app.tivi.data.daos.EntityInserter
2625
import app.tivi.data.repositories.FollowedShowRepositoryTest
2726
import app.tivi.data.repositories.SeasonsEpisodesRepositoryTest
2827
import app.tivi.data.repositories.episodes.EpisodeDataSource
2928
import app.tivi.data.repositories.episodes.SeasonsEpisodesDataSource
3029
import app.tivi.data.repositories.followedshows.TraktFollowedShowsDataSource
3130
import app.tivi.data.repositories.shows.ShowDataSource
31+
import app.tivi.data.repositories.shows.ShowImagesDataSource
32+
import app.tivi.data.repositories.shows.ShowStoreModule
3233
import app.tivi.inject.Trakt
3334
import app.tivi.trakt.TraktAuthState
3435
import app.tivi.trakt.TraktServiceModule
@@ -37,7 +38,6 @@ import app.tivi.utils.TestTransactionRunner
3738
import app.tivi.utils.TiviTestDatabase
3839
import com.uwetrottmann.tmdb2.Tmdb
3940
import com.uwetrottmann.trakt5.TraktV2
40-
import dagger.Binds
4141
import dagger.Component
4242
import dagger.Module
4343
import dagger.Provides
@@ -64,7 +64,8 @@ class TestDataSourceModule(
6464
private val tmdbEpisodeDataSource: EpisodeDataSource = mockk(),
6565
private val seasonsDataSource: SeasonsEpisodesDataSource = mockk(),
6666
private val traktShowDataSource: ShowDataSource = mockk(),
67-
private val tmdbShowDataSource: ShowDataSource = mockk()
67+
private val tmdbShowDataSource: ShowDataSource = mockk(),
68+
private val tmdbShowImagesDataSource: ShowImagesDataSource = mockk()
6869
) {
6970
@Provides
7071
fun provideTraktFollowedShowsDataSource() = traktFollowedShowsDataSource
@@ -87,13 +88,17 @@ class TestDataSourceModule(
8788
@Provides
8889
@app.tivi.inject.Tmdb
8990
fun provideTmdbShowDataSource(): ShowDataSource = tmdbShowDataSource
91+
92+
@Provides
93+
@app.tivi.inject.Tmdb
94+
fun provideTmdbShowImagesDataSource(): ShowImagesDataSource = tmdbShowImagesDataSource
9095
}
9196

9297
@Module(includes = [
9398
TestRoomDatabaseModule::class,
94-
TestDatabaseModuleBinds::class,
9599
DatabaseDaoModule::class,
96-
TraktServiceModule::class
100+
TraktServiceModule::class,
101+
ShowStoreModule::class
97102
])
98103
class TestDatabaseModule {
99104
@Provides
@@ -127,10 +132,3 @@ class TestRoomDatabaseModule {
127132
@Provides
128133
fun provideDatabaseTransactionRunner(): DatabaseTransactionRunner = TestTransactionRunner
129134
}
130-
131-
@Module
132-
abstract class TestDatabaseModuleBinds {
133-
@Singleton
134-
@Binds
135-
abstract fun provideEntityInserter(inserter: TiviEntityInserter): EntityInserter
136-
}

data-android/src/test/java/app/tivi/data/repositories/FollowedShowRepositoryTest.kt

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import app.tivi.data.entities.Success
2626
import app.tivi.data.repositories.followedshows.FollowedShowsRepository
2727
import app.tivi.data.repositories.followedshows.TraktFollowedShowsDataSource
2828
import app.tivi.utils.SuccessFakeShowDataSource
29+
import app.tivi.utils.SuccessFakeShowImagesDataSource
2930
import app.tivi.utils.followedShow1Local
3031
import app.tivi.utils.followedShow1Network
3132
import app.tivi.utils.followedShow1PendingDelete
@@ -38,7 +39,7 @@ import app.tivi.utils.show
3839
import app.tivi.utils.show2
3940
import io.mockk.coEvery
4041
import javax.inject.Inject
41-
import kotlinx.coroutines.test.runBlockingTest
42+
import kotlinx.coroutines.runBlocking
4243
import org.hamcrest.MatcherAssert.assertThat
4344
import org.hamcrest.Matchers.`is`
4445
import org.junit.Before
@@ -59,26 +60,25 @@ class FollowedShowRepositoryTest {
5960

6061
@Before
6162
fun setup() {
62-
val fakeShowDataSource = SuccessFakeShowDataSource()
63-
6463
DaggerTestComponent.builder()
6564
.testDataSourceModule(
6665
TestDataSourceModule(
67-
traktShowDataSource = fakeShowDataSource,
68-
tmdbShowDataSource = fakeShowDataSource
66+
traktShowDataSource = SuccessFakeShowDataSource,
67+
tmdbShowDataSource = SuccessFakeShowDataSource,
68+
tmdbShowImagesDataSource = SuccessFakeShowImagesDataSource
6969
)
7070
)
7171
.build()
7272
.inject(this)
7373

74-
runBlockingTest {
74+
runBlocking {
7575
// We'll assume that there's a show in the db
7676
insertShow(database)
7777
}
7878
}
7979

8080
@Test
81-
fun testSync() = runBlockingTest {
81+
fun testSync() = runBlocking {
8282
coEvery { traktDataSource.getFollowedListId() } returns Success(0)
8383
coEvery { traktDataSource.getListShows(0) } returns Success(listOf(followedShow1Network to show))
8484

@@ -89,7 +89,7 @@ class FollowedShowRepositoryTest {
8989
}
9090

9191
@Test
92-
fun testSync_emptyResponse() = runBlockingTest {
92+
fun testSync_emptyResponse() = runBlocking {
9393
insertFollowedShow(database)
9494

9595
coEvery { traktDataSource.getFollowedListId() } returns Success(0)
@@ -102,7 +102,7 @@ class FollowedShowRepositoryTest {
102102
}
103103

104104
@Test
105-
fun testSync_responseDifferentShow() = runBlockingTest {
105+
fun testSync_responseDifferentShow() = runBlocking {
106106
insertFollowedShow(database)
107107

108108
coEvery { traktDataSource.getFollowedListId() } returns Success(0)
@@ -115,7 +115,7 @@ class FollowedShowRepositoryTest {
115115
}
116116

117117
@Test
118-
fun testSync_pendingDelete() = runBlockingTest {
118+
fun testSync_pendingDelete() = runBlocking {
119119
followShowsDao.insert(followedShow1PendingDelete)
120120

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

130130
@Test
131-
fun testSync_pendingAdd() = runBlockingTest {
131+
fun testSync_pendingAdd() = runBlocking {
132132
followShowsDao.insert(followedShow1PendingUpload)
133133

134134
// Return an error for the list ID so that we disable syncing

data-android/src/test/java/app/tivi/data/repositories/SeasonsEpisodesRepositoryTest.kt

+12-12
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import app.tivi.utils.showId
4343
import io.mockk.coEvery
4444
import javax.inject.Inject
4545
import kotlinx.coroutines.flow.produceIn
46-
import kotlinx.coroutines.test.runBlockingTest
46+
import kotlinx.coroutines.runBlocking
4747
import kotlinx.coroutines.withTimeout
4848
import org.hamcrest.MatcherAssert.assertThat
4949
import org.hamcrest.Matchers.`is`
@@ -75,14 +75,14 @@ class SeasonsEpisodesRepositoryTest {
7575
.build()
7676
.inject(this)
7777

78-
runBlockingTest {
78+
runBlocking {
7979
// We'll assume that there's a show in the db
8080
insertShow(database)
8181
}
8282
}
8383

8484
@Test
85-
fun testSyncEpisodeWatches() = runBlockingTest {
85+
fun testSyncEpisodeWatches() = runBlocking {
8686
seasonsDao.insert(s1)
8787
episodesDao.insertAll(s1_episodes)
8888

@@ -96,7 +96,7 @@ class SeasonsEpisodesRepositoryTest {
9696
}
9797

9898
@Test
99-
fun testEpisodeWatches_sameEntries() = runBlockingTest {
99+
fun testEpisodeWatches_sameEntries() = runBlocking {
100100
seasonsDao.insert(s1)
101101
episodesDao.insertAll(s1_episodes)
102102

@@ -112,7 +112,7 @@ class SeasonsEpisodesRepositoryTest {
112112
}
113113

114114
@Test
115-
fun testEpisodeWatches_deletesMissing() = runBlockingTest {
115+
fun testEpisodeWatches_deletesMissing() = runBlocking {
116116
seasonsDao.insert(s1)
117117
episodesDao.insertAll(s1_episodes)
118118

@@ -128,7 +128,7 @@ class SeasonsEpisodesRepositoryTest {
128128
}
129129

130130
@Test
131-
fun testEpisodeWatches_emptyResponse() = runBlockingTest {
131+
fun testEpisodeWatches_emptyResponse() = runBlocking {
132132
seasonsDao.insert(s1)
133133
episodesDao.insertAll(s1_episodes)
134134

@@ -144,7 +144,7 @@ class SeasonsEpisodesRepositoryTest {
144144
}
145145

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

158158
@Test
159-
fun testSyncSeasonsEpisodes_sameEntries() = runBlockingTest {
159+
fun testSyncSeasonsEpisodes_sameEntries() = runBlocking {
160160
seasonsDao.insert(s1)
161161
episodesDao.insertAll(s1_episodes)
162162

@@ -171,7 +171,7 @@ class SeasonsEpisodesRepositoryTest {
171171
}
172172

173173
@Test
174-
fun testSyncSeasonsEpisodes_emptyResponse() = runBlockingTest {
174+
fun testSyncSeasonsEpisodes_emptyResponse() = runBlocking {
175175
seasonsDao.insert(s1)
176176
episodesDao.insertAll(s1_episodes)
177177

@@ -186,7 +186,7 @@ class SeasonsEpisodesRepositoryTest {
186186
}
187187

188188
@Test
189-
fun testSyncSeasonsEpisodes_deletesMissingSeasons() = runBlockingTest {
189+
fun testSyncSeasonsEpisodes_deletesMissingSeasons() = runBlocking {
190190
seasonsDao.insertAll(s1, s2)
191191
episodesDao.insertAll(s1_episodes)
192192
episodesDao.insertAll(s2_episodes)
@@ -202,7 +202,7 @@ class SeasonsEpisodesRepositoryTest {
202202
}
203203

204204
@Test
205-
fun testSyncSeasonsEpisodes_deletesMissingEpisodes() = runBlockingTest {
205+
fun testSyncSeasonsEpisodes_deletesMissingEpisodes() = runBlocking {
206206
seasonsDao.insertAll(s1, s2)
207207
episodesDao.insertAll(s1_episodes)
208208
episodesDao.insertAll(s2_episodes)
@@ -219,7 +219,7 @@ class SeasonsEpisodesRepositoryTest {
219219
}
220220

221221
@Test
222-
fun testObserveNextEpisodeToWatch_singleFlow() = runBlockingTest {
222+
fun testObserveNextEpisodeToWatch_singleFlow() = runBlocking {
223223
seasonsDao.insertAll(s1)
224224
episodesDao.insertAll(s1_episodes)
225225

data-android/src/test/java/app/tivi/utils/SuccessFakeShowDataSource.kt renamed to data-android/src/test/java/app/tivi/utils/fakes.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@
1717
package app.tivi.utils
1818

1919
import app.tivi.data.entities.Result
20+
import app.tivi.data.entities.ShowTmdbImage
2021
import app.tivi.data.entities.Success
2122
import app.tivi.data.entities.TiviShow
2223
import app.tivi.data.repositories.shows.ShowDataSource
24+
import app.tivi.data.repositories.shows.ShowImagesDataSource
2325

24-
class SuccessFakeShowDataSource : ShowDataSource {
26+
object SuccessFakeShowDataSource : ShowDataSource {
2527
override suspend fun getShow(show: TiviShow): Result<TiviShow> {
2628
return Success(show)
2729
}
2830
}
31+
32+
object SuccessFakeShowImagesDataSource : ShowImagesDataSource {
33+
override suspend fun getShowImages(show: TiviShow): Result<List<ShowTmdbImage>> {
34+
return Success(emptyList())
35+
}
36+
}

data/src/main/java/app/tivi/data/repositories/shows/ShowsModule.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import javax.inject.Singleton
3131
import kotlinx.coroutines.async
3232
import kotlinx.coroutines.coroutineScope
3333

34+
@Module(includes = [ShowsModuleBinds::class, ShowStoreModule::class])
35+
class ShowsModule
36+
3437
@Module
3538
internal abstract class ShowsModuleBinds {
3639
@Binds
@@ -46,8 +49,8 @@ internal abstract class ShowsModuleBinds {
4649
abstract fun bindTmdbShowImagesDataSource(source: TmdbShowImagesDataSource): ShowImagesDataSource
4750
}
4851

49-
@Module(includes = [ShowsModuleBinds::class])
50-
class ShowsModule {
52+
@Module
53+
class ShowStoreModule {
5154
@Provides
5255
@Singleton
5356
fun provideShowStore(

0 commit comments

Comments
 (0)