forked from ffgiraldez/reactive-mvvm-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✅ create suggestion & search viewmodel test suite (ffgiraldez#28)
- Loading branch information
1 parent
c213837
commit 5dc2352
Showing
11 changed files
with
249 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
.DS_Store | ||
/build | ||
/captures | ||
*.iml | ||
*.iml | ||
*/.kotlintest |
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
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
79 changes: 79 additions & 0 deletions
79
app/src/test/java/es/ffgiraldez/comicsearch/comic/gen/EntitiesGen.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,79 @@ | ||
package es.ffgiraldez.comicsearch.comic.gen | ||
|
||
import arrow.core.Either | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
import es.ffgiraldez.comicsearch.comics.domain.Volume | ||
import es.ffgiraldez.comicsearch.platform.left | ||
import es.ffgiraldez.comicsearch.platform.right | ||
import io.kotlintest.properties.Gen | ||
|
||
class ComicErrorGenerator : Gen<ComicError> { | ||
override fun constants(): Iterable<ComicError> = emptyList() | ||
|
||
override fun random(): Sequence<ComicError> = generateSequence { | ||
takeIf { Gen.bool().random().first() } | ||
?.let { ComicError.EmptyResultsError } ?: ComicError.NetworkError | ||
} | ||
} | ||
|
||
class QueryGenerator : Gen<String> { | ||
override fun constants(): Iterable<String> = Gen.string().constants().filter { it.isNotEmpty() } | ||
|
||
override fun random(): Sequence<String> = Gen.string().random().filter { it.isNotEmpty() } | ||
} | ||
|
||
class SuggestionGenerator : Gen<Either<ComicError, List<String>>> { | ||
override fun constants(): Iterable<Either<ComicError, List<String>>> = emptyList() | ||
|
||
override fun random(): Sequence<Either<ComicError, List<String>>> = generateSequence { | ||
generateEither(Gen.bool().random().first()) | ||
} | ||
|
||
private fun generateEither(it: Boolean): Either<ComicError, List<String>> { | ||
return if (it) { | ||
left(Gen.comicError().random().first()) | ||
} else { | ||
right((1..10).fold(emptyList()) { acc, _ -> acc + Gen.query().random().iterator().next() }) | ||
} | ||
} | ||
} | ||
|
||
class VolumeGenerator : Gen<Volume> { | ||
override fun constants(): Iterable<Volume> = emptyList() | ||
|
||
override fun random(): Sequence<Volume> = generateSequence { | ||
Volume( | ||
Gen.string().random().first(), | ||
Gen.string().random().first(), | ||
Gen.string().random().first() | ||
) | ||
} | ||
|
||
} | ||
|
||
class SearchGenerator : Gen<Either<ComicError, List<Volume>>> { | ||
override fun constants(): Iterable<Either<ComicError, List<Volume>>> = emptyList() | ||
|
||
override fun random(): Sequence<Either<ComicError, List<Volume>>> = generateSequence { | ||
generateEither(Gen.bool().random().first()) | ||
} | ||
|
||
private fun generateEither(it: Boolean): Either<ComicError, List<Volume>> { | ||
return if (it) { | ||
left(Gen.comicError().random().first()) | ||
} else { | ||
right((1..10).fold(emptyList()) { acc, _ -> acc + Gen.volume().random().iterator().next() }) | ||
} | ||
} | ||
} | ||
|
||
|
||
fun Gen.Companion.suggestions(): Gen<Either<ComicError, List<String>>> = SuggestionGenerator() | ||
|
||
fun Gen.Companion.search(): Gen<Either<ComicError, List<Volume>>> = SearchGenerator() | ||
|
||
fun Gen.Companion.comicError(): Gen<ComicError> = ComicErrorGenerator() | ||
|
||
fun Gen.Companion.query(): Gen<String> = QueryGenerator() | ||
|
||
fun Gen.Companion.volume(): Gen<Volume> = VolumeGenerator() |
44 changes: 44 additions & 0 deletions
44
app/src/test/java/es/ffgiraldez/comicsearch/query/search/presentation/SearchViewModelSpec.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,44 @@ | ||
package es.ffgiraldez.comicsearch.query.search.presentation | ||
|
||
import arrow.core.Either | ||
import com.nhaarman.mockitokotlin2.mock | ||
import es.ffgiraldez.comicsearch.comic.gen.query | ||
import es.ffgiraldez.comicsearch.comic.gen.search | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
import es.ffgiraldez.comicsearch.comics.domain.Volume | ||
import es.ffgiraldez.comicsearch.platform.toFlowable | ||
import es.ffgiraldez.comicsearch.query.base.presentation.QueryViewState | ||
import es.ffgiraldez.comicsearch.query.base.presentation.toViewState | ||
import io.kotlintest.properties.Gen | ||
import io.kotlintest.properties.assertAll | ||
import io.kotlintest.specs.StringSpec | ||
import io.reactivex.Flowable | ||
import org.mockito.ArgumentMatchers.anyString | ||
|
||
class SearchViewModelSpec : | ||
StringSpec({ | ||
"Search ViewModel should trigger search for a query" { | ||
assertAll(Gen.search(), Gen.query()) { results, query -> | ||
val viewModel = givenSuggestionViewModel(results) | ||
val observer = viewModel.state.toFlowable().test() | ||
val viewState = results.toViewState() | ||
|
||
viewModel.inputQuery(query) | ||
|
||
observer.assertNotComplete() | ||
.assertNoErrors() | ||
.assertValues(QueryViewState.idle(), QueryViewState.loading(), viewState) | ||
} | ||
} | ||
|
||
|
||
}) | ||
|
||
private fun SearchViewModel.inputQuery(input: String) { | ||
query.value = input | ||
} | ||
|
||
private fun givenSuggestionViewModel(results: Either<ComicError, List<Volume>>): SearchViewModel = | ||
SearchViewModel.invoke(mock { | ||
on { findByTerm(anyString()) }.thenReturn(Flowable.just(results)) | ||
}) |
59 changes: 59 additions & 0 deletions
59
...st/java/es/ffgiraldez/comicsearch/query/sugestion/presentation/SuggestionViewModelSpec.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,59 @@ | ||
package es.ffgiraldez.comicsearch.query.sugestion.presentation | ||
|
||
import arrow.core.Either | ||
import com.nhaarman.mockitokotlin2.mock | ||
import es.ffgiraldez.comicsearch.comic.gen.query | ||
import es.ffgiraldez.comicsearch.comic.gen.suggestions | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
import es.ffgiraldez.comicsearch.platform.toFlowable | ||
import es.ffgiraldez.comicsearch.query.base.presentation.QueryViewState | ||
import es.ffgiraldez.comicsearch.query.base.presentation.toViewState | ||
import io.kotlintest.properties.Gen | ||
import io.kotlintest.properties.assertAll | ||
import io.kotlintest.provided.ProjectConfig | ||
import io.kotlintest.specs.StringSpec | ||
import io.reactivex.Flowable | ||
import org.mockito.ArgumentMatchers.anyString | ||
import java.util.concurrent.TimeUnit.SECONDS | ||
|
||
class SuggestionViewModelSpec : | ||
StringSpec({ | ||
"Suggestion ViewModel should not trigger search for empty query" { | ||
assertAll(Gen.suggestions()) { suggestions -> | ||
val viewModel = givenSuggestionViewModel(suggestions) | ||
val observer = viewModel.state.toFlowable().test() | ||
|
||
viewModel.inputQuery("") | ||
|
||
observer.assertNotComplete() | ||
.assertNoErrors() | ||
.assertValues(QueryViewState.idle()) | ||
} | ||
} | ||
|
||
"Suggestion ViewModel should trigger search for a valid query" { | ||
assertAll(Gen.suggestions(), Gen.query()) { suggestions, query -> | ||
val viewModel = givenSuggestionViewModel(suggestions) | ||
val observer = viewModel.state.toFlowable().test() | ||
val viewState = suggestions.toViewState() | ||
|
||
viewModel.inputQuery(query) | ||
|
||
observer.assertNotComplete() | ||
.assertNoErrors() | ||
.assertValues(QueryViewState.idle(), QueryViewState.loading(), viewState) | ||
} | ||
} | ||
|
||
|
||
}) | ||
|
||
private fun SuggestionViewModel.inputQuery(input: String) { | ||
query.value = input | ||
ProjectConfig.testScheduler.advanceTimeBy(10, SECONDS) | ||
} | ||
|
||
private fun givenSuggestionViewModel(suggestions: Either<ComicError, List<String>>): SuggestionViewModel = | ||
SuggestionViewModel.invoke(mock { | ||
on { findByTerm(anyString()) }.thenReturn(Flowable.just(suggestions)) | ||
}) |
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,44 @@ | ||
package io.kotlintest.provided | ||
|
||
import androidx.arch.core.executor.ArchTaskExecutor | ||
import androidx.arch.core.executor.TaskExecutor | ||
import io.kotlintest.AbstractProjectConfig | ||
import io.reactivex.plugins.RxJavaPlugins | ||
import io.reactivex.schedulers.Schedulers | ||
import io.reactivex.schedulers.TestScheduler | ||
|
||
object ProjectConfig : AbstractProjectConfig() { | ||
|
||
override fun parallelism(): Int = 2 | ||
|
||
override fun beforeAll() { | ||
super.beforeAll() | ||
ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() { | ||
override fun executeOnDiskIO(runnable: Runnable) { | ||
runnable.run() | ||
} | ||
|
||
override fun postToMainThread(runnable: Runnable) { | ||
runnable.run() | ||
} | ||
|
||
override fun isMainThread(): Boolean { | ||
return true | ||
} | ||
}) | ||
|
||
RxJavaPlugins.reset() | ||
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() } | ||
RxJavaPlugins.setNewThreadSchedulerHandler { Schedulers.trampoline() } | ||
RxJavaPlugins.setComputationSchedulerHandler { testScheduler } | ||
|
||
} | ||
|
||
val testScheduler = TestScheduler() | ||
|
||
override fun afterAll() { | ||
super.afterAll() | ||
ArchTaskExecutor.getInstance().setDelegate(null) | ||
RxJavaPlugins.reset() | ||
} | ||
} |
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