-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from andrewafanasenko/develop
Develop
- Loading branch information
Showing
6 changed files
with
239 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...est/java/com/example/tvschedule/data/favorite/source/local/FavoriteLocalDataSourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package com.example.tvschedule.data.favorite.source.local | ||
|
||
import com.example.tvschedule.data.favorite.source.local.db.FavoriteShowsDao | ||
import com.example.tvschedule.data.util.ModelUtil | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import org.mockito.Mockito | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
|
||
|
||
class FavoriteLocalDataSourceTest { | ||
|
||
private val favoriteShowsDao = mock<FavoriteShowsDao>() | ||
private val favoriteLocalDataSource = FavoriteLocalDataSourceImpl(favoriteShowsDao) | ||
|
||
@Test | ||
fun `get favorite shows updates success`() = runTest { | ||
val showEntities = listOf(ModelUtil.showEntity) | ||
val flow = flow { emit(showEntities) } | ||
whenever(favoriteShowsDao.showsUpdates()).thenReturn(flow) | ||
favoriteLocalDataSource.favoriteShows().collect { result -> | ||
verify(favoriteShowsDao, Mockito.times(1)).showsUpdates() | ||
assertThat(result).isEqualTo(showEntities) | ||
} | ||
} | ||
|
||
@Test | ||
fun `get favorite shows updates failed`() = runTest { | ||
val exception = IllegalStateException("Failed to get show updates") | ||
whenever(favoriteShowsDao.showsUpdates()).thenThrow(exception) | ||
runCatching { | ||
favoriteLocalDataSource.favoriteShows() | ||
}.onFailure { e -> | ||
verify(favoriteShowsDao, Mockito.times(1)).showsUpdates() | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
|
||
@Test | ||
fun `add to favorites success`() = runTest { | ||
whenever(favoriteShowsDao.addShow(ModelUtil.showEntity)).thenReturn(Unit) | ||
val result = favoriteLocalDataSource.addToFavorite(ModelUtil.showEntity) | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.addShow(ModelUtil.showEntity) | ||
assertThat(result).isEqualTo(Unit) | ||
} | ||
|
||
@Test | ||
fun `add to favorites failed`() = runTest { | ||
val exception = IllegalStateException("Failed to add to favorite") | ||
whenever(favoriteShowsDao.addShow(ModelUtil.showEntity)).thenThrow(exception) | ||
runCatching { | ||
favoriteLocalDataSource.addToFavorite(ModelUtil.showEntity) | ||
}.onFailure { e -> | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.addShow(ModelUtil.showEntity) | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
|
||
@Test | ||
fun `remove from favorites success`() = runTest { | ||
whenever(favoriteShowsDao.deleteShow(ModelUtil.showId)).thenReturn(Unit) | ||
val result = favoriteLocalDataSource.removeFromFavorite(ModelUtil.showId) | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.deleteShow(ModelUtil.showId) | ||
assertThat(result).isEqualTo(Unit) | ||
} | ||
|
||
@Test | ||
fun `remove from favorites failed`() = runTest { | ||
val exception = IllegalStateException("Failed to remove from favorite") | ||
whenever(favoriteShowsDao.deleteShow(ModelUtil.showId)).thenThrow(exception) | ||
runCatching { | ||
favoriteLocalDataSource.removeFromFavorite(ModelUtil.showId) | ||
}.onFailure { e -> | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.deleteShow(ModelUtil.showId) | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
|
||
@Test | ||
fun `update favorite success`() = runTest { | ||
whenever(favoriteShowsDao.updateShow(ModelUtil.showEntity)).thenReturn(Unit) | ||
val result = favoriteLocalDataSource.updateFavorite(ModelUtil.showEntity) | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.updateShow(ModelUtil.showEntity) | ||
assertThat(result).isEqualTo(Unit) | ||
} | ||
|
||
@Test | ||
fun `update favorite failed`() = runTest { | ||
val exception = IllegalStateException("Failed to update favorite") | ||
whenever(favoriteShowsDao.updateShow(ModelUtil.showEntity)).thenThrow(exception) | ||
runCatching { | ||
favoriteLocalDataSource.updateFavorite(ModelUtil.showEntity) | ||
}.onFailure { e -> | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.updateShow(ModelUtil.showEntity) | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
|
||
@Test | ||
fun `get favorite success`() = runTest { | ||
whenever(favoriteShowsDao.getShow(ModelUtil.showId)).thenReturn(ModelUtil.showEntity) | ||
val result = favoriteLocalDataSource.getShow(ModelUtil.showId) | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.getShow(ModelUtil.showId) | ||
assertThat(result).isEqualTo(ModelUtil.showEntity) | ||
} | ||
|
||
@Test | ||
fun `get favorite failed`() = runTest { | ||
val exception = IllegalStateException("Failed to get favorite") | ||
whenever(favoriteShowsDao.getShow(ModelUtil.showId)).thenThrow(exception) | ||
runCatching { | ||
favoriteLocalDataSource.getShow(ModelUtil.showId) | ||
}.onFailure { e -> | ||
verify(favoriteShowsDao, Mockito.times(1)) | ||
.getShow(ModelUtil.showId) | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...est/java/com/example/tvschedule/data/schedule/source/local/ScheduleLocalDataSourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.tvschedule.data.schedule.source.local | ||
|
||
import com.example.tvschedule.data.util.ModelUtil | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import java.time.LocalDate | ||
|
||
|
||
class ScheduleLocalDataSourceTest { | ||
|
||
private val scheduleLocalDataSource = ScheduleLocalDataSourceImpl() | ||
|
||
@Test | ||
fun `get from local data source success`() = runTest { | ||
scheduleLocalDataSource.schedules[LocalDate.now()] = ModelUtil.schedulesResponse | ||
val result = scheduleLocalDataSource.schedules[LocalDate.now()] | ||
assertThat(result).isEqualTo(ModelUtil.schedulesResponse) | ||
} | ||
|
||
@Test | ||
fun `get from local data source failed`() = runTest { | ||
val exception = IllegalStateException("Failed to get from local data source") | ||
scheduleLocalDataSource.schedules[LocalDate.now().plusDays(1)] = ModelUtil.schedulesResponse | ||
runCatching { | ||
scheduleLocalDataSource.schedules[LocalDate.now()] | ||
}.onFailure { e -> | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...va/com/example/tvschedule/data/schedule/source/remote/ScheduleRemoteDataSourceImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.tvschedule.data.schedule.source.remote | ||
|
||
import com.example.tvschedule.data.schedule.api.ScheduleApi | ||
import com.example.tvschedule.data.util.ModelUtil | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import org.mockito.Mockito | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import java.time.LocalDate | ||
|
||
|
||
class ScheduleRemoteDataSourceTest { | ||
|
||
private val api = mock<ScheduleApi>() | ||
|
||
private val scheduleRemoteDataSource = ScheduleRemoteDataSourceImpl(api) | ||
|
||
@Test | ||
fun `get schedule success`() = runTest { | ||
whenever(api.getSchedule(LocalDate.now())).thenReturn(ModelUtil.schedulesResponse) | ||
val result = scheduleRemoteDataSource.getSchedule(LocalDate.now()) | ||
verify(api, Mockito.times(1)).getSchedule(LocalDate.now()) | ||
assertThat(result).isEqualTo(ModelUtil.schedulesResponse) | ||
} | ||
|
||
@Test | ||
fun `get schedule failed`() = runTest { | ||
val exception = IllegalStateException("Failed to get schedule") | ||
whenever(api.getSchedule(LocalDate.now())).thenThrow(exception) | ||
runCatching { | ||
scheduleRemoteDataSource.getSchedule(LocalDate.now()) | ||
}.onFailure { e -> | ||
verify(api, Mockito.times(1)).getSchedule(LocalDate.now()) | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
} |