-
Notifications
You must be signed in to change notification settings - Fork 1
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 #123 from snuhcs-course/test/reaction-unit-test
Test: reaction unit test
- Loading branch information
Showing
9 changed files
with
394 additions
and
30 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
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
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
120 changes: 120 additions & 0 deletions
120
.../app/src/test/java/com/goliath/emojihub/repositories/remote/ReactionRepositoryImplTest.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,120 @@ | ||
package com.goliath.emojihub.repositories.remote | ||
|
||
import androidx.paging.testing.asSnapshot | ||
import com.goliath.emojihub.data_sources.api.ReactionApi | ||
import com.goliath.emojihub.mockLogClass | ||
import com.goliath.emojihub.sampleReactionWithEmojiDto | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import retrofit2.Response | ||
|
||
@RunWith(JUnit4::class) | ||
class ReactionRepositoryImplTest { | ||
private val reactionApi = mockk<ReactionApi>() | ||
private val reactionRepository = ReactionRepositoryImpl(reactionApi) | ||
@Before | ||
fun setUp() { | ||
mockLogClass() | ||
} | ||
|
||
@Test | ||
fun fetchReactionList_returnsFlowOfPagingDataOfReactionWithEmojiDto() { | ||
val numSampleReactions = 10 | ||
val sampleReactionWithEmojiDtoList = List(numSampleReactions) { sampleReactionWithEmojiDto } | ||
val expectedFetchedReactionWithEmojiDtoList = List(numSampleReactions*2) { sampleReactionWithEmojiDto } | ||
// *2 because of .asSnapshot() load one more time | ||
coEvery { | ||
reactionApi.fetchReactionList(any(), any(), any(), any()) | ||
} returns Response.success(sampleReactionWithEmojiDtoList) | ||
// when | ||
val fetchedReactionPagingDataFlow = runBlocking { | ||
reactionRepository.fetchReactionList("1234", "U+1F44D") | ||
} | ||
val fetchedReactionWithEmojiDtoList = runBlocking { | ||
fetchedReactionPagingDataFlow.asSnapshot() | ||
} | ||
// then | ||
coVerify(exactly = 2) { reactionApi.fetchReactionList(any(), any(), any(), any()) } | ||
runBlocking { | ||
assertEquals(expectedFetchedReactionWithEmojiDtoList.size, fetchedReactionWithEmojiDtoList.size) | ||
assertEquals(expectedFetchedReactionWithEmojiDtoList, fetchedReactionWithEmojiDtoList) | ||
} | ||
} | ||
|
||
@Test | ||
fun uploadReaction_success_returnsSuccessResponse() { | ||
// given | ||
val expectedResponse = Response.success(Unit) | ||
coEvery { | ||
reactionApi.uploadReaction(any(), any()) | ||
} returns expectedResponse | ||
// when | ||
val response = runBlocking { | ||
reactionRepository.uploadReaction("1234", "1234") | ||
} | ||
// then | ||
coVerify(exactly = 1) { reactionApi.uploadReaction(any(), any()) } | ||
assertEquals(expectedResponse, response) | ||
} | ||
|
||
@Test | ||
fun uploadReaction_failure_returnsFailureResponse() { | ||
// given | ||
val expectedResponse = Response.error<Unit>(400, mockk(relaxed = true)) | ||
coEvery { | ||
reactionApi.uploadReaction(any(), any()) | ||
} returns expectedResponse | ||
// when | ||
val response = runBlocking { | ||
reactionRepository.uploadReaction("1234", "1234") | ||
} | ||
// then | ||
coVerify(exactly = 1) { reactionApi.uploadReaction(any(), any()) } | ||
assertFalse(response.isSuccessful) | ||
} | ||
|
||
// @Test | ||
// TODO: Not implemented yet | ||
fun getReactionWithId() { | ||
} | ||
|
||
@Test | ||
fun deleteReaction_success_returnsSuccessResponse() { | ||
// given | ||
val expectedResponse = Response.success(Unit) | ||
coEvery { | ||
reactionApi.deleteReaction(any()) | ||
} returns expectedResponse | ||
// when | ||
val response = runBlocking { | ||
reactionRepository.deleteReaction("1234") | ||
} | ||
// then | ||
coVerify(exactly = 1) { reactionApi.deleteReaction(any()) } | ||
assertEquals(expectedResponse, response) | ||
} | ||
|
||
@Test | ||
fun deleteReaction_failure_returnsFailureResponse() { | ||
// given | ||
val expectedResponse = Response.error<Unit>(400, mockk(relaxed = true)) | ||
coEvery { | ||
reactionApi.deleteReaction(any()) | ||
} returns expectedResponse | ||
// when | ||
val response = runBlocking { | ||
reactionRepository.deleteReaction("1234") | ||
} | ||
// then | ||
coVerify(exactly = 1) { reactionApi.deleteReaction(any()) } | ||
assertFalse(response.isSuccessful) | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
android/app/src/test/java/com/goliath/emojihub/usecases/ReactionUseCaseImplTest.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,103 @@ | ||
package com.goliath.emojihub.usecases | ||
|
||
import androidx.paging.PagingData | ||
import androidx.paging.map | ||
import androidx.paging.testing.asSnapshot | ||
import com.goliath.emojihub.createReactionWithEmojiDtoList | ||
import com.goliath.emojihub.data_sources.ApiErrorController | ||
import com.goliath.emojihub.mockLogClass | ||
import com.goliath.emojihub.models.ReactionWithEmoji | ||
import com.goliath.emojihub.repositories.remote.ReactionRepository | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import retrofit2.Response | ||
|
||
@RunWith(JUnit4::class) | ||
class ReactionUseCaseImplTest { | ||
private val reactionRepository = mockk<ReactionRepository>() | ||
private val apiErrorController = spyk<ApiErrorController>() | ||
private val reactionUseCase = ReactionUseCaseImpl(reactionRepository, apiErrorController) | ||
@Before | ||
fun setUp() { | ||
mockLogClass() | ||
} | ||
|
||
@Test | ||
fun updateReactionList_withSamplePagingReactionData_updatesReactionListStateFlow() { | ||
// given | ||
val samplePagingReactionData = mockk<PagingData<ReactionWithEmoji>>() | ||
// when | ||
runBlocking { reactionUseCase.updateReactionList(samplePagingReactionData) } | ||
// then | ||
assertEquals(samplePagingReactionData, reactionUseCase.reactionList.value) | ||
} | ||
|
||
@Test | ||
fun fetchReactionList_returnsFlowOfReactionPagingData() { | ||
// given | ||
val sampleReactionPagingDataFlow = createReactionWithEmojiDtoList(5) | ||
val sampleAnswer = sampleReactionPagingDataFlow.map { it.map { dto -> ReactionWithEmoji(dto) } } | ||
coEvery { | ||
reactionRepository.fetchReactionList(any(), any()) | ||
} returns sampleReactionPagingDataFlow | ||
// when | ||
val result = runBlocking { reactionUseCase.fetchReactionList("1234", "U+1F44D") } | ||
// then | ||
coVerify(exactly = 1) { reactionRepository.fetchReactionList(any(), any()) } | ||
runBlocking { | ||
val sampleAnswerAsSnapshot = sampleAnswer.asSnapshot() | ||
val resultAsSnapshot = result.asSnapshot() | ||
for (i in sampleAnswerAsSnapshot.indices) { | ||
assertEquals(sampleAnswerAsSnapshot[i], resultAsSnapshot[i]) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun uploadReaction_successWithValidPostId_returnsTrue() { | ||
// given | ||
val samplePostId = "1234" | ||
val sampleEmojiId = "5678" | ||
coEvery { | ||
reactionRepository.uploadReaction(any(), any()) | ||
} returns Response.success(Unit) | ||
// when | ||
val result = runBlocking { reactionUseCase.uploadReaction(samplePostId, sampleEmojiId) } | ||
// then | ||
assertTrue(result) | ||
} | ||
|
||
@Test | ||
fun uploadReaction_failureWithInvalidPostId_returnsFalse() { | ||
// given | ||
val samplePostId = "-1234" | ||
val sampleEmojiId = "5678" | ||
coEvery { | ||
reactionRepository.uploadReaction(any(), any()) | ||
} returns Response.error(404, mockk(relaxed = true)) | ||
// when | ||
val result = runBlocking { reactionUseCase.uploadReaction(samplePostId, sampleEmojiId) } | ||
// then | ||
assertFalse(result) | ||
} | ||
|
||
// @Test | ||
// TODO: Not implemented yet | ||
fun getReactionWithId() { | ||
} | ||
|
||
// @Test | ||
// TODO: Not implemented yet | ||
fun deleteReaction() { | ||
} | ||
} |
Oops, something went wrong.