mirror of
https://github.com/hmalik144/Weather-apps.git
synced 2026-03-18 07:26:04 +00:00
Test suite expansion (#20)
- Code inspection - Redundant resources removed - Resources moved the corresponding flavours - Deprecated dependencies upgraded - lint changes - circleci updated to capture screenshot - Testsuite expansion
This commit is contained in:
@@ -1,20 +1,35 @@
|
||||
package com.appttude.h_mal.atlas_weather.data.repository
|
||||
|
||||
import com.appttude.h_mal.atlas_weather.data.network.WeatherApi
|
||||
import com.appttude.h_mal.atlas_weather.data.network.response.forecast.WeatherResponse
|
||||
import com.appttude.h_mal.atlas_weather.data.prefs.LOCATION_CONST
|
||||
import com.appttude.h_mal.atlas_weather.data.prefs.PreferenceProvider
|
||||
import com.appttude.h_mal.atlas_weather.data.room.AppDatabase
|
||||
import com.appttude.h_mal.atlas_weather.data.room.entity.EntityItem
|
||||
import com.appttude.h_mal.atlas_weather.utils.BaseTest
|
||||
import com.nhaarman.mockitokotlin2.any
|
||||
import com.nhaarman.mockitokotlin2.doAnswer
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coJustRun
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.justRun
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers.anyString
|
||||
import retrofit2.Response
|
||||
import java.io.IOException
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertIs
|
||||
|
||||
private const val MORE_THAN_FIVE_MINS = 330000L
|
||||
private const val LESS_THAN_FIVE_MINS = 270000L
|
||||
|
||||
class RepositoryImplTest {
|
||||
class RepositoryImplTest : BaseTest() {
|
||||
|
||||
lateinit var repository: RepositoryImpl
|
||||
|
||||
@@ -71,4 +86,60 @@ class RepositoryImplTest {
|
||||
val valid: Boolean = repository.isSearchValid(location)
|
||||
assertEquals(valid, true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getWeatherFromApi_validLatLong_validSearch() {
|
||||
//Arrange
|
||||
val mockResponse = createSuccessfulRetrofitMock<WeatherResponse>()
|
||||
|
||||
//Act
|
||||
//create a successful retrofit response
|
||||
coEvery { api.getFromApi("", "") }.returns(mockResponse)
|
||||
|
||||
// Assert
|
||||
runBlocking {
|
||||
val result = repository.getWeatherFromApi("", "")
|
||||
assertIs<WeatherResponse>(result)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getWeatherFromApi_validLatLong_invalidResponse() {
|
||||
//Arrange
|
||||
val mockResponse = createErrorRetrofitMock<WeatherResponse>()
|
||||
|
||||
//Act
|
||||
//create a successful retrofit response
|
||||
coEvery { api.getFromApi(any(), any()) } returns (mockResponse)
|
||||
|
||||
// Assert
|
||||
val ioExceptionReturned = assertFailsWith<IOException> {
|
||||
runBlocking {
|
||||
repository.getWeatherFromApi("", "")
|
||||
}
|
||||
}
|
||||
assertEquals(ioExceptionReturned.message, "Error Code: 400")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun loadWeatherList_validResponse() {
|
||||
// Arrange
|
||||
val elements = listOf<EntityItem>(
|
||||
mockk { every { id } returns any() },
|
||||
mockk { every { id } returns any() },
|
||||
mockk { every { id } returns any() },
|
||||
mockk { every { id } returns any() }
|
||||
)
|
||||
|
||||
//Act
|
||||
coEvery { db.getWeatherDao().getWeatherListWithoutCurrent() } returns elements
|
||||
|
||||
// Assert
|
||||
runBlocking {
|
||||
val result = repository.loadWeatherList()
|
||||
assertIs<List<String>>(result)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import com.appttude.h_mal.atlas_weather.data.repository.SettingsRepository
|
||||
import com.appttude.h_mal.atlas_weather.data.room.entity.CURRENT_LOCATION
|
||||
import com.appttude.h_mal.atlas_weather.data.room.entity.EntityItem
|
||||
import com.appttude.h_mal.atlas_weather.model.weather.FullWeather
|
||||
import com.google.gson.Gson
|
||||
import com.appttude.h_mal.atlas_weather.utils.BaseTest
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
@@ -18,9 +18,7 @@ import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.io.IOException
|
||||
|
||||
class ServicesHelperTest {
|
||||
|
||||
private val gson = Gson()
|
||||
class ServicesHelperTest : BaseTest() {
|
||||
|
||||
lateinit var helper: ServicesHelper
|
||||
|
||||
@@ -40,8 +38,7 @@ class ServicesHelperTest {
|
||||
MockKAnnotations.init(this)
|
||||
helper = ServicesHelper(repository, settingsRepository, locationProvider)
|
||||
|
||||
val json = this::class.java.classLoader!!.getResource("weather_sample.json").readText()
|
||||
weatherResponse = gson.fromJson(json, WeatherResponse::class.java)
|
||||
weatherResponse = getTestData("weather_sample.json", WeatherResponse::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,320 +0,0 @@
|
||||
{
|
||||
"lat": 51.5,
|
||||
"lon": -0.12,
|
||||
"timezone": "Europe/London",
|
||||
"timezone_offset": 0,
|
||||
"current": {
|
||||
"dt": 1608391380,
|
||||
"sunrise": 1608364972,
|
||||
"sunset": 1608393158,
|
||||
"temp": 10.53,
|
||||
"feels_like": 5.17,
|
||||
"pressure": 1006,
|
||||
"humidity": 71,
|
||||
"dew_point": 5.5,
|
||||
"uvi": 0.12,
|
||||
"clouds": 39,
|
||||
"visibility": 10000,
|
||||
"wind_speed": 6.2,
|
||||
"wind_deg": 210,
|
||||
"weather": [
|
||||
{
|
||||
"id": 500,
|
||||
"main": "Rain",
|
||||
"description": "light rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"rain": {
|
||||
"1h": 0.19
|
||||
}
|
||||
},
|
||||
"daily": [
|
||||
{
|
||||
"dt": 1608375600,
|
||||
"sunrise": 1608364972,
|
||||
"sunset": 1608393158,
|
||||
"temp": {
|
||||
"day": 11.78,
|
||||
"min": 9.1,
|
||||
"max": 12.31,
|
||||
"night": 9.1,
|
||||
"eve": 10.27,
|
||||
"morn": 10.8
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 6.46,
|
||||
"night": 5.08,
|
||||
"eve": 5.58,
|
||||
"morn": 4.63
|
||||
},
|
||||
"pressure": 1005,
|
||||
"humidity": 69,
|
||||
"dew_point": 6.42,
|
||||
"wind_speed": 6.37,
|
||||
"wind_deg": 217,
|
||||
"weather": [
|
||||
{
|
||||
"id": 500,
|
||||
"main": "Rain",
|
||||
"description": "light rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"clouds": 97,
|
||||
"pop": 0.98,
|
||||
"rain": 1.69,
|
||||
"uvi": 0.53
|
||||
},
|
||||
{
|
||||
"dt": 1608462000,
|
||||
"sunrise": 1608451406,
|
||||
"sunset": 1608479581,
|
||||
"temp": {
|
||||
"day": 9.9,
|
||||
"min": 7.41,
|
||||
"max": 10.52,
|
||||
"night": 7.41,
|
||||
"eve": 8.83,
|
||||
"morn": 8.59
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 4.19,
|
||||
"night": 3.99,
|
||||
"eve": 4.55,
|
||||
"morn": 4.79
|
||||
},
|
||||
"pressure": 1013,
|
||||
"humidity": 64,
|
||||
"dew_point": 3.48,
|
||||
"wind_speed": 6.12,
|
||||
"wind_deg": 226,
|
||||
"weather": [
|
||||
{
|
||||
"id": 500,
|
||||
"main": "Rain",
|
||||
"description": "light rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"clouds": 8,
|
||||
"pop": 0.58,
|
||||
"rain": 0.65,
|
||||
"uvi": 0.45
|
||||
},
|
||||
{
|
||||
"dt": 1608548400,
|
||||
"sunrise": 1608537838,
|
||||
"sunset": 1608566008,
|
||||
"temp": {
|
||||
"day": 11.06,
|
||||
"min": 7.01,
|
||||
"max": 13.57,
|
||||
"night": 13.2,
|
||||
"eve": 12.68,
|
||||
"morn": 8.59
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 6.32,
|
||||
"night": 9.99,
|
||||
"eve": 9.75,
|
||||
"morn": 4.64
|
||||
},
|
||||
"pressure": 1005,
|
||||
"humidity": 91,
|
||||
"dew_point": 9.69,
|
||||
"wind_speed": 6.7,
|
||||
"wind_deg": 185,
|
||||
"weather": [
|
||||
{
|
||||
"id": 501,
|
||||
"main": "Rain",
|
||||
"description": "moderate rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"clouds": 100,
|
||||
"pop": 1,
|
||||
"rain": 7.85,
|
||||
"uvi": 0.21
|
||||
},
|
||||
{
|
||||
"dt": 1608634800,
|
||||
"sunrise": 1608624266,
|
||||
"sunset": 1608652438,
|
||||
"temp": {
|
||||
"day": 12.97,
|
||||
"min": 11.7,
|
||||
"max": 13.21,
|
||||
"night": 11.7,
|
||||
"eve": 12.37,
|
||||
"morn": 12.93
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 11.39,
|
||||
"night": 10.49,
|
||||
"eve": 10.96,
|
||||
"morn": 9.65
|
||||
},
|
||||
"pressure": 1012,
|
||||
"humidity": 83,
|
||||
"dew_point": 10.31,
|
||||
"wind_speed": 2.38,
|
||||
"wind_deg": 214,
|
||||
"weather": [
|
||||
{
|
||||
"id": 500,
|
||||
"main": "Rain",
|
||||
"description": "light rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"clouds": 100,
|
||||
"pop": 1,
|
||||
"rain": 3.25,
|
||||
"uvi": 0.34
|
||||
},
|
||||
{
|
||||
"dt": 1608721200,
|
||||
"sunrise": 1608710690,
|
||||
"sunset": 1608738871,
|
||||
"temp": {
|
||||
"day": 12.28,
|
||||
"min": 10.12,
|
||||
"max": 12.62,
|
||||
"night": 10.12,
|
||||
"eve": 10.12,
|
||||
"morn": 11.73
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 7.48,
|
||||
"night": 6.73,
|
||||
"eve": 6.6,
|
||||
"morn": 8.15
|
||||
},
|
||||
"pressure": 1006,
|
||||
"humidity": 64,
|
||||
"dew_point": 5.76,
|
||||
"wind_speed": 5.45,
|
||||
"wind_deg": 224,
|
||||
"weather": [
|
||||
{
|
||||
"id": 500,
|
||||
"main": "Rain",
|
||||
"description": "light rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"clouds": 97,
|
||||
"pop": 0.94,
|
||||
"rain": 2.52,
|
||||
"uvi": 0.52
|
||||
},
|
||||
{
|
||||
"dt": 1608811200,
|
||||
"sunrise": 1608797112,
|
||||
"sunset": 1608825307,
|
||||
"temp": {
|
||||
"day": 7.3,
|
||||
"min": 4.66,
|
||||
"max": 8.32,
|
||||
"night": 4.66,
|
||||
"eve": 5.76,
|
||||
"morn": 5.85
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 0.36,
|
||||
"night": -1.25,
|
||||
"eve": -0.31,
|
||||
"morn": -2.46
|
||||
},
|
||||
"pressure": 1020,
|
||||
"humidity": 60,
|
||||
"dew_point": 0.15,
|
||||
"wind_speed": 7.09,
|
||||
"wind_deg": 5,
|
||||
"weather": [
|
||||
{
|
||||
"id": 500,
|
||||
"main": "Rain",
|
||||
"description": "light rain",
|
||||
"icon": "10d"
|
||||
}
|
||||
],
|
||||
"clouds": 85,
|
||||
"pop": 0.52,
|
||||
"rain": 0.6,
|
||||
"uvi": 1
|
||||
},
|
||||
{
|
||||
"dt": 1608897600,
|
||||
"sunrise": 1608883530,
|
||||
"sunset": 1608911747,
|
||||
"temp": {
|
||||
"day": 4.12,
|
||||
"min": 2.2,
|
||||
"max": 4.63,
|
||||
"night": 2.97,
|
||||
"eve": 3.44,
|
||||
"morn": 2.28
|
||||
},
|
||||
"feels_like": {
|
||||
"day": -0.33,
|
||||
"night": -0.43,
|
||||
"eve": -0.1,
|
||||
"morn": -2.82
|
||||
},
|
||||
"pressure": 1033,
|
||||
"humidity": 70,
|
||||
"dew_point": -3.09,
|
||||
"wind_speed": 3.35,
|
||||
"wind_deg": 334,
|
||||
"weather": [
|
||||
{
|
||||
"id": 800,
|
||||
"main": "Clear",
|
||||
"description": "clear sky",
|
||||
"icon": "01d"
|
||||
}
|
||||
],
|
||||
"clouds": 0,
|
||||
"pop": 0,
|
||||
"uvi": 1
|
||||
},
|
||||
{
|
||||
"dt": 1608984000,
|
||||
"sunrise": 1608969944,
|
||||
"sunset": 1608998190,
|
||||
"temp": {
|
||||
"day": 6.03,
|
||||
"min": 2.76,
|
||||
"max": 6.92,
|
||||
"night": 6.92,
|
||||
"eve": 6.45,
|
||||
"morn": 3.59
|
||||
},
|
||||
"feels_like": {
|
||||
"day": 0.49,
|
||||
"night": -0.96,
|
||||
"eve": -0.28,
|
||||
"morn": -0.44
|
||||
},
|
||||
"pressure": 1024,
|
||||
"humidity": 69,
|
||||
"dew_point": 0.84,
|
||||
"wind_speed": 5.25,
|
||||
"wind_deg": 251,
|
||||
"weather": [
|
||||
{
|
||||
"id": 804,
|
||||
"main": "Clouds",
|
||||
"description": "overcast clouds",
|
||||
"icon": "04d"
|
||||
}
|
||||
],
|
||||
"clouds": 100,
|
||||
"pop": 0,
|
||||
"uvi": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.appttude.h_mal.atlas_weather.utils
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import okhttp3.ResponseBody
|
||||
import retrofit2.Response
|
||||
|
||||
|
||||
open class BaseTest {
|
||||
private val gson by lazy { Gson() }
|
||||
|
||||
fun <T : Any> getTestData(resourceName: String): T {
|
||||
val json = this::class.java.classLoader!!.getResource(resourceName).readText()
|
||||
val typeToken = object : TypeToken<T>() {}.type
|
||||
return gson.fromJson<T>(json, typeToken)
|
||||
}
|
||||
|
||||
fun <T : Any> getTestData(resourceName: String, cls: Class<T>): T {
|
||||
val json = this::class.java.classLoader!!.getResource(resourceName).readText()
|
||||
return gson.fromJson<T>(json, cls)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> createSuccessfulRetrofitMock(): Response<T> {
|
||||
val mockResponse = mockk<T>()
|
||||
return Response.success(mockResponse)
|
||||
}
|
||||
|
||||
fun <T: Any> createErrorRetrofitMock(code: Int = 400): Response<T> {
|
||||
val responseBody = mockk<ResponseBody>(relaxed = true)
|
||||
val rawResponse = mockk<okhttp3.Response>().also {
|
||||
every { it.code } returns code
|
||||
}
|
||||
return Response.error<T>(code, responseBody)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.appttude.h_mal.atlas_weather.utils
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.TimeoutException
|
||||
|
||||
fun <T> LiveData<T>.getOrAwaitValue(
|
||||
time: Long = 2,
|
||||
timeUnit: TimeUnit = TimeUnit.SECONDS
|
||||
): T {
|
||||
var data: T? = null
|
||||
val latch = CountDownLatch(1)
|
||||
val observer = object : Observer<T> {
|
||||
override fun onChanged(o: T?) {
|
||||
data = o
|
||||
latch.countDown()
|
||||
this@getOrAwaitValue.removeObserver(this)
|
||||
}
|
||||
}
|
||||
|
||||
this.observeForever(observer)
|
||||
|
||||
// Don't wait indefinitely if the LiveData is not set.
|
||||
if (!latch.await(time, timeUnit)) {
|
||||
throw TimeoutException("LiveData value was never set.")
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return data as T
|
||||
}
|
||||
|
||||
fun sleep(millis: Long = 1000) {
|
||||
runBlocking(Dispatchers.Default) { delay(millis) }
|
||||
}
|
||||
@@ -1,26 +1,35 @@
|
||||
package com.appttude.h_mal.atlas_weather.viewmodel
|
||||
|
||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import com.appttude.h_mal.atlas_weather.data.location.LocationProviderImpl
|
||||
import com.appttude.h_mal.atlas_weather.data.network.response.forecast.WeatherResponse
|
||||
import com.appttude.h_mal.atlas_weather.data.repository.Repository
|
||||
import com.appttude.h_mal.atlas_weather.data.room.entity.CURRENT_LOCATION
|
||||
import com.appttude.h_mal.atlas_weather.data.room.entity.EntityItem
|
||||
import com.appttude.h_mal.atlas_weather.model.ViewState
|
||||
import com.appttude.h_mal.atlas_weather.model.types.LocationType
|
||||
import com.appttude.h_mal.atlas_weather.model.weather.FullWeather
|
||||
import com.appttude.h_mal.atlas_weather.utils.BaseTest
|
||||
import com.appttude.h_mal.atlas_weather.utils.getOrAwaitValue
|
||||
import com.appttude.h_mal.atlas_weather.utils.sleep
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.InjectMockKs
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.TimeoutException
|
||||
import java.io.IOException
|
||||
import kotlin.test.assertIs
|
||||
|
||||
|
||||
class WorldViewModelTest : BaseTest() {
|
||||
|
||||
@Suppress("unused")
|
||||
class WorldViewModelTest {
|
||||
@get:Rule
|
||||
val rule = InstantTaskExecutorRule()
|
||||
|
||||
@InjectMockKs
|
||||
lateinit var viewModel: WorldViewModel
|
||||
|
||||
@MockK(relaxed = true)
|
||||
@@ -29,46 +38,102 @@ class WorldViewModelTest {
|
||||
@MockK
|
||||
lateinit var locationProvider: LocationProviderImpl
|
||||
|
||||
private lateinit var weatherResponse: WeatherResponse
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
viewModel = WorldViewModel(locationProvider, repository)
|
||||
|
||||
weatherResponse = getTestData("weather_sample.json", WeatherResponse::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fetchDataForSingleLocation_validLocation_validReturn() {
|
||||
// Arrange
|
||||
val entityItem = EntityItem(CURRENT_LOCATION, FullWeather(weatherResponse).apply {
|
||||
temperatureUnit = "°C"
|
||||
locationString = CURRENT_LOCATION
|
||||
})
|
||||
|
||||
// Act
|
||||
every { repository.isSearchValid(CURRENT_LOCATION) }.returns(true)
|
||||
coEvery { locationProvider.getLatLongFromLocationName(CURRENT_LOCATION) } returns Pair(
|
||||
weatherResponse.lat,
|
||||
weatherResponse.lon
|
||||
)
|
||||
coEvery {
|
||||
repository.getWeatherFromApi(
|
||||
weatherResponse.lat.toString(),
|
||||
weatherResponse.lon.toString()
|
||||
)
|
||||
}.returns(weatherResponse)
|
||||
coEvery {
|
||||
locationProvider.getLocationNameFromLatLong(
|
||||
weatherResponse.lat,
|
||||
weatherResponse.lon,
|
||||
LocationType.City
|
||||
)
|
||||
}.returns(CURRENT_LOCATION)
|
||||
every { repository.saveLastSavedAt(CURRENT_LOCATION) } returns Unit
|
||||
coEvery { repository.saveCurrentWeatherToRoom(entityItem) } returns Unit
|
||||
|
||||
viewModel.fetchDataForSingleLocation(CURRENT_LOCATION)
|
||||
|
||||
// Assert
|
||||
viewModel.uiState.observeForever {
|
||||
println(it.javaClass.name)
|
||||
}
|
||||
|
||||
sleep(3000)
|
||||
assertIs<ViewState.HasData<*>>(viewModel.uiState.getOrAwaitValue())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fetchDataForSingleLocation_invalidLocation_invalidReturn() {
|
||||
// Arrange
|
||||
val location = CURRENT_LOCATION
|
||||
|
||||
// Act
|
||||
every { locationProvider.getLatLongFromLocationName(CURRENT_LOCATION) } throws IOException("Unable to get location")
|
||||
every { repository.isSearchValid(CURRENT_LOCATION) }.returns(true)
|
||||
coEvery {
|
||||
repository.getWeatherFromApi(
|
||||
weatherResponse.lat.toString(),
|
||||
weatherResponse.lon.toString()
|
||||
)
|
||||
}.returns(weatherResponse)
|
||||
|
||||
viewModel.fetchDataForSingleLocation(location)
|
||||
|
||||
assertEquals(viewModel.operationRefresh.getOrAwaitValue()?.getContentIfNotHandled(), false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun <T> LiveData<T>.getOrAwaitValue(
|
||||
time: Long = 2,
|
||||
timeUnit: TimeUnit = TimeUnit.SECONDS
|
||||
): T {
|
||||
var data: T? = null
|
||||
val latch = CountDownLatch(1)
|
||||
val observer = object : Observer<T> {
|
||||
override fun onChanged(o: T?) {
|
||||
data = o
|
||||
latch.countDown()
|
||||
this@getOrAwaitValue.removeObserver(this)
|
||||
}
|
||||
// Assert
|
||||
sleep(300)
|
||||
assertIs<ViewState.HasError<*>>(viewModel.uiState.getOrAwaitValue())
|
||||
}
|
||||
|
||||
this.observeForever(observer)
|
||||
@Test
|
||||
fun searchAboveFallbackTime_validLocation_validReturn() {
|
||||
// Arrange
|
||||
val entityItem = EntityItem(CURRENT_LOCATION, FullWeather(weatherResponse).apply {
|
||||
temperatureUnit = "°C"
|
||||
locationString = CURRENT_LOCATION
|
||||
})
|
||||
|
||||
// Don't wait indefinitely if the LiveData is not set.
|
||||
if (!latch.await(time, timeUnit)) {
|
||||
throw TimeoutException("LiveData value was never set.")
|
||||
// Act
|
||||
coEvery { repository.getSingleWeather(CURRENT_LOCATION) }.returns(entityItem)
|
||||
every { repository.isSearchValid(CURRENT_LOCATION) }.returns(false)
|
||||
coEvery {
|
||||
repository.getWeatherFromApi(
|
||||
weatherResponse.lat.toString(),
|
||||
weatherResponse.lon.toString()
|
||||
)
|
||||
}.returns(weatherResponse)
|
||||
every { repository.saveLastSavedAt(CURRENT_LOCATION) } returns Unit
|
||||
coEvery { repository.saveCurrentWeatherToRoom(entityItem) } returns Unit
|
||||
|
||||
viewModel.fetchDataForSingleLocation(CURRENT_LOCATION)
|
||||
|
||||
// Assert
|
||||
sleep(300)
|
||||
assertIs<ViewState.HasData<*>>(viewModel.uiState.getOrAwaitValue())
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return data as T
|
||||
}
|
||||
Reference in New Issue
Block a user