Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odaridavid committed Mar 14, 2024
1 parent 621306c commit f61a837
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HomeViewModelIntegrationTest {

@MockK
val mockSettingsRepository = mockk<SettingsRepository>(relaxed = true).apply {
coEvery { getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR.value)
coEvery { getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR)
coEvery { getExcludedData() } returns flowOf(ExcludedData.NONE.value)
}

Expand Down Expand Up @@ -77,12 +77,12 @@ class HomeViewModelIntegrationTest {
viewModel.processIntent(HomeScreenIntent.LoadWeatherData)

val expectedState = HomeScreenViewState(
units = "metric",
units = Units.METRIC,
defaultLocation = DefaultLocation(
longitude = 0.0, latitude = 0.0
),
locationName = "-",
language = "English",
language = SupportedLanguage.ENGLISH,
weather = fakeSuccessMappedWeatherResponse,
isLoading = false,
errorMessageId = null
Expand Down Expand Up @@ -113,12 +113,12 @@ class HomeViewModelIntegrationTest {
viewModel.processIntent(HomeScreenIntent.LoadWeatherData)

val expectedState = HomeScreenViewState(
units = "metric",
units = Units.METRIC,
defaultLocation = DefaultLocation(
longitude = 0.0, latitude = 0.0
),
locationName = "-",
language = "English",
language = SupportedLanguage.ENGLISH,
weather = null,
isLoading = false,
errorMessageId = R.string.error_client
Expand All @@ -142,12 +142,12 @@ class HomeViewModelIntegrationTest {
val viewModel = createViewModel(weatherRepository = weatherRepository)

val expectedState = HomeScreenViewState(
units = "metric",
units = Units.METRIC,
defaultLocation = DefaultLocation(
longitude = 0.0, latitude = 0.0
),
locationName = "-",
language = "English",
language = SupportedLanguage.ENGLISH,
weather = fakeSuccessMappedWeatherResponse,
isLoading = false,
errorMessageId = null
Expand All @@ -173,12 +173,12 @@ class HomeViewModelIntegrationTest {
)

val expectedState = HomeScreenViewState(
units = "metric",
units = Units.METRIC,
defaultLocation = DefaultLocation(
longitude = 0.0, latitude = 0.0
),
locationName = "Paradise",
language = "English",
language = SupportedLanguage.ENGLISH,
weather = fakeSuccessMappedWeatherResponse,
isLoading = false,
errorMessageId = null
Expand All @@ -196,18 +196,15 @@ class HomeViewModelIntegrationTest {
private fun createViewModel(
weatherRepository: WeatherRepository,
settingsRepository: SettingsRepository = mockk<SettingsRepository>() {
coEvery { getUnits() } returns flowOf(Units.METRIC.value)
coEvery { getUnits() } returns flowOf(Units.METRIC)
coEvery { getDefaultLocation() } returns flowOf(
DefaultLocation(
longitude = 0.0, latitude = 0.0
)
)
coEvery { getLanguage() } returns flowOf(SupportedLanguage.ENGLISH.languageName)
coEvery { getLanguage() } returns flowOf(SupportedLanguage.ENGLISH)
coEvery { getAppVersion() } returns "1.0.0"
coEvery { getAvailableLanguages() } returns listOf(SupportedLanguage.ENGLISH.languageName)
coEvery { getAvailableUnits() } returns listOf(Units.METRIC.value)
coEvery { getFormats() } returns listOf(TimeFormat.TWENTY_FOUR_HOUR.value)
coEvery { getFormat() } returns flowOf(TimeFormat.TWENTY_FOUR_HOUR.value)
coEvery { getFormat() } returns flowOf(TimeFormat.TWENTY_FOUR_HOUR)
coEvery { getExcludedData() } returns flowOf(ExcludedData.CURRENT.value)
}
): HomeViewModel =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ import org.junit.Test

class SettingsRepositoryUnitTest {
// TODO Integration test with the real data store
@Test
fun `when we fetch available languages, then we get all available languages`() {
val settingsRepo = createSettingsRepository()
assert(settingsRepo.getAvailableLanguages().size == SupportedLanguage.entries.size)
}

@Test
fun `when we update language, then we get the updated language`() {
val settingsRepo = createSettingsRepository()
runBlocking {
settingsRepo.setLanguage(SupportedLanguage.FRENCH.languageName)
settingsRepo.setLanguage(SupportedLanguage.FRENCH)
settingsRepo.getLanguage().map { language ->
assert(language == SupportedLanguage.FRENCH.languageName)
assert(language == SupportedLanguage.FRENCH)
}
}
}
Expand All @@ -36,7 +31,7 @@ class SettingsRepositoryUnitTest {
val settingsRepo = createSettingsRepository()
runBlocking {
settingsRepo.getLanguage().map { language ->
assert(language == SupportedLanguage.ENGLISH.languageName)
assert(language == SupportedLanguage.ENGLISH)
}
}
}
Expand All @@ -45,9 +40,9 @@ class SettingsRepositoryUnitTest {
fun `when we update units, then we get the updated units`() {
val settingsRepo = createSettingsRepository()
runBlocking {
settingsRepo.setUnits(Units.IMPERIAL.value)
settingsRepo.setUnits(Units.IMPERIAL)
settingsRepo.getUnits().map { units ->
assert(units == Units.IMPERIAL.value)
assert(units == Units.IMPERIAL)
}
}
}
Expand All @@ -57,24 +52,18 @@ class SettingsRepositoryUnitTest {
val settingsRepo = createSettingsRepository()
runBlocking {
settingsRepo.getUnits().map { units ->
assert(units == Units.METRIC.value)
assert(units == Units.METRIC)
}
}
}

@Test
fun `when we fetch available units, then we get all available units`() {
val settingsRepo = createSettingsRepository()
assert(settingsRepo.getAvailableUnits().size == Units.entries.size)
}

@Test
fun `when we update time format, then we get the updated time format`() {
val settingsRepo = createSettingsRepository()
runBlocking {
settingsRepo.setFormat(TimeFormat.TWELVE_HOUR.name)
settingsRepo.setFormat(TimeFormat.TWELVE_HOUR)
settingsRepo.getFormat().map { format ->
assert(format == TimeFormat.TWELVE_HOUR.name)
assert(format == TimeFormat.TWELVE_HOUR)
}
}
}
Expand All @@ -84,17 +73,11 @@ class SettingsRepositoryUnitTest {
val settingsRepo = createSettingsRepository()
runBlocking {
settingsRepo.getFormat().map { format ->
assert(format == TimeFormat.TWENTY_FOUR_HOUR.name)
assert(format == TimeFormat.TWENTY_FOUR_HOUR)
}
}
}

@Test
fun `when we fetch available time formats, then we get all available formats`() {
val settingsRepo = createSettingsRepository()
assert(settingsRepo.getFormats().size == TimeFormat.entries.size)
}

@Test
fun `when we fetch the location, then we get the default location`() {
val settingsRepo = createSettingsRepository()
Expand Down Expand Up @@ -122,7 +105,13 @@ class SettingsRepositoryUnitTest {
fun `when we update excluded data, then we get the updated excluded data`() {
val settingsRepo = createSettingsRepository()
runBlocking {
settingsRepo.setExcludedData(listOf(ExcludedData.ALERTS, ExcludedData.MINUTELY, ExcludedData.DAILY))
settingsRepo.setExcludedData(
listOf(
ExcludedData.ALERTS,
ExcludedData.MINUTELY,
ExcludedData.DAILY
)
)
settingsRepo.getExcludedData().map { excludedData ->
assert(excludedData == "${ExcludedData.ALERTS.value},${ExcludedData.MINUTELY.value},${ExcludedData.DAILY.value}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class SettingsViewModelIntegrationTest {
settingsViewModel.processIntent(SettingsScreenIntent.LoadSettingScreenData)

val expectedState = SettingsScreenViewState(
selectedUnit = Units.METRIC.value,
selectedLanguage = SupportedLanguage.ENGLISH.languageName,
availableLanguages = SupportedLanguage.entries.map { it.languageName },
availableUnits = Units.entries.map { it.value },
selectedTimeFormat = TimeFormat.TWENTY_FOUR_HOUR.name,
availableFormats = TimeFormat.entries.map { it.value },
selectedUnit = Units.METRIC,
selectedLanguage = SupportedLanguage.ENGLISH,
availableLanguages = SupportedLanguage.entries,
availableUnits = Units.entries,
selectedTimeFormat = TimeFormat.TWENTY_FOUR_HOUR,
availableFormats = TimeFormat.entries,
versionInfo = "1.0.0",
selectedExcludedData = listOf(ExcludedData.MINUTELY, ExcludedData.ALERTS),
excludedData = ExcludedData.entries,
Expand All @@ -61,12 +61,12 @@ class SettingsViewModelIntegrationTest {
fun `when we change units, then the units are updated`() = runBlocking {
val settingsViewModel = createSettingsScreenViewModel()

settingsViewModel.processIntent(SettingsScreenIntent.ChangeUnits(selectedUnits = "standard"))
settingsViewModel.processIntent(SettingsScreenIntent.ChangeUnits(selectedUnits = Units.STANDARD))

settingsViewModel.state.test {
awaitItem().also { state ->
assert(state.error == null)
assert(state.selectedUnit == "standard")
assert(state.selectedUnit == Units.STANDARD)
}
}
}
Expand All @@ -75,12 +75,12 @@ class SettingsViewModelIntegrationTest {
fun `when we change language, then the language is updated `() = runBlocking {
val settingsViewModel = createSettingsScreenViewModel()

settingsViewModel.processIntent(SettingsScreenIntent.ChangeLanguage(selectedLanguage = "French"))
settingsViewModel.processIntent(SettingsScreenIntent.ChangeLanguage(selectedLanguage = SupportedLanguage.AFRIKAANS))

settingsViewModel.state.test {
awaitItem().also { state ->
assert(state.error == null)
assert(state.selectedLanguage == "French")
assert(state.selectedLanguage == SupportedLanguage.AFRIKAANS)
}
}
}
Expand All @@ -89,12 +89,12 @@ class SettingsViewModelIntegrationTest {
fun `when we change time format, then the format is updated `() = runBlocking {
val settingsViewModel = createSettingsScreenViewModel()

settingsViewModel.processIntent(SettingsScreenIntent.ChangeTimeFormat(selectedTimeFormat = TimeFormat.TWENTY_FOUR_HOUR.name))
settingsViewModel.processIntent(SettingsScreenIntent.ChangeTimeFormat(selectedTimeFormat = TimeFormat.TWENTY_FOUR_HOUR))

settingsViewModel.state.test {
awaitItem().also { state ->
assert(state.error == null)
assert(state.selectedTimeFormat == TimeFormat.TWENTY_FOUR_HOUR.name)
assert(state.selectedTimeFormat == TimeFormat.TWENTY_FOUR_HOUR)
}
}
}
Expand All @@ -103,12 +103,24 @@ class SettingsViewModelIntegrationTest {
fun `when we change excluded data, then the excluded data is updated `() = runBlocking {
val settingsViewModel = createSettingsScreenViewModel()

settingsViewModel.processIntent(SettingsScreenIntent.ChangeExcludedData(selectedExcludedData = listOf(ExcludedData.CURRENT, ExcludedData.DAILY)))
settingsViewModel.processIntent(
SettingsScreenIntent.ChangeExcludedData(
selectedExcludedData = listOf(
ExcludedData.CURRENT,
ExcludedData.DAILY
)
)
)

settingsViewModel.state.test {
awaitItem().also { state ->
assert(state.error == null)
assert(state.selectedExcludedData == listOf(ExcludedData.CURRENT, ExcludedData.DAILY))
assert(
state.selectedExcludedData == listOf(
ExcludedData.CURRENT,
ExcludedData.DAILY
)
)
assert(state.selectedExcludedDataDisplayValue == "current,daily")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WeatherRepositoryUnitTest {

@MockK
val mockSettingsRepository = mockk<SettingsRepository>(relaxed = true).apply {
coEvery { getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR.value)
coEvery { getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR)
coEvery { getExcludedData() } returns flowOf(ExcludedData.NONE.value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,46 @@ import kotlinx.coroutines.flow.flow

class FakeSettingsRepository : SettingsRepository {

private val settingsMap = mutableMapOf<String, String>()
override suspend fun setLanguage(language: String) {
private val settingsMap = mutableMapOf<String, Any>()
override suspend fun setLanguage(language: SupportedLanguage) {
settingsMap[KEY_LANGUAGE] = language
}

override suspend fun getLanguage(): Flow<String> = flow {
emit(settingsMap[KEY_LANGUAGE] ?: SupportedLanguage.ENGLISH.languageName)
override suspend fun getLanguage(): Flow<SupportedLanguage> = flow {
emit(settingsMap[KEY_LANGUAGE] as? SupportedLanguage ?: SupportedLanguage.ENGLISH)
}

override suspend fun setUnits(units: String) {
override suspend fun setUnits(units: Units) {
settingsMap[KEY_UNITS] = units
}

override suspend fun getUnits(): Flow<String> = flow {
emit(settingsMap[KEY_UNITS] ?: Units.METRIC.value)
override suspend fun getUnits(): Flow<Units> = flow {
emit(settingsMap[KEY_UNITS] as? Units ?: Units.METRIC)
}

override fun getAppVersion(): String = "1.0.0"

override fun getAvailableLanguages(): List<String> =
SupportedLanguage.entries.map { it.languageName }

override suspend fun setDefaultLocation(defaultLocation: DefaultLocation) {
settingsMap[KEY_LAT_LNG] = "${defaultLocation.latitude}/${defaultLocation.longitude}"
}

override suspend fun getDefaultLocation(): Flow<DefaultLocation> = flow {
val latLng = settingsMap[KEY_LAT_LNG] ?: "0.0/0.0"
val latLng = settingsMap[KEY_LAT_LNG] as? String ?: "0.0/0.0"
val latLngList = latLng.split("/")
DefaultLocation(latitude = latLngList[0].toDouble(), longitude = latLngList[1].toDouble())
}

override suspend fun getFormat(): Flow<String> = flow {
emit(settingsMap[KEY_TIME_FORMAT] ?: TimeFormat.TWENTY_FOUR_HOUR.name)
override suspend fun getFormat(): Flow<TimeFormat> = flow {
emit(settingsMap[KEY_TIME_FORMAT] as? TimeFormat ?: TimeFormat.TWENTY_FOUR_HOUR)
}

override suspend fun setFormat(format: String) {
override suspend fun setFormat(format: TimeFormat) {
settingsMap[KEY_TIME_FORMAT] = format
}

override fun getFormats(): List<String> {
return TimeFormat.entries.map { it.value }
}

override suspend fun getExcludedData(): Flow<String> = flow {
emit(
settingsMap[KEY_EXCLUDED_DATA]
settingsMap[KEY_EXCLUDED_DATA] as? String
?: "${ExcludedData.MINUTELY.value},${ExcludedData.ALERTS.value}"
)
}
Expand All @@ -71,6 +64,4 @@ class FakeSettingsRepository : SettingsRepository {
val formattedData = excludedData.joinToString(separator = ",") { it.value }
settingsMap[KEY_EXCLUDED_DATA] = formattedData
}

override fun getAvailableUnits(): List<String> = Units.entries.map { it.value }
}

0 comments on commit f61a837

Please sign in to comment.