Skip to content

Commit

Permalink
Fixes #53 Throw and error if no configs rather than infinitely loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ncipollo committed Oct 7, 2023
1 parent da64e9f commit f336520
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.tix.feature.plan.domain.ticket

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.*
import org.tix.config.data.TixConfiguration
import org.tix.domain.FlowTransformer
import org.tix.feature.plan.domain.error.TicketPlanningException
import org.tix.ticket.Ticket

class TicketPlannerUseCase(
Expand All @@ -14,8 +12,12 @@ class TicketPlannerUseCase(
override fun transformFlow(upstream: Flow<TicketPlannerAction>) =
upstream.flatMapLatest { action ->
val planners = plannerFactory.planners(action.shouldDryRun, action.config)
planners.asFlow()
.flatMapConcat { it.plan(action.tickets) }
if (planners.isEmpty()) {
flowOf(TicketPlanFailed(TicketPlanningException("no ticket systems configs")))
} else {
planners.asFlow()
.flatMapConcat { it.plan(action.tickets) }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import app.cash.turbine.test
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.tix.domain.transform
import org.tix.feature.plan.domain.error.TicketPlanningException
import org.tix.fixture.config.tixConfiguration
import org.tix.ticket.Ticket
import kotlin.test.Test
import kotlin.test.expect

class TicketPlannerUseCaseTest {
private val config = tixConfiguration
private val plannerFactory = MockTicketPlannerFactory(2)

private val tickets = listOf(Ticket(title = "a"), Ticket(title = "b"))

private val useCase = TicketPlannerUseCase(plannerFactory)


@Test
fun transform() = runTest {
val plannerFactory = MockTicketPlannerFactory(2)
val useCase = TicketPlannerUseCase(plannerFactory)
val action = TicketPlannerAction(config, shouldDryRun = true, tickets)
flowOf(action)
.transform(useCase)
Expand All @@ -34,6 +37,21 @@ class TicketPlannerUseCaseTest {
}
}

@Test
fun transform_noConfigs() = runTest {
val plannerFactory = MockTicketPlannerFactory(0)
val useCase = TicketPlannerUseCase(plannerFactory)
val action = TicketPlannerAction(config, shouldDryRun = true, tickets)
flowOf(action)
.transform(useCase)
.test {
val expected = TicketPlanFailed(TicketPlanningException("no ticket systems configs"))
expect(expected) { awaitItem() }
awaitComplete()
plannerFactory.assertPlannersCalled(shouldDryRun = true, config)
}
}

private suspend fun TurbineTestContext<TicketPlanStatus>.expectTicketCreated(ticketTitle: String) =
expect(ticketTitle) {
val status = this.awaitItem() as TicketPlanUpdated
Expand Down

0 comments on commit f336520

Please sign in to comment.