Skip to content

Commit

Permalink
added local story source tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fayaz07 committed Jan 25, 2022
1 parent 1516496 commit e15ddbf
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ package com.mohammadfayaz.hn.data.sources.local

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import com.mohammadfayaz.hn.data.sources.local.source.IdsLocalSource
import com.mohammadfayaz.hn.di.DatabaseModule
import com.mohammadfayaz.hn.utils.SourceConstants.FAKE_LOCAL_IDS
import com.mohammadfayaz.hn.domain.models.StoryIdModel
import com.mohammadfayaz.hn.domain.models.StoryType
import com.mohammadfayaz.hn.utils.DbConstants.FAKE_DB
import com.mohammadfayaz.hn.utils.SourceConstants.FAKE_LOCAL_IDS
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.UninstallModules
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.junit.*
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters
import java.lang.Thread.sleep
import javax.inject.Inject
import javax.inject.Named

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.mohammadfayaz.hn.data.sources.local

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.mohammadfayaz.hn.data.sources.local.source.StoriesLocalSource
import com.mohammadfayaz.hn.di.DatabaseModule
import com.mohammadfayaz.hn.domain.models.StoryModel
import com.mohammadfayaz.hn.domain.models.StoryType
import com.mohammadfayaz.hn.utils.DbConstants
import com.mohammadfayaz.hn.utils.SourceConstants
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.UninstallModules
import kotlinx.coroutines.runBlocking
import org.junit.*
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters
import javax.inject.Inject
import javax.inject.Named
import kotlin.random.Random

@HiltAndroidTest
@UninstallModules(DatabaseModule::class)
@RunWith(AndroidJUnit4::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class LocalStorySourceTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()

@Inject
@Named(DbConstants.FAKE_DB)
lateinit var db: HackerNewsDB

@Inject
@Named(SourceConstants.FAKE_LOCAL_STORIES)
lateinit var storiesLocalSource: StoriesLocalSource

@Before
fun init() {
hiltRule.inject()
}

@Test
fun storiesTest() = runBlocking {
for (i in 0..6) {
val id = Random.nextInt(0, 100)
val randomTypeIndex = Random.nextInt(0, StoryType.values().size)
val type = StoryType.values()[randomTypeIndex]

storiesLocalSource.put(StoryModel(id, "story"), type)

val story = storiesLocalSource.getById(id)
assertThat(story).isNotEqualTo(null)
assertThat(story?.id).isEqualTo(id)
assertThat(story?.storyType).isEqualTo(type)
}
}

@After
fun tearDownDb() {
db.close()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.mohammadfayaz.hn.di

import com.mohammadfayaz.hn.data.sources.local.dao.IdsDao
import com.mohammadfayaz.hn.data.sources.local.dao.StoryDao
import com.mohammadfayaz.hn.data.sources.local.source.IdsLocalSource
import com.mohammadfayaz.hn.data.sources.local.source.StoriesLocalSource
import com.mohammadfayaz.hn.utils.DbConstants.FAKE_IDS_DAO
import com.mohammadfayaz.hn.utils.DbConstants.FAKE_STORY_DAO
import com.mohammadfayaz.hn.utils.SourceConstants.FAKE_LOCAL_IDS
import com.mohammadfayaz.hn.utils.SourceConstants.FAKE_LOCAL_STORIES
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -18,4 +22,7 @@ object LocalSources {
@Named(FAKE_LOCAL_IDS)
fun provideIdsLocalSource(@Named(FAKE_IDS_DAO) idsDao: IdsDao) = IdsLocalSource(idsDao)

@Provides
@Named(FAKE_LOCAL_STORIES)
fun provideStoriesLocalSource(@Named(FAKE_STORY_DAO) dao: StoryDao) = StoriesLocalSource(dao)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.mohammadfayaz.hn.utils

object SourceConstants {
const val FAKE_LOCAL_IDS = "l_ids"
const val FAKE_LOCAL_STORIES = "l_stories"
}

0 comments on commit e15ddbf

Please sign in to comment.