Skip to content

Commit

Permalink
Update WeatherRepository tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaKordic committed Jul 31, 2019
1 parent 3c86346 commit 60072f8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.coroutines.launch
import org.koin.core.KoinComponent

class WeatherViewModel(private val getWeather: GetWeatherUseCase,
private val coroutineContextProvider: CoroutineContextProvider) : ViewModel(), KoinComponent {
private val coroutineContextProvider: CoroutineContextProvider) : ViewModel() {

// we make this private and provide only immutable live data to observers so they can't change anything
private val _weatherLiveData = MutableLiveData<ViewState<WeatherInfo>>()
Expand Down
4 changes: 2 additions & 2 deletions data/src/main/java/com/example/data/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.data.di

import com.example.data.repository.weather.WeatherRepositoryImpl
import com.example.data.utils.Connectivity
import com.example.data.utils.ConnectivityImpl
import com.example.domain.repository.WeatherRepository
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module

val repositoryModule = module {
factory<WeatherRepository> { WeatherRepositoryImpl(get(), get(), get()) }
factory { Connectivity(androidContext()) }
factory { ConnectivityImpl(androidContext()) }
}
11 changes: 2 additions & 9 deletions data/src/main/java/com/example/data/utils/Connectivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package com.example.data.utils

import android.content.Context
import android.net.ConnectivityManager

class Connectivity(private val context: Context) {
interface Connectivity {

fun hasNetworkAccess(): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val info = connectivityManager.activeNetworkInfo
return info != null && info.isConnected
}
fun hasNetworkAccess(): Boolean
}
13 changes: 13 additions & 0 deletions data/src/main/java/com/example/data/utils/ConnectivityImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.data.utils

import android.content.Context
import android.net.ConnectivityManager

class ConnectivityImpl(private val context: Context) : Connectivity {

override fun hasNetworkAccess(): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val info = connectivityManager.activeNetworkInfo
return info != null && info.isConnected
}
}
6 changes: 4 additions & 2 deletions data/src/test/java/com/example/data/WeatherRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class WeatherRepositoryTest {

private val weatherTestApi: WeatherApi = mock()
private val weatherDao: WeatherDao = mock()
private val weatherRepository = WeatherRepositoryImpl(weatherTestApi, weatherDao)
private val connectivity: Connectivity = mock()
private val weatherRepository = WeatherRepositoryImpl(weatherTestApi, weatherDao, connectivity)

@Test
fun `test getWeatherForLocation calls api and saves data to db upon success`() {
runBlocking {
whenever(connectivity.hasNetworkAccess()).thenReturn(true)
whenever(weatherTestApi.getWeatherForLocation(OSIJEK_CITY_NAME)).thenReturn(Response.success(successWeatherInfoResponse))
whenever(weatherDao.updateWeatherAndReturn(successWeatherInfoResponse.mapToRoomEntity())).thenReturn(fakeWeatherEntity)
weatherRepository.getWeatherForLocation(OSIJEK_CITY_NAME)
Expand All @@ -29,11 +31,11 @@ class WeatherRepositoryTest {
@Test
fun `test getWeatherForLocation calls api and returns cached data from db upon failure`() {
runBlocking {
whenever(connectivity.hasNetworkAccess()).thenReturn(true)
whenever(weatherTestApi.getWeatherForLocation(OSIJEK_CITY_NAME))
.thenReturn(Response.error(FAKE_FAILURE_ERROR_CODE, failureResponseBody))
weatherRepository.getWeatherForLocation(OSIJEK_CITY_NAME)
verify(weatherTestApi, times(1)).getWeatherForLocation(OSIJEK_CITY_NAME)
// verify(weatherDao, times(1)).getWeatherInfoForCity(OSIJEK_CITY_NAME)
verify(weatherDao, never()).updateWeatherAndReturn(fakeWeatherEntity)
}
}
Expand Down

0 comments on commit 60072f8

Please sign in to comment.