Skip to content

Commit

Permalink
Merge pull request #23 from andrewafanasenko/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
andrewafanasenko authored Dec 18, 2023
2 parents 348e01d + eb701be commit 4b010c5
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ jobs:
- name: Upload APK
uses: actions/upload-artifact@v2
with:
name: tv-schedule1.apk
name: tv-schedule.apk
path: apks/
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
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)
}
}
}
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)
}
}
}

0 comments on commit 4b010c5

Please sign in to comment.