-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made ImageRepositoryImpl easily testable and added unit tests
- Loading branch information
1 parent
2ee33c9
commit 2940209
Showing
8 changed files
with
250 additions
and
19 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
88 changes: 88 additions & 0 deletions
88
composeApp/src/commonTest/kotlin/data/repository/ImageRepositoryImplTest.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,88 @@ | ||
package data.repository | ||
|
||
import di.mapperModule | ||
import di.networkModule | ||
import io.ktor.http.HttpStatusCode | ||
import kotlinx.coroutines.test.runTest | ||
import networking.createApiInterface | ||
import org.koin.core.context.startKoin | ||
import org.koin.core.context.stopKoin | ||
import org.koin.test.KoinTest | ||
import util.NetworkHelper | ||
import kotlin.test.AfterTest | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
|
||
class ImageRepositoryImplTest : KoinTest { | ||
|
||
private lateinit var sut: ImageRepository | ||
|
||
@BeforeTest | ||
fun setUp() { | ||
startKoin { modules(networkModule(), mapperModule()) } | ||
} | ||
|
||
@Test | ||
fun givenSuccessResponse_whenGetImageSearchResultIsCalled_successStateIsReturned() = runTest { | ||
val ktorfit = NetworkHelper.createKtorfitWithMockResponse( | ||
HttpStatusCode.OK,NetworkHelper.successfulImageListResponse | ||
) | ||
|
||
val apiService = ktorfit.createApiInterface() | ||
|
||
sut = ImageRepositoryImpl(apiService) | ||
val response = sut.getImageSearchResult("Nigeria", 1, 2) | ||
assertNotNull(response) | ||
assertTrue(response is Resource.Success) | ||
} | ||
|
||
@Test | ||
fun givenSuccessResponse_whenGetPhotoIsCalled_successStateIsReturned() = runTest { | ||
val ktorfit = NetworkHelper.createKtorfitWithMockResponse( | ||
HttpStatusCode.OK,NetworkHelper.successfulImageResponse | ||
) | ||
|
||
val apiService = ktorfit.createApiInterface() | ||
|
||
sut = ImageRepositoryImpl(apiService) | ||
val response = sut.getPhoto("Nigeria") | ||
assertNotNull(response) | ||
assertTrue(response is Resource.Success) | ||
} | ||
|
||
@Test | ||
fun givenErrorResponse_whenGetPhotoIsCalled_failureStateIsReturned() = runTest { | ||
val ktorfit = NetworkHelper.createKtorfitWithMockResponse( | ||
HttpStatusCode.Unauthorized,NetworkHelper.authorizationError | ||
) | ||
|
||
val apiService = ktorfit.createApiInterface() | ||
|
||
sut = ImageRepositoryImpl(apiService) | ||
val response = sut.getPhoto("vzpEjttBI0M") | ||
assertNotNull(response) | ||
assertTrue(response is Resource.Failure) | ||
} | ||
|
||
@Test | ||
fun givenErrorResponse_whenGetImageSearchResultIsCalled_failureStateIsReturned() = runTest { | ||
val ktorfit = NetworkHelper.createKtorfitWithMockResponse( | ||
HttpStatusCode.OK,NetworkHelper.authorizationError | ||
) | ||
|
||
val apiService = ktorfit.createApiInterface() | ||
|
||
sut = ImageRepositoryImpl(apiService) | ||
val response = sut.getImageSearchResult("Nigeria", 1, 2) | ||
assertNotNull(response) | ||
assertTrue(response is Resource.Failure) | ||
} | ||
|
||
@AfterTest | ||
fun tearDown() { | ||
stopKoin() | ||
} | ||
|
||
} |
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,25 @@ | ||
package di | ||
|
||
import data.mapper.PhotoCreatorMapper | ||
import data.mapper.PhotoMapper | ||
import data.mapper.PhotosUrlsMapper | ||
import kotlinx.serialization.json.Json | ||
import org.koin.dsl.module | ||
|
||
|
||
fun networkModule() = module { | ||
single { | ||
|
||
Json { | ||
ignoreUnknownKeys = true | ||
prettyPrint = true | ||
isLenient = true | ||
} | ||
} | ||
} | ||
|
||
fun mapperModule() = module { | ||
single { PhotoCreatorMapper() } | ||
single { PhotosUrlsMapper() } | ||
single { PhotoMapper(get(), get()) } | ||
} |
Oops, something went wrong.