mirror of
https://github.com/hmalik144/Weather-apps.git
synced 2026-03-18 07:26:04 +00:00
Completion of weather app with new api
- MVVM - SOLID - Atlas Weather Views ported to kotlin
This commit is contained in:
@@ -2,7 +2,7 @@ package com.appttude.h_mal.atlas_weather;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.appttude.h_mal.atlas_weather.mvvm.data.repository
|
||||
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.data.network.WeatherApi
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.data.prefs.PreferenceProvider
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.data.room.AppDatabase
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
private const val MORE_THAN_FIVE_MINS = 310000L
|
||||
private const val LESS_THAN_FIVE_MINS = 290000L
|
||||
|
||||
class RepositoryImplTest {
|
||||
|
||||
lateinit var repository: RepositoryImpl
|
||||
|
||||
@MockK
|
||||
lateinit var api: WeatherApi
|
||||
|
||||
@MockK
|
||||
lateinit var db: AppDatabase
|
||||
|
||||
@MockK
|
||||
lateinit var prefs: PreferenceProvider
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
repository = RepositoryImpl(api, db, prefs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isSearchValid_lessThanFiveMinutes_invalidSearch() {
|
||||
//Arrange
|
||||
val location = "CurrentLocation"
|
||||
|
||||
//Act
|
||||
every { prefs.getLastSavedAt(location) } returns (System.currentTimeMillis() - LESS_THAN_FIVE_MINS)
|
||||
|
||||
//Assert
|
||||
val valid: Boolean = repository.isSearchValid(location)
|
||||
assertEquals(valid, false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isSearchValid_moreThanFiveMinutes_validSearch() {
|
||||
//Arrange
|
||||
val location = "CurrentLocation"
|
||||
|
||||
//Act
|
||||
every { prefs.getLastSavedAt(location) } returns (System.currentTimeMillis() - MORE_THAN_FIVE_MINS)
|
||||
|
||||
// Assert
|
||||
val valid: Boolean = repository.isSearchValid(location)
|
||||
assertEquals(valid, true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isSearchValid_noPreviousValue_validSearch() {
|
||||
//Arrange
|
||||
val location = "CurrentLocation"
|
||||
|
||||
//Act
|
||||
every { prefs.getLastSavedAt(location) } returns 0L
|
||||
|
||||
// Assert
|
||||
val valid: Boolean = repository.isSearchValid(location)
|
||||
assertEquals(valid, true)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.appttude.h_mal.atlas_weather.mvvm.viewmodel
|
||||
|
||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.data.location.LocationProvider
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.data.network.response.forecast.WeatherResponse
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.data.repository.Repository
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.data.room.entity.EntityItem
|
||||
import com.appttude.h_mal.atlas_weather.mvvm.utils.Event
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.impl.annotations.RelaxedMockK
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TestRule
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class MainViewModelTest{
|
||||
|
||||
@get:Rule
|
||||
var rule: TestRule = InstantTaskExecutorRule()
|
||||
|
||||
lateinit var mainViewModel: MainViewModel
|
||||
|
||||
@MockK(relaxed = true)
|
||||
lateinit var repository: Repository
|
||||
|
||||
@MockK
|
||||
lateinit var locationProvider: LocationProvider
|
||||
|
||||
@RelaxedMockK
|
||||
lateinit var mockedLiveData: LiveData<EntityItem>
|
||||
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
mainViewModel = MainViewModel(locationProvider, repository)
|
||||
every { repository.loadCurrentWeatherFromRoom("CurrentLocation") } answers { mockedLiveData }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fetchData_workingLocation_successfulOperation() = runBlocking{
|
||||
val observer = mockk<Observer<Event<Boolean>>>(relaxed = true)
|
||||
val mockResponse = mockk<WeatherResponse>()
|
||||
mainViewModel.operationState.observeForever(observer)
|
||||
|
||||
// Act
|
||||
every { observer.onChanged(Event(false)) }
|
||||
every { locationProvider.getLatLong() } returns Pair(0.00, 0.00)
|
||||
coEvery { repository.getWeatherFromApi("0.00", "0.00") } returns mockResponse
|
||||
|
||||
|
||||
// Assert
|
||||
mainViewModel.fetchData()
|
||||
delay(50)
|
||||
assertEquals(mainViewModel.operationState.value?.getContentIfNotHandled(), false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user