Skip to content
Permalink
Browse files

Convert rest of tests to use Dagger components

  • Loading branch information...
chrisbanes committed Oct 6, 2019
1 parent 31f6486 commit 7c90dd92d3fed14cb4bd7eceec50e89339f9ff7a
@@ -18,13 +18,15 @@ package app.tivi.data

import android.content.Context
import androidx.room.Room
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.entities.Success
import app.tivi.data.entities.TiviShow
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.TraktSeasonsEpisodesDataSource
import app.tivi.data.repositories.episodes.SeasonsEpisodesDataSource
import app.tivi.data.repositories.followedshows.TraktFollowedShowsDataSource
import app.tivi.data.repositories.shows.ShowDataSource
import app.tivi.inject.Trakt
@@ -39,33 +41,28 @@ import dagger.Binds
import dagger.Component
import dagger.Module
import dagger.Provides
import io.mockk.coEvery
import io.mockk.mockk
import javax.inject.Singleton

@Singleton
@Component(modules = [
TestDataSourceModule::class,
TestDatabaseModule::class,
TestContextModule::class
TestDatabaseModule::class
])
interface TestComponent {
fun inject(test: SeasonsEpisodesRepositoryTest)
fun inject(test: FollowedShowRepositoryTest)
}

@Module
class TestContextModule(private val context: Context) {
@Provides
fun provideContext(): Context = context
fun inject(test: EpisodesTest)
fun inject(test: EpisodeWatchEntryTest)
fun inject(test: SeasonsTest)
}

@Module
class TestDataSourceModule(
private val traktFollowedShowsDataSource: TraktFollowedShowsDataSource = mockk(),
private val traktEpisodeDataSource: EpisodeDataSource = mockk(),
private val tmdbEpisodeDataSource: EpisodeDataSource = mockk(),
private val seasonsDataSource: TraktSeasonsEpisodesDataSource = mockk(),
private val seasonsDataSource: SeasonsEpisodesDataSource = mockk(),
private val traktShowDataSource: ShowDataSource = mockk(),
private val tmdbShowDataSource: ShowDataSource = mockk()
) {
@@ -81,8 +78,7 @@ class TestDataSourceModule(
fun provideTmdbEpisodeDataSource() = tmdbEpisodeDataSource

@Provides
@Trakt
fun provideTraktSeasonsEpisodesDataSource() = seasonsDataSource
fun provideSeasonsEpisodesDataSource() = seasonsDataSource

@Provides
@Trakt
@@ -108,6 +104,13 @@ class TestDatabaseModule {

@Provides
fun provideTraktAuthState() = TraktAuthState.LOGGED_IN

@Provides
fun provideContext(): Context = ApplicationProvider.getApplicationContext()

@Singleton
@Provides
fun provideLogger(): Logger = mockk(relaxUnitFun = true)
}

@Module
@@ -123,10 +126,6 @@ class TestRoomDatabaseModule {
@Singleton
@Provides
fun provideDatabaseTransactionRunner(): DatabaseTransactionRunner = TestTransactionRunner

@Singleton
@Provides
fun provideLogger(): Logger = mockk(relaxUnitFun = true)
}

@Module
@@ -17,8 +17,13 @@
package app.tivi.data.dao

import android.database.sqlite.SQLiteConstraintException
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import app.tivi.data.DaggerTestComponent
import app.tivi.data.TestDataSourceModule
import app.tivi.data.TiviDatabase
import app.tivi.data.daos.EpisodeWatchEntryDao
import app.tivi.utils.BaseDatabaseTest
import app.tivi.data.daos.EpisodesDao
import app.tivi.data.daos.SeasonsDao
import app.tivi.utils.episodeWatch2PendingDelete
import app.tivi.utils.episodeWatch2PendingSend
import app.tivi.utils.insertShow
@@ -32,21 +37,35 @@ import kotlinx.coroutines.test.runBlockingTest
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.nullValue
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import javax.inject.Inject

class EpisodeWatchEntryTest : BaseDatabaseTest() {
private lateinit var episodeWatchEntryDao: EpisodeWatchEntryDao
@RunWith(RobolectricTestRunner::class)
class EpisodeWatchEntryTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

override fun setup() {
super.setup()
@Inject lateinit var database: TiviDatabase
@Inject lateinit var episodesDao: EpisodesDao
@Inject lateinit var seasonsDao: SeasonsDao
@Inject lateinit var episodeWatchEntryDao: EpisodeWatchEntryDao

episodeWatchEntryDao = db.episodeWatchesDao()
@Before
fun setup() {
DaggerTestComponent.builder()
.testDataSourceModule(TestDataSourceModule())
.build()
.inject(this)

runBlockingTest {
// We'll assume that there's a show, season and s1_episodes in the db
insertShow(db)
db.seasonsDao().insert(s1)
db.episodesDao().insertAll(s1_episodes)
insertShow(database)
seasonsDao.insert(s1)
episodesDao.insertAll(s1_episodes)
}
}

@@ -91,7 +110,7 @@ class EpisodeWatchEntryTest : BaseDatabaseTest() {
fun deleteEpisode_deletesWatch() = runBlockingTest {
episodeWatchEntryDao.insert(s1e1w)
// Now delete episode
db.episodesDao().delete(s1e1)
episodesDao.delete(s1e1)
assertThat(episodeWatchEntryDao.entryWithId(s1e1w_id), `is`(nullValue()))
}
}
@@ -17,8 +17,12 @@
package app.tivi.data.dao

import android.database.sqlite.SQLiteConstraintException
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import app.tivi.data.DaggerTestComponent
import app.tivi.data.TestDataSourceModule
import app.tivi.data.TiviDatabase
import app.tivi.data.daos.EpisodesDao
import app.tivi.utils.BaseDatabaseTest
import app.tivi.data.daos.SeasonsDao
import app.tivi.utils.insertShow
import app.tivi.utils.s1
import app.tivi.utils.s1_episodes
@@ -32,19 +36,33 @@ import kotlinx.coroutines.test.runBlockingTest
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.nullValue
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import javax.inject.Inject

class EpisodesTest : BaseDatabaseTest() {
private lateinit var episodeDao: EpisodesDao
@RunWith(RobolectricTestRunner::class)
class EpisodesTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

override fun setup() {
super.setup()
@Inject lateinit var database: TiviDatabase
@Inject lateinit var episodeDao: EpisodesDao
@Inject lateinit var seasonsDao: SeasonsDao

@Before
fun setup() {
DaggerTestComponent.builder()
.testDataSourceModule(TestDataSourceModule())
.build()
.inject(this)

runBlocking {
episodeDao = db.episodesDao()
// We'll assume that there's a show and season in the db
insertShow(db)
db.seasonsDao().insert(s1)
insertShow(database)
seasonsDao.insert(s1)
}
}

@@ -73,7 +91,7 @@ class EpisodesTest : BaseDatabaseTest() {
fun deleteSeason_deletesEpisode() = runBlockingTest {
episodeDao.insert(s1e1)
// Now delete season
db.seasonsDao().delete(s1)
seasonsDao.delete(s1)
assertThat(episodeDao.episodeWithId(s1e1.id), `is`(nullValue()))
}

@@ -17,8 +17,11 @@
package app.tivi.data.dao

import android.database.sqlite.SQLiteConstraintException
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import app.tivi.data.DaggerTestComponent
import app.tivi.data.TestDataSourceModule
import app.tivi.data.TiviDatabase
import app.tivi.data.daos.SeasonsDao
import app.tivi.utils.BaseDatabaseTest
import app.tivi.utils.deleteShow
import app.tivi.utils.insertShow
import app.tivi.utils.s0
@@ -30,18 +33,31 @@ import kotlinx.coroutines.test.runBlockingTest
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.nullValue
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import javax.inject.Inject

class SeasonsTest : BaseDatabaseTest() {
private lateinit var seasonsDao: SeasonsDao
@RunWith(RobolectricTestRunner::class)
class SeasonsTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

override fun setup() {
super.setup()
@Inject lateinit var database: TiviDatabase
@Inject lateinit var seasonsDao: SeasonsDao

@Before
fun setup() {
DaggerTestComponent.builder()
.testDataSourceModule(TestDataSourceModule())
.build()
.inject(this)

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

@@ -86,7 +102,7 @@ class SeasonsTest : BaseDatabaseTest() {
fun deleteShow_deletesSeason() = runBlockingTest {
seasonsDao.insert(s1)
// Now delete show
deleteShow(db)
deleteShow(database)

assertThat(seasonsDao.seasonWithId(s1_id), `is`(nullValue()))
}
@@ -16,11 +16,8 @@

package app.tivi.data.repositories

import android.content.Context
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import app.tivi.data.DaggerTestComponent
import app.tivi.data.TestContextModule
import app.tivi.data.TestDataSourceModule
import app.tivi.data.TiviDatabase
import app.tivi.data.daos.FollowedShowsDao
@@ -64,12 +61,9 @@ class FollowedShowRepositoryTest {

@Before
fun setup() {
val context = ApplicationProvider.getApplicationContext<Context>()

val fakeShowDataSource = SuccessFakeShowDataSource()

DaggerTestComponent.builder()
.testContextModule(TestContextModule(context))
.testDataSourceModule(
TestDataSourceModule(
traktShowDataSource = fakeShowDataSource,

0 comments on commit 7c90dd9

Please sign in to comment.
You can’t perform that action at this time.