-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
app/src/androidTest/java/com/mohammadfayaz/hn/data/sources/local/LocalStorySourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters