Skip to content

Commit

Permalink
Merge pull request #29 from Trendyol/testing-infra
Browse files Browse the repository at this point in the history
Testing Infra
  • Loading branch information
yigitozgumus authored Dec 15, 2024
2 parents d156b64 + 4f25a84 commit bb6cce2
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.trendyol.transmission.features.colorpicker

import androidx.compose.ui.graphics.Color
import com.trendyol.transmission.components.features.ColorPickerUiState
import com.trendyol.transmission.components.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.components.features.colorpicker.ColorPickerSignal
import com.trendyol.transmission.components.features.colorpicker.ColorPickerTransformer
import com.trendyol.transmission.transformer.util.TestCoroutineRule
import com.trendyol.transmission.components.features.ColorPickerUiState
import com.trendyol.transmissiontest.attachToRouter
import com.trendyol.transmissiontest.test
import org.junit.Before
Expand Down Expand Up @@ -70,8 +70,9 @@ class ColorPickerTransformerTest {

@Test
fun `GIVEN inputTransformer, WHEN SelectColor signal is sent, THEN SelectedColorUpdate is sent to MultiOutputTransformer`() {
sut.attachToRouter().test(signal = ColorPickerSignal.SelectColor(3, Color.Blue)) {
assertTrue { effectStream.last() is ColorPickerEffect.SelectedColorUpdate }
}
sut.attachToRouter()
.test(signal = ColorPickerSignal.SelectColor(3, Color.Blue)) {
assertTrue { effectStream.last() is ColorPickerEffect.SelectedColorUpdate }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.trendyol.transmission.components.features.output.OutputTransformer
import com.trendyol.transmission.transformer.util.TestCoroutineRule
import com.trendyol.transmission.components.features.ColorPickerUiState
import com.trendyol.transmission.components.features.OutputUiState
import com.trendyol.transmission.components.features.colorpicker.ColorPickerTransformer
import com.trendyol.transmissiontest.attachToRouter
import com.trendyol.transmissiontest.test
import org.junit.Before
Expand Down Expand Up @@ -45,8 +46,8 @@ class OutputTransformerTest {
@Test
fun `GIVEN sut, WHEN inputUpdate effect comes and ColorPickerUIState exists, THEN RouterPayloadEffect should be published`() {
sut.attachToRouter()
.register {
addQueryData(ColorPickerUiState(), key = "ColorPickerUiState")
.registerData(ColorPickerTransformer.holderContract) {
ColorPickerUiState()
}
.test(effect = InputEffect.InputUpdate("test")) {
assertEquals(OutputUiState(outputText = "test"), dataStream[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.trendyol.transmissiontest

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.router.TransmissionRouter
import com.trendyol.transmission.router.RegistryScope
import com.trendyol.transmission.router.builder.TransmissionTestingRouterBuilder
import com.trendyol.transmission.router.builder.TransmissionRouterBuilder
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.request.Contract
import com.trendyol.transmissiontest.computation.ComputationTransformer
import com.trendyol.transmissiontest.computation.ComputationWithArgsTransformer
import com.trendyol.transmissiontest.data.DataTransformer
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
Expand All @@ -15,23 +18,39 @@ import kotlinx.coroutines.test.runTest

@OptIn(ExperimentalCoroutinesApi::class)
class TestSuite {
private var orderedInitialProcessing: List<Transmission> = emptyList()
private var orderedInitialProcessing: MutableList<Transmission> = mutableListOf()
private var transformer: Transformer? = null
private var registryScope: RegistryScope.() -> Unit = {}
private lateinit var router: TransmissionRouter
private val supplementaryTransformerSet: MutableList<Transformer> = mutableListOf()

fun initialize(transformer: Transformer): TestSuite {
this.transformer = transformer
return this
}

fun register(registry: RegistryScope.() -> Unit = {}): TestSuite {
this.registryScope = registry
fun <D : Transmission.Data?> registerData(
contract: Contract.DataHolder<D>, data: () -> D
): TestSuite {
supplementaryTransformerSet += DataTransformer(contract, data)
return this
}

fun <C : Contract.Computation<D?>, D : Any> registerComputation(
contract: C, data: () -> D?
): TestSuite {
supplementaryTransformerSet += ComputationTransformer(contract, data)
return this
}

fun <C : Contract.ComputationWithArgs<A,D?>, D : Any, A: Any> registerComputation(
contract: C, data: () -> D?
): TestSuite {
supplementaryTransformerSet += ComputationWithArgsTransformer(contract, data)
return this
}

fun processBeforeTesting(vararg transmissions: Transmission): TestSuite {
orderedInitialProcessing = transmissions.toList()
orderedInitialProcessing += transmissions.toList()
return this
}

Expand All @@ -41,10 +60,11 @@ class TestSuite {
transmission: Transmission,
scope: suspend TransformerTestScope.(scope: TestScope) -> Unit
) {
router = TransmissionTestingRouterBuilder.build {
router = TransmissionRouterBuilder.build {
addDispatcher(UnconfinedTestDispatcher())
this@TestSuite.transformer?.let { addTransformerSet(setOf(it)) }
testing(this@TestSuite.registryScope)
this@TestSuite.transformer?.let {
addTransformerSet((listOf(it) + supplementaryTransformerSet).toSet())
}
}

runTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.trendyol.transmissiontest.computation

import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.request.Contract
import com.trendyol.transmission.transformer.request.computation.Computations
import com.trendyol.transmission.transformer.request.computation.createComputations
import com.trendyol.transmission.transformer.request.computation.register
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher

@OptIn(ExperimentalCoroutinesApi::class)
internal class ComputationTransformer<C: Contract.Computation<D?>, D: Any?>(
contract: C, data: () -> D?
): Transformer(dispatcher = UnconfinedTestDispatcher()) {
override val computations: Computations = createComputations {
register(contract) {
data()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.trendyol.transmissiontest.computation

import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.request.Contract
import com.trendyol.transmission.transformer.request.computation.Computations
import com.trendyol.transmission.transformer.request.computation.createComputations
import com.trendyol.transmission.transformer.request.computation.register
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher

@OptIn(ExperimentalCoroutinesApi::class)
internal class ComputationWithArgsTransformer<C: Contract.ComputationWithArgs<A, D?>, D: Any?, A: Any>(
contract: C, data: () -> D?
): Transformer(dispatcher = UnconfinedTestDispatcher()) {
override val computations: Computations = createComputations {
register(contract) {
data()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.trendyol.transmissiontest.data

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.dataHolder
import com.trendyol.transmission.transformer.request.Contract
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher

@OptIn(ExperimentalCoroutinesApi::class)
class DataTransformer<D : Transmission.Data?>(
contract: Contract.DataHolder<D>, data: () -> D
) : Transformer(dispatcher = UnconfinedTestDispatcher()) {

private val dataHolder = dataHolder(
initialValue = data(),
contract = contract,
publishUpdates = false
)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import kotlinx.coroutines.launch
internal class RequestDelegate(
private val queryScope: CoroutineScope,
private val routerRef: TransmissionRouter,
private val registry: RegistryScopeImpl? = null,
) : RequestHandler {

private val routerQueryResultChannel: MutableSharedFlow<QueryResult> = MutableSharedFlow()
Expand All @@ -36,11 +35,7 @@ internal class RequestDelegate(

init {
queryScope.launch {
if (registry != null) {
outGoingQuery.consumeAsFlow().collect { testQuery(it) }
} else {
outGoingQuery.consumeAsFlow().collect { processQuery(it) }
}
outGoingQuery.consumeAsFlow().collect { processQuery(it) }
}
}

Expand Down Expand Up @@ -164,56 +159,6 @@ internal class RequestDelegate(

// endregion

// region test queries

private fun testQuery(query: Query) = queryScope.launch {
when (query) {
is Query.Computation -> testComputationQuery(query)
is Query.Data -> testDataQuery(query)
is Query.ComputationWithArgs<*> -> testComputationQueryWithArgs(query)
is Query.Execution -> {}
is Query.ExecutionWithArgs<*> -> {}
}
}

private fun testDataQuery(
query: Query.Data,
) = queryScope.launch {
val dataToSend = QueryResult.Data(
owner = query.sender,
data = registry?.dataMap?.get(query.key),
key = query.key,
resultIdentifier = query.queryIdentifier
)
queryScope.launch {
queryResultChannel.trySend(dataToSend)
}
}

private fun testComputationQuery(
query: Query.Computation,
) = queryScope.launch {
val computationToSend = QueryResult.Computation(
owner = query.sender,
data = registry?.computationMap?.get(query.key),
key = query.key,
resultIdentifier = query.queryIdentifier
)
queryResultChannel.trySend(computationToSend)
}

private fun <A : Any> testComputationQueryWithArgs(
query: Query.ComputationWithArgs<A>,
) = queryScope.launch {
val computationToSend = QueryResult.Computation(
owner = query.sender,
data = registry?.computationMap?.get(query.key),
key = query.key,
resultIdentifier = query.queryIdentifier
)
queryResultChannel.trySend(computationToSend)
}

// region Request Handler

override suspend fun <C : Contract.DataHolder<D>, D : Transmission.Data> getData(contract: C): D? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class TransmissionRouter internal constructor(
internal val transformerSetLoader: TransformerSetLoader? = null,
internal val autoInitialization: Boolean = true,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
registryScope: RegistryScopeImpl? = null
) {

private val exceptionHandler = CoroutineExceptionHandler { _, _ -> }
Expand All @@ -52,7 +51,6 @@ class TransmissionRouter internal constructor(
private val _requestDelegate = RequestDelegate(
queryScope = routerScope,
routerRef = this@TransmissionRouter,
registry = registryScope
)

val requestHelper: RequestHandler = _requestDelegate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package com.trendyol.transmission.router.builder

import com.trendyol.transmission.router.RegistryScope
import com.trendyol.transmission.router.RegistryScopeImpl
import com.trendyol.transmission.router.loader.TransformerSetLoader
import com.trendyol.transmission.transformer.Transformer
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

internal class TransmissionRouterBuilderInternal internal constructor(
scope: TransmissionTestingRouterBuilderScope.() -> Unit
scope: TransmissionRouterBuilderScope.() -> Unit
) {

internal var dispatcher: CoroutineDispatcher = Dispatchers.Default
internal var registryScope: RegistryScopeImpl? = null
internal lateinit var transformerSetLoader: TransformerSetLoader
internal var autoInitialization: Boolean = true

private val scopeImpl = object : TransmissionTestingRouterBuilderScope {

override fun testing(scope: RegistryScope.() -> Unit) {
this@TransmissionRouterBuilderInternal.registryScope = RegistryScopeImpl().apply(scope)
}
private val scopeImpl = object : TransmissionRouterBuilderScope {

override fun addDispatcher(dispatcher: CoroutineDispatcher) {
this@TransmissionRouterBuilderInternal.dispatcher = dispatcher

}

override fun addTransformerSet(transformerSet: Set<Transformer>) {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ sealed interface Contract {
internal val key: String,
) : Contract

class Computation<T : Any> internal constructor(
class Computation<T : Any?> internal constructor(
internal val key: String,
internal val useCache: Boolean = false
) : Contract

class ComputationWithArgs<A : Any, T : Any> internal constructor(
class ComputationWithArgs<A : Any, T : Any?> internal constructor(
internal val key: String,
internal val useCache: Boolean = false
) : Contract
Expand Down
Loading

0 comments on commit bb6cce2

Please sign in to comment.