Skip to content

Commit

Permalink
Added more tests.
Browse files Browse the repository at this point in the history
Imrproved test quality.
  • Loading branch information
aliumujib committed Dec 29, 2019
1 parent 0df6a71 commit 17ee544
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import com.aliumujib.countryflags.cache.mapper.CountryCacheModelMapper
import com.aliumujib.countryflags.cache.room.CountriesDao
import com.aliumujib.countryflags.data.contracts.ICountriesCache
import com.aliumujib.countryflags.data.model.CountryEntity
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Single
import javax.inject.Inject

class CountriesCacheImpl @Inject constructor(
private val countriesDao: CountriesDao,
private val countryCacheModelMapper: CountryCacheModelMapper
) : ICountriesCache {

override fun fetchCountries(): Maybe<List<CountryEntity>> {
override fun fetchAllCountries(): Maybe<List<CountryEntity>> {
return countriesDao.getAllCountries().map {
countryCacheModelMapper.mapFromEntityList(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ import com.aliumujib.countryflags.cache.mapper.CurrencyCacheModelMapper
import com.aliumujib.countryflags.cache.mapper.LanguageCacheModelMapper
import com.aliumujib.countryflags.cache.room.CountriesDB
import com.aliumujib.countryflags.data.contracts.ICountriesCache
import com.aliumujib.countryflags.data.model.CountryEntity
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import io.reactivex.Maybe
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
Expand Down Expand Up @@ -59,21 +53,15 @@ class CountriesCacheImplTest {
fun `check that calling fetchCountries on cache returns data`() {
val data = CacheDataFactory.makeCountryEntityList(10)
countriesCache.saveCountries(data)
val testObserver = countriesCache.fetchCountries().test()
val testObserver = countriesCache.fetchAllCountries().test()
testObserver.assertValue(data)
}

@Test
fun `check that calling fetchCountries on cache completes`() {
val testObserver = countriesCache.fetchCountries().test()
val testObserver = countriesCache.fetchAllCountries().test()
testObserver.assertComplete()
}

private fun stubGetCountriesCacheResponse(
countries: List<CountryEntity>
) {
whenever(countriesCache.fetchCountries())
.thenReturn(Maybe.just(countries))
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.aliumujib.countryflags.data.contracts

import com.aliumujib.countryflags.data.model.CountryEntity
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Single

interface ICountriesCache {

fun fetchCountries(): Maybe<List<CountryEntity>>
fun fetchAllCountries(): Maybe<List<CountryEntity>>

fun saveCountries(countryEntity: List<CountryEntity>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.aliumujib.countryflags.data.model.CountryEntity
import com.aliumujib.countryflags.domain.models.Country
import javax.inject.Inject

class CountryEntityMapper @Inject constructor(
open class CountryEntityMapper @Inject constructor(
private val currencyEntityMapper: CurrencyEntityMapper,
private val languageEntityMapper: LanguageEntityMapper
) : EntityMapper<CountryEntity, Country>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CountriesRepositoryImpl @Inject constructor(
countriesCache.saveCountries(countryEntityMapper.mapToEntityList(it))
}
} else {
countriesCache.fetchCountries().map {
countriesCache.fetchAllCountries().map {
countryEntityMapper.mapFromEntityList(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.test.assertEquals
class CurrencyEntityMapperTest {


var currencyEntityMapper: CurrencyEntityMapper = CurrencyEntityMapper()
private var currencyEntityMapper: CurrencyEntityMapper = CurrencyEntityMapper()

@Before
fun setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.test.assertEquals
@RunWith(JUnit4::class)
class LanguageEntityMapperTest {

var entityMapper: LanguageEntityMapper = LanguageEntityMapper()
private var entityMapper: LanguageEntityMapper = LanguageEntityMapper()

@Before
fun setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import com.aliumujib.countryflags.data.mapper.CountryEntityMapper
import com.aliumujib.countryflags.data.mapper.CurrencyEntityMapper
import com.aliumujib.countryflags.data.mapper.LanguageEntityMapper
import com.aliumujib.countryflags.data.model.CountryEntity
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.never
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import io.reactivex.Maybe
import konveyor.base.randomBuild
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
Expand All @@ -27,6 +30,8 @@ class CountriesRepositoryImplTest {

private val currencyEntityMapper = CurrencyEntityMapper()
private val languageEntityMapper = LanguageEntityMapper()


private val countryEntityMapper =
CountryEntityMapper(currencyEntityMapper, languageEntityMapper)

Expand All @@ -53,31 +58,70 @@ class CountriesRepositoryImplTest {
verify(iCountriesRemote).fetchAllCountries()
}

@Test
fun `check that calling fetchCountries on repository with isConnected as true doesn't call cache implementation`() {
val data = DummyDataFactory.makeCountryEntityList(10)
stubGetCountriesRemoteResponse(data)
countriesRepositoryImpl.fetchCountries(true).test()
verify(iCountriesCache, never()).fetchAllCountries()
}

@Test
fun `check that calling fetchCountries on repository with isConnected as false calls cache implementation`() {
val data = DummyDataFactory.makeCountryEntityList(10)
stubGetCountriesCacheResponse(data)
countriesRepositoryImpl.fetchCountries(false).test()
verify(iCountriesCache).fetchCountries()
verify(iCountriesCache).fetchAllCountries()
}

@Test
fun `check that calling fetchCountries on repository with isConnected as false doesn't call remote implementation`() {
val data = DummyDataFactory.makeCountryEntityList(10)
stubGetCountriesCacheResponse(data)
countriesRepositoryImpl.fetchCountries(false).test()
verify(iCountriesRemote, never()).fetchAllCountries()
}

@Test
fun `check that calling searchCountries on repository completes`() {
val data = DummyDataFactory.makeCountryEntityList(10)
stubSearchCountriesRemoteResponse("deeee",data)
val testObserver = countriesRepositoryImpl.searchCountries("deeee").test()
val query = randomBuild<String>()
stubSearchCountriesRemoteResponse(query, data)
val testObserver = countriesRepositoryImpl.searchCountries(query).test()
testObserver.assertComplete()
}

@Test
fun `check that calling fetchAllCountries on repository when is connected is true saves data when successful`() {
fun `check that calling fetchAllCountries on repository when isConnected is true saves data when successful`() {
val data = DummyDataFactory.makeCountryEntityList(10)
stubGetCountriesRemoteResponse(data)
countriesRepositoryImpl.fetchCountries(true).test()
verify(iCountriesCache).saveCountries(data)
}

@Test
fun `check that calling fetchAllCountries on repository when isConnected is true never saves data when there's an error in the request`() {
val error: Throwable = randomBuild()
stubGetCountriesRemoteError(error)
countriesRepositoryImpl.fetchCountries(true).test()
verify(iCountriesCache, never()).saveCountries(any())
}

@Test
fun `check that calling fetchAllCountries on repository returns an error when there's an error in the request`() {
val error: Throwable = randomBuild()
stubGetCountriesRemoteError(error)
val testObserver = countriesRepositoryImpl.fetchCountries(true).test()
testObserver.assertError(error)
}

@Test
fun `check that calling fetchAllCountries on repository when isConnected is false never saves data `() {
val data = DummyDataFactory.makeCountryEntityList(10)
stubGetCountriesCacheResponse(data)
countriesRepositoryImpl.fetchCountries(false).test()
verify(iCountriesCache, never()).saveCountries(data)
}

@Test
fun `check that calling fetchCountries on repository returns correct data`() {
Expand All @@ -88,12 +132,17 @@ class CountriesRepositoryImplTest {
testObserver.assertValue(mappedData)
}


private fun stubGetCountriesRemoteError(
error: Throwable
) {
whenever(iCountriesRemote.fetchAllCountries())
.thenReturn(Maybe.error(error))
}

private fun stubGetCountriesCacheResponse(
countries: List<CountryEntity>
) {
whenever(iCountriesCache.fetchCountries())
whenever(iCountriesCache.fetchAllCountries())
.thenReturn(Maybe.just(countries))
}

Expand All @@ -105,7 +154,7 @@ class CountriesRepositoryImplTest {
}

private fun stubSearchCountriesRemoteResponse(
query:String,
query: String,
countries: List<CountryEntity>
) {
whenever(iCountriesRemote.searchCountries(query))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.atMost
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Single
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -46,7 +44,6 @@ class FetchAllCountriesTest {

@Test(expected = IllegalArgumentException::class)
fun `confirm that calling fetchAllCountries without params returns an error`() {
val country = CountriesDataFactory.makeCountry()
val countries = CountriesDataFactory.makeCountryList(3)
stubFetchAllCountries(countries)
val testObserver =
Expand All @@ -64,7 +61,6 @@ class FetchAllCountriesTest {

@Test
fun `confirm that calling fetchAllCountries calls CountriesRepository fetchAllCountries() method only once`() {
val country = CountriesDataFactory.makeCountry()
val countries = CountriesDataFactory.makeCountryList(3)
stubFetchAllCountries(countries)
fetchAllCountries.buildUseCaseMaybe(FetchAllCountries.Params(true)).test()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import com.aliumujib.countryflags.domain.models.Country
import com.aliumujib.countryflags.domain.repositories.countries.ICountriesRepository
import com.aliumujib.countryflags.domain.test.CountriesDataFactory
import com.aliumujib.countryflags.domain.util.TestPostExecutionThread
import com.nhaarman.mockito_kotlin.*
import com.nhaarman.mockito_kotlin.atMost
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import io.reactivex.Maybe
import konveyor.base.randomBuild
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -68,9 +71,10 @@ class SearchCountriesTest {

@Test
fun `confirm that calling searchCountries completes`() {
stubSearchCountries("anything", CountriesDataFactory.makeCountryList(10))
val query = randomBuild<String>()
stubSearchCountries(query, CountriesDataFactory.makeCountryList(10))
val testObserver =
searchCountries.buildUseCaseMaybe(SearchCountries.Params("anything")).test()
searchCountries.buildUseCaseMaybe(SearchCountries.Params(query)).test()
testObserver.assertComplete()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.aliumujib.countryflags.navigator

import android.os.Bundle
import com.aliumujib.countryflags.models.CountryModel

interface Navigator {
fun goToDetailScreen(countryModel: CountryModel)
fun goBack()
fun start(savedInstanceState: Bundle?)
fun showCountryList()
fun setupActionBar()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aliumujib.countryflags.navigator

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
Expand Down Expand Up @@ -73,4 +74,12 @@ class NavigatorImpl @Inject constructor(
fragmentManager.popBackStack()
}

override fun start(savedInstanceState: Bundle?) {
if (savedInstanceState == null) {
this.showCountryList()
} else {
this.setupActionBar()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ class AllCountriesActivity : AppCompatActivity(), HasSupportFragmentInjector {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

setSupportActionBar(toolbar)

if (savedInstanceState == null) {
navigator.showCountryList()
} else {
navigator.setupActionBar()
}
navigator.start(savedInstanceState)

}

Expand Down
Loading

0 comments on commit 17ee544

Please sign in to comment.