diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 85aa05c..71c1a8d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -79,5 +79,5 @@ jobs: - name: Upload APK uses: actions/upload-artifact@v2 with: - name: tv-schedule1.apk + name: tv-schedule.apk path: apks/ diff --git a/app/src/main/java/com/example/tvschedule/data/favorite/source/local/db/FavoriteShowsDao.kt b/app/src/main/java/com/example/tvschedule/data/favorite/source/local/db/FavoriteShowsDao.kt index de1eada..7e70a8e 100644 --- a/app/src/main/java/com/example/tvschedule/data/favorite/source/local/db/FavoriteShowsDao.kt +++ b/app/src/main/java/com/example/tvschedule/data/favorite/source/local/db/FavoriteShowsDao.kt @@ -16,7 +16,7 @@ interface FavoriteShowsDao { suspend fun addShow(show: ShowEntity) @Query("DELETE FROM show WHERE show.id is :showId") - suspend fun deleteShow(showId: Long): Int + suspend fun deleteShow(showId: Long) @Update suspend fun updateShow(show: ShowEntity) diff --git a/app/src/test/java/com/example/tvschedule/data/favorite/source/local/FavoriteLocalDataSourceTest.kt b/app/src/test/java/com/example/tvschedule/data/favorite/source/local/FavoriteLocalDataSourceTest.kt new file mode 100644 index 0000000..4d6a43a --- /dev/null +++ b/app/src/test/java/com/example/tvschedule/data/favorite/source/local/FavoriteLocalDataSourceTest.kt @@ -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() + 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) + } + } +} diff --git a/app/src/test/java/com/example/tvschedule/data/schedule/repository/ScheduleRepositoryTest.kt b/app/src/test/java/com/example/tvschedule/data/schedule/repository/ScheduleRepositoryTest.kt index 85d35a9..f6758e6 100644 --- a/app/src/test/java/com/example/tvschedule/data/schedule/repository/ScheduleRepositoryTest.kt +++ b/app/src/test/java/com/example/tvschedule/data/schedule/repository/ScheduleRepositoryTest.kt @@ -52,4 +52,40 @@ class ScheduleRepositoryTest { assertThat(e).isEqualTo(exception) } } + + @Test + fun `get schedule and return local if exist success`() = runTest { + whenever(localDataSource.schedules) + .thenReturn(hashMapOf(ModelUtil.scheduleDate to ModelUtil.schedulesResponse)) + whenever(mapper.mapList(ModelUtil.schedulesResponse)).thenReturn(ModelUtil.schedules) + val result = scheduleRepository.getSchedule(ModelUtil.scheduleDate) + verify(localDataSource, Mockito.times(1)).schedules + verify(remoteDataSource, Mockito.times(0)) + .getSchedule(ModelUtil.scheduleDate) + verify(mapper, Mockito.times(1)).mapList(ModelUtil.schedulesResponse) + assertThat(result).isEqualTo(ModelUtil.schedules) + } + + @Test + fun `get show from local success`() = runTest { + whenever(localDataSource.schedules) + .thenReturn(hashMapOf(ModelUtil.scheduleDate to ModelUtil.schedulesResponse)) + whenever(showMapper.map(ModelUtil.showResponse)).thenReturn(ModelUtil.show) + val result = scheduleRepository.getShowFromCache(ModelUtil.showId) + verify(localDataSource, Mockito.times(1)).schedules + verify(showMapper, Mockito.times(1)).map(ModelUtil.showResponse) + assertThat(result).isEqualTo(ModelUtil.show) + } + + @Test + fun `get show from local failed`() = runTest { + val exception = IllegalStateException("Failed to get show from local") + whenever(localDataSource.schedules).thenThrow(exception) + runCatching { + scheduleRepository.getShowFromCache(ModelUtil.showId) + }.onFailure { e -> + verify(localDataSource, Mockito.times(1)).schedules + assertThat(e).isEqualTo(exception) + } + } } diff --git a/app/src/test/java/com/example/tvschedule/data/schedule/source/local/ScheduleLocalDataSourceTest.kt b/app/src/test/java/com/example/tvschedule/data/schedule/source/local/ScheduleLocalDataSourceTest.kt new file mode 100644 index 0000000..1b473ea --- /dev/null +++ b/app/src/test/java/com/example/tvschedule/data/schedule/source/local/ScheduleLocalDataSourceTest.kt @@ -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) + } + } +} diff --git a/app/src/test/java/com/example/tvschedule/data/schedule/source/remote/ScheduleRemoteDataSourceImplTest.kt b/app/src/test/java/com/example/tvschedule/data/schedule/source/remote/ScheduleRemoteDataSourceImplTest.kt new file mode 100644 index 0000000..555c748 --- /dev/null +++ b/app/src/test/java/com/example/tvschedule/data/schedule/source/remote/ScheduleRemoteDataSourceImplTest.kt @@ -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() + + 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) + } + } +}