-
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
1 parent
35baa65
commit 1fcf9ff
Showing
18 changed files
with
222 additions
and
111 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
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...railer/presentation/library/testsupport/fooapp/domain/exception/AlreadyExistsException.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,3 @@ | ||
package tech.coner.trailer.presentation.library.testsupport.fooapp.domain.exception | ||
|
||
class AlreadyExistsException(message: String? = null, cause: Throwable? = null) : Exception(message, cause) |
File renamed without changes.
48 changes: 48 additions & 0 deletions
48
...oner/trailer/presentation/library/testsupport/fooapp/domain/service/TestableFooService.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,48 @@ | ||
package tech.coner.trailer.presentation.library.testsupport.fooapp.domain.service | ||
|
||
import tech.coner.trailer.presentation.library.testsupport.fooapp.data.service.FooService | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.FOO_ID_BAR | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.FOO_ID_BAT | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.FOO_ID_BAZ | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.FOO_ID_FOO | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.Foo | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.exception.AlreadyExistsException | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.exception.NotFoundException | ||
import tech.coner.trailer.toolkit.konstraints.Constraint | ||
|
||
class TestableFooService(private val constraint: Constraint<Foo>) : FooService { | ||
|
||
private val map: MutableMap<Foo.Id, Foo> = arrayOf( | ||
FOO_ID_FOO to "foo", | ||
FOO_ID_BAR to "bar", | ||
FOO_ID_BAZ to "baz", | ||
FOO_ID_BAT to "bat", | ||
) | ||
.associate { (idValue, name) -> Foo.Id(idValue).let { it to Foo(it, name) } } | ||
.toMutableMap() | ||
|
||
override suspend fun create(create: Foo): Result<Foo> { | ||
if (map.containsKey(create.id)) { | ||
return Result.failure(AlreadyExistsException()) | ||
} | ||
return constraint(create) | ||
.map { map[create.id] = create; create } | ||
} | ||
|
||
override suspend fun findById(id: Foo.Id): Result<Foo> { | ||
return map[id]?.let { Result.success(it) } | ||
?: Result.failure(NotFoundException()) | ||
} | ||
|
||
override suspend fun update(update: Foo): Result<Foo> { | ||
return findById(update.id) | ||
.mapCatching { constraint(update).getOrThrow() } | ||
.map { map[update.id] = update; update } | ||
} | ||
|
||
override suspend fun deleteById(id: Foo.Id): Result<Foo> { | ||
return map.remove(id) | ||
?.let { Result.success(it) } | ||
?: Result.failure(NotFoundException()) | ||
} | ||
} |
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
18 changes: 12 additions & 6 deletions
18
...pp/presentation/presenter/FooPresenter.kt → ...sentation/presenter/FooDetailPresenter.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 |
---|---|---|
@@ -1,19 +1,25 @@ | ||
package tech.coner.trailer.presentation.library.testsupport.fooapp.presentation.presenter | ||
|
||
import kotlin.coroutines.CoroutineContext | ||
import tech.coner.trailer.presentation.library.presenter.LoadableItemPresenter | ||
import tech.coner.trailer.presentation.library.state.LoadableItem | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.presentation.model.FooModel | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.presentation.state.FooState | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.data.service.FooService | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.Foo | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.presentation.adapter.FooAdapter | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.presentation.model.FooModel | ||
|
||
class FooPresenter(override val argument: Foo.Id) : LoadableItemPresenter< | ||
class FooDetailPresenter( | ||
override val argument: Foo.Id, | ||
private val service: FooService, | ||
override val coroutineContext: CoroutineContext | ||
) : LoadableItemPresenter< | ||
Foo.Id, | ||
FooState, | ||
Foo, | ||
Unit, | ||
FooModel | ||
>() { | ||
override val initialState = FooState(LoadableItem.Empty()) | ||
override val adapter = FooAdapter() | ||
|
||
override suspend fun performLoad(): Result<Foo> { | ||
return service.findById(argument) | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...c/test/kotlin/tech/coner/trailer/presentation/library/presenter/FooDetailPresenterTest.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,83 @@ | ||
package tech.coner.trailer.presentation.library.presenter | ||
|
||
import app.cash.turbine.test | ||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isInstanceOf | ||
import assertk.assertions.isNotNull | ||
import assertk.assertions.prop | ||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
import tech.coner.trailer.presentation.library.model.LoadableModel | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.constraint.FooConstraint | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.FOO_ID_FOO | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.entity.Foo | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.exception.NotFoundException | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.domain.service.TestableFooService | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.presentation.model.FooModel | ||
import tech.coner.trailer.presentation.library.testsupport.fooapp.presentation.presenter.FooDetailPresenter | ||
|
||
class FooDetailPresenterTest { | ||
|
||
@Test | ||
fun itsModelFlowShouldBeAdaptedFromInitialState() = runTest { | ||
val id = Foo.Id(FOO_ID_FOO) | ||
val presenter = createPresenter(id) | ||
|
||
presenter.modelFlow.test { | ||
assertThat(expectMostRecentItem()) | ||
.isEqualTo(LoadableModel.Empty(null)) | ||
} | ||
} | ||
|
||
@Test | ||
fun itsModelFlowShouldEmitWhenLoadingAndLoaded() = runTest { | ||
val id = Foo.Id(FOO_ID_FOO) | ||
val presenter = createPresenter(id) | ||
|
||
presenter.modelFlow.test { | ||
skipItems(1) | ||
|
||
presenter.load() | ||
|
||
assertThat(awaitItem()) | ||
.isInstanceOf<LoadableModel.Loading<Unit, FooModel>>() | ||
assertThat(awaitItem()) | ||
.isInstanceOf<LoadableModel.Loaded<Unit, FooModel>>() | ||
} | ||
} | ||
|
||
@Test | ||
fun itsModelFlowShouldEmitWhenLoadingAndLoadFailed() = runTest { | ||
val id = Foo.Id(Int.MAX_VALUE) | ||
val presenter = createPresenter(id) | ||
|
||
presenter.modelFlow.test { | ||
skipItems(1) | ||
|
||
presenter.load() | ||
|
||
assertThat(awaitItem()) | ||
.isInstanceOf<LoadableModel.Loading<Unit, FooModel>>() | ||
assertThat(awaitItem()) | ||
.isInstanceOf<LoadableModel.LoadFailed<Unit, FooModel>>() | ||
.prop(LoadableModel.LoadFailed<Unit, FooModel>::cause) | ||
.isNotNull() | ||
.isInstanceOf<NotFoundException>() | ||
} | ||
} | ||
|
||
} | ||
|
||
private fun TestScope.createPresenter(argument: Foo.Id): FooDetailPresenter { | ||
return FooDetailPresenter( | ||
argument = argument, | ||
service = TestableFooService( | ||
constraint = FooConstraint() | ||
), | ||
coroutineContext = coroutineContext + Job() + CoroutineName("FooDetailPresenter") | ||
) | ||
} |
32 changes: 0 additions & 32 deletions
32
...est/kotlin/tech/coner/trailer/presentation/library/presenter/LoadableItemPresenterTest.kt
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
...oner/trailer/presentation/library/testsupport/fooapp/domain/service/TestableFooService.kt
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...tech/coner/trailer/presentation/library/testsupport/fooapp/presentation/state/FooState.kt
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.