-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
058ab10
commit aae0a45
Showing
8 changed files
with
219 additions
and
34 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
35 changes: 35 additions & 0 deletions
35
domain/src/test/kotlin/com/rubylichtenstein/domain/favorites/FakeFavoritesRepository.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,35 @@ | ||
package com.rubylichtenstein.domain.favorites | ||
|
||
import com.rubylichtenstein.domain.common.AsyncResult | ||
import com.rubylichtenstein.domain.common.asAsyncResult | ||
import com.rubylichtenstein.domain.images.DogImageEntity | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
|
||
class FakeFavoritesRepository( | ||
initialDogImages: List<DogImageEntity> | ||
) : FavoritesRepository { | ||
|
||
private val _images = MutableStateFlow(initialDogImages) | ||
|
||
override val favoriteImagesFlow: Flow<AsyncResult<List<DogImageEntity>>> = | ||
_images.map { images -> | ||
images.filter { it.isFavorite } | ||
}.asAsyncResult() | ||
|
||
override suspend fun updateFavoriteStatus(url: String, isFavorite: Boolean) { | ||
val currentImages = _images.value.toMutableList() | ||
|
||
// Find the image with the given URL | ||
val imageIndex = currentImages.indexOfFirst { it.url == url } | ||
|
||
if (imageIndex != -1) { | ||
// Update the isFavorite status of the image | ||
val updatedImage = currentImages[imageIndex].copy(isFavorite = isFavorite) | ||
currentImages[imageIndex] = updatedImage | ||
} | ||
|
||
_images.value = currentImages | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
domain/src/test/kotlin/com/rubylichtenstein/domain/favorites/GetFavoriteImagesUseCaseTest.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,50 @@ | ||
package com.rubylichtenstein.domain.favorites | ||
|
||
import app.cash.turbine.test | ||
import com.rubylichtenstein.domain.common.AsyncResult | ||
import com.rubylichtenstein.domain.images.DogImageEntity | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class GetFavoriteImagesUseCaseTest { | ||
|
||
private lateinit var useCase: GetFavoriteImagesUseCase | ||
private lateinit var fakeFavoritesRepository: FakeFavoritesRepository | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
val element1 = DogImageEntity( | ||
url = "sampleUrl1.com", | ||
isFavorite = false, | ||
breedName = "Dog", | ||
breedKey = "dog" | ||
) | ||
|
||
val element2 = DogImageEntity( | ||
url = "sampleUrl2.com", | ||
isFavorite = true, | ||
breedName = "Dog", | ||
breedKey = "dog" | ||
) | ||
|
||
val images = listOf( | ||
element1, | ||
element2 | ||
) | ||
|
||
fakeFavoritesRepository = FakeFavoritesRepository(images) | ||
useCase = GetFavoriteImagesUseCase(fakeFavoritesRepository) | ||
} | ||
|
||
@Test | ||
fun `when invoke is called, it should return favorite images from the repository`() = runTest { | ||
useCase.invoke().test { | ||
Assertions.assertEquals(AsyncResult.Loading, awaitItem()) | ||
val initialState = awaitItem() as AsyncResult.Success | ||
Assertions.assertEquals(1, initialState.data.size) | ||
Assertions.assertEquals("sampleUrl2.com", initialState.data[0].url) | ||
} | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
domain/src/test/kotlin/com/rubylichtenstein/domain/images/DogImageEntityKtTest.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,59 @@ | ||
package com.rubylichtenstein.domain.images | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class DogImageEntityKtTest { | ||
|
||
@Test | ||
fun `when subBreed is null, only breed should be returned`() { | ||
// Given | ||
val breed = "golden" | ||
val subBreed: String? = null | ||
|
||
// When | ||
val result = buildBreedKey(subBreed, breed) | ||
|
||
// Then | ||
assertEquals(breed, result) | ||
} | ||
|
||
@Test | ||
fun `when subBreed is not null, breed and subBreed should be concatenated with a slash`() { | ||
// Given | ||
val breed = "golden" | ||
val subBreed = "retriever" | ||
|
||
// When | ||
val result = buildBreedKey(subBreed, breed) | ||
|
||
// Then | ||
assertEquals("$breed/$subBreed", result) | ||
} | ||
|
||
@Test | ||
fun `when breed is empty and subBreed is not null, only slash and subBreed should be returned`() { | ||
// Given | ||
val breed = "" | ||
val subBreed = "retriever" | ||
|
||
// When | ||
val result = buildBreedKey(subBreed, breed) | ||
|
||
// Then | ||
assertEquals("/$subBreed", result) | ||
} | ||
|
||
@Test | ||
fun `when both breed and subBreed are empty, only a slash should be returned`() { | ||
// Given | ||
val breed = "" | ||
val subBreed = "" | ||
|
||
// When | ||
val result = buildBreedKey(subBreed, breed) | ||
|
||
// Then | ||
assertEquals("/", result) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
domain/src/test/kotlin/com/rubylichtenstein/domain/images/FakeImagesRepository.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,20 @@ | ||
package com.rubylichtenstein.domain.images | ||
|
||
import com.rubylichtenstein.domain.common.AsyncResult | ||
import com.rubylichtenstein.domain.common.asAsyncResult | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
|
||
class FakeImagesRepository( | ||
initialDogImages: List<DogImageEntity> | ||
) : ImagesRepository { | ||
|
||
private val _images = MutableStateFlow(initialDogImages) | ||
|
||
override fun getImagesByBreed(breedKey: String): Flow<AsyncResult<List<DogImageEntity>>> { | ||
return _images.map { images -> | ||
images.filter { it.breedKey == breedKey } | ||
}.asAsyncResult() | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
domain/src/test/kotlin/com/rubylichtenstein/domain/images/GetBreedImagesUseCaseTest.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,47 @@ | ||
package com.rubylichtenstein.domain.images | ||
|
||
import app.cash.turbine.test | ||
import com.rubylichtenstein.domain.common.AsyncResult | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class GetBreedImagesUseCaseTest { | ||
private lateinit var useCase: GetBreedImagesUseCase | ||
private lateinit var fakeImagesRepository: FakeImagesRepository | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
val element1 = DogImageEntity( | ||
url = "sampleUrl1.com", | ||
isFavorite = false, | ||
breedName = "Dog", | ||
breedKey = "Dog" | ||
) | ||
|
||
val element2 = DogImageEntity( | ||
url = "sampleUrl2.com", | ||
isFavorite = true, | ||
breedName = "Dog", | ||
breedKey = "Dog" | ||
) | ||
|
||
val images = listOf( | ||
element1, | ||
element2 | ||
) | ||
|
||
fakeImagesRepository = FakeImagesRepository(images) | ||
useCase = GetBreedImagesUseCase(fakeImagesRepository) | ||
} | ||
|
||
@Test | ||
operator fun invoke() = runTest { | ||
useCase.invoke("Dog").test { | ||
Assertions.assertEquals(AsyncResult.Loading, awaitItem()) | ||
val initialState = awaitItem() as AsyncResult.Success | ||
Assertions.assertEquals(2, initialState.data.size) | ||
} | ||
} | ||
} |