-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
15 changed files
with
463 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/model/PaginationMetaApiModel.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,18 @@ | ||
package co.nimblehq.blisskmmic.data.model | ||
|
||
import co.nimblehq.blisskmmic.domain.model.PaginationMeta | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
class PaginationMetaApiModel( | ||
val page: Int, | ||
val pages: Int, | ||
@SerialName("page_size") | ||
val pageSize: Int, | ||
val records: Int | ||
) | ||
|
||
fun PaginationMetaApiModel.toPaginationMeta(): PaginationMeta { | ||
return PaginationMeta(page, pages, pageSize, records) | ||
} |
28 changes: 28 additions & 0 deletions
28
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/model/SurveyApiModel.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,28 @@ | ||
package co.nimblehq.blisskmmic.data.model | ||
|
||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.jsonapi.model.ApiJson | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SurveyApiModel( | ||
val id: String, | ||
val title: String, | ||
val description: String, | ||
@SerialName("is_active") | ||
val isActive: Boolean, | ||
@SerialName("cover_image_url") | ||
val coverImageUrl: String, | ||
@SerialName("survey_type") | ||
val surveyType: String | ||
) | ||
|
||
fun SurveyApiModel.toSurvey(): Survey { | ||
return Survey( | ||
id, | ||
coverImageUrl, | ||
title, | ||
description | ||
) | ||
} |
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
24 changes: 24 additions & 0 deletions
24
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/network/target/SurveyTargetType.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,24 @@ | ||
package co.nimblehq.blisskmmic.data.network.target | ||
|
||
import co.nimblehq.blisskmmic.data.network.helpers.TargetType | ||
import io.ktor.http.* | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
sealed class SurveyTargetType: TargetType | ||
|
||
class SurveySelectionTargetType(page: Int = 1, size: Int = 3): SurveyTargetType() { | ||
|
||
@Serializable | ||
data class SurveySelectionInput( | ||
@SerialName("page[number]") | ||
val pageNumber: Int, | ||
@SerialName("page[size]") | ||
val pageSize: Int | ||
) | ||
|
||
override var path = "surveys" | ||
override var method = HttpMethod.Post | ||
override var body = SurveySelectionInput(page, size) | ||
override var headers: Map<String, String>? = null | ||
} |
24 changes: 24 additions & 0 deletions
24
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/data/repository/SurveyRepositoryImpl.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,24 @@ | ||
package co.nimblehq.blisskmmic.data.repository | ||
|
||
import co.nimblehq.blisskmmic.data.model.* | ||
import co.nimblehq.blisskmmic.data.network.core.NetworkClient | ||
import co.nimblehq.blisskmmic.data.network.datasource.NetworkDataSource | ||
import co.nimblehq.blisskmmic.data.network.target.SurveySelectionTargetType | ||
import co.nimblehq.blisskmmic.domain.model.PaginationMeta | ||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.blisskmmic.domain.repository.SurveyRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
class SurveyRepositoryImpl(private val networkDataSource: NetworkDataSource): SurveyRepository { | ||
|
||
override fun survey(page: Int): Flow<Pair<List<Survey>, PaginationMeta>> { | ||
return networkDataSource.survey(SurveySelectionTargetType(page)) | ||
.map { | ||
Pair( | ||
it.first.map { item -> item.toSurvey() }, | ||
it.second.toPaginationMeta() | ||
) | ||
} | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/model/PaginationMeta.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,8 @@ | ||
package co.nimblehq.blisskmmic.domain.model | ||
|
||
data class PaginationMeta( | ||
val page: Int, | ||
val pages: Int, | ||
val pageSize: Int, | ||
val records: Int | ||
) |
11 changes: 11 additions & 0 deletions
11
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/repository/SurveyRepository.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,11 @@ | ||
package co.nimblehq.blisskmmic.domain.repository | ||
|
||
import co.nimblehq.blisskmmic.domain.model.PaginationMeta | ||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.blisskmmic.domain.model.Token | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SurveyRepository { | ||
|
||
fun survey(page: Int): Flow<Pair<List<Survey>, PaginationMeta>> | ||
} |
23 changes: 23 additions & 0 deletions
23
shared/src/commonMain/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCase.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,23 @@ | ||
package co.nimblehq.blisskmmic.domain.usecase | ||
|
||
import co.nimblehq.blisskmmic.domain.model.Survey | ||
import co.nimblehq.blisskmmic.domain.model.Token | ||
import co.nimblehq.blisskmmic.domain.repository.SurveyRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
interface SurveyListUseCase { | ||
|
||
operator fun invoke(page: Int): Flow<List<Survey>> | ||
} | ||
|
||
class SurveyListUseCaseImpl( | ||
private val repository: SurveyRepository | ||
) : SurveyListUseCase { | ||
|
||
override operator fun invoke(page: Int): Flow<List<Survey>> { | ||
return repository | ||
.survey(page) | ||
.map { it.first } | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/data/repository/SurveyRepositoryTest.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,64 @@ | ||
package co.nimblehq.blisskmmic.data.repository | ||
|
||
import co.nimblehq.blisskmmic.data.model.PaginationMetaApiModel | ||
import co.nimblehq.blisskmmic.data.model.SurveyApiModel | ||
import co.nimblehq.blisskmmic.data.network.datasource.NetworkDataSource | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.test.runTest | ||
import org.kodein.mock.Fake | ||
import org.kodein.mock.Mock | ||
import org.kodein.mock.tests.TestsWithMocks | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.fail | ||
|
||
@ExperimentalCoroutinesApi | ||
class SurveyRepositoryTest: TestsWithMocks() { | ||
|
||
@Mock | ||
lateinit var networkDataSource: NetworkDataSource | ||
@Fake | ||
lateinit var survey: SurveyApiModel | ||
@Fake | ||
lateinit var meta: PaginationMetaApiModel | ||
|
||
private val surveyRepository by withMocks { SurveyRepositoryImpl(networkDataSource) } | ||
|
||
override fun setUpMocks() = injectMocks(mocker) | ||
|
||
@BeforeTest | ||
fun setUp() { | ||
mocker.reset() | ||
} | ||
|
||
@Test | ||
fun `When calling survey with success response, it returns correct object`() = runTest { | ||
mocker.every { | ||
networkDataSource.survey(isAny()) | ||
} returns flow { emit(Pair(listOf(survey), meta)) } | ||
surveyRepository | ||
.survey(1) | ||
.collect { | ||
it.first.first().title shouldBe survey.title | ||
it.second.page shouldBe meta.page | ||
} | ||
} | ||
|
||
@Test | ||
fun `When calling survey with failure response, it returns correct error`() = runTest { | ||
mocker.every { | ||
networkDataSource.survey(isAny()) | ||
} returns flow { error("Fail") } | ||
surveyRepository | ||
.survey(1) | ||
.catch { | ||
it.message shouldBe "Fail" | ||
} | ||
.collect { | ||
fail("Should not return object") | ||
} | ||
} | ||
} |
Oops, something went wrong.