-
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.
Merge pull request #48 from fayaz07/dev
good for mvp
- Loading branch information
Showing
30 changed files
with
291 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
84 changes: 84 additions & 0 deletions
84
app/src/androidTest/java/com/mohammadfayaz/hn/data/sources/local/LocalIdsSourceTest.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,84 @@ | ||
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.assertWithMessage | ||
import com.mohammadfayaz.hn.data.sources.local.source.IdsLocalSource | ||
import com.mohammadfayaz.hn.di.DatabaseModule | ||
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 kotlinx.coroutines.runBlocking | ||
import org.junit.* | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.MethodSorters | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
@HiltAndroidTest | ||
@UninstallModules(DatabaseModule::class) | ||
@RunWith(AndroidJUnit4::class) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
class LocalIdsSourceTest { | ||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule | ||
var instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
@Inject | ||
@Named(FAKE_DB) | ||
lateinit var db: HackerNewsDB | ||
|
||
@Inject | ||
@Named(FAKE_LOCAL_IDS) | ||
lateinit var idsLocalSource: IdsLocalSource | ||
|
||
private val items = mutableListOf<StoryIdModel>() | ||
|
||
@Before | ||
fun init() { | ||
hiltRule.inject() | ||
|
||
items.add(StoryIdModel(1, StoryType.SHOW)) | ||
items.add(StoryIdModel(2, StoryType.ASK)) | ||
items.add(StoryIdModel(3, StoryType.TOP)) | ||
items.add(StoryIdModel(4, StoryType.SHOW)) | ||
items.add(StoryIdModel(5, StoryType.JOB)) | ||
} | ||
|
||
private fun matchItemsCount(type: StoryType, count: Int) = runBlocking { | ||
val items = idsLocalSource.getIdsByStoryType(type) | ||
assertWithMessage("There should be exactly $count story ids of type ${type.string}") | ||
.that(items.size) | ||
.isEqualTo(count) | ||
} | ||
|
||
@Test | ||
fun aThereShouldNotBeAnyItems() = runBlocking { | ||
StoryType.values().asList().forEach { | ||
matchItemsCount(it, 0) | ||
} | ||
} | ||
|
||
@Test | ||
fun bInsertFewStoryIds(): Unit = runBlocking { | ||
idsLocalSource.putIds(items) | ||
|
||
StoryType.values().asList().forEach { type -> | ||
val selected = items.filter { | ||
it.type == type | ||
} | ||
matchItemsCount(type, selected.count()) | ||
} | ||
} | ||
|
||
@After | ||
fun tearDownDb() { | ||
db.close() | ||
} | ||
} |
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() | ||
} | ||
} |
68 changes: 0 additions & 68 deletions
68
app/src/androidTest/java/com/mohammadfayaz/hn/db/DatabaseTest.kt
This file was deleted.
Oops, something went wrong.
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
28 changes: 28 additions & 0 deletions
28
app/src/androidTest/java/com/mohammadfayaz/hn/di/LocalSources.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,28 @@ | ||
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 | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Named | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object LocalSources { | ||
|
||
@Provides | ||
@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) | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/androidTest/java/com/mohammadfayaz/hn/utils/DbConstants.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,9 @@ | ||
package com.mohammadfayaz.hn.utils | ||
|
||
object DbConstants { | ||
const val FAKE_DB: String = "fake_db" | ||
const val FAKE_STORY_DAO: String = "fake_story_dao" | ||
const val FAKE_IDS_DAO: String = "fake_ids_dao" | ||
const val FAKE_FAVOURITES_DAO: String = "fake_favourites_dao" | ||
const val FAKE_COMMENTS_DAO: String = "fake_comments_dao" | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/androidTest/java/com/mohammadfayaz/hn/utils/SourceConstants.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,6 @@ | ||
package com.mohammadfayaz.hn.utils | ||
|
||
object SourceConstants { | ||
const val FAKE_LOCAL_IDS = "l_ids" | ||
const val FAKE_LOCAL_STORIES = "l_stories" | ||
} |
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
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
Oops, something went wrong.