Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Tests to Use Turbine #672

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ktlint = "0.39.0"
kover = "0.9.0-RC"
store = "5.1.0-SNAPSHOT"
truth = "1.1.3"
turbine = "1.2.0"
binary-compatibility-validator = "0.15.0-Beta.2"

[libraries]
Expand Down Expand Up @@ -56,7 +57,7 @@ kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-cor
junit = { group = "junit", name = "junit", version.ref = "junit" }
google-truth = { group = "com.google.truth", name = "truth", version.ref = "truth" }
touchlab-kermit = { group = "co.touchlab", name = "kermit", version.ref = "kermit" }
turbine = "app.cash.turbine:turbine:1.0.0"
turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }
binary-compatibility-validator = {module = "org.jetbrains.kotlinx:binary-compatibility-validator", version.ref = "binary-compatibility-validator"}

[plugins]
Expand Down
1 change: 1 addition & 0 deletions store/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kotlin {
dependencies {
implementation(libs.junit)
implementation(libs.kotlinx.coroutines.test)
implementation(libs.turbine)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.mobilenativefoundation.store.store5

import app.cash.turbine.test
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.mobilenativefoundation.store.store5.util.assertEmitsExactly
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand All @@ -34,9 +34,9 @@ class FetcherResponseTests {
}

@Test
fun givenAFetcherThatEmitsErrorAndDataWhenSteamingThenItCanEmitValueAfterAnError() {
val exception = RuntimeException("first error")
fun givenAFetcherThatEmitsErrorAndDataWhenSteamingThenItCanEmitValueAfterAnError() =
testScope.runTest {
val exception = RuntimeException("first error")
val store =
StoreBuilder.from(
fetcher =
Expand All @@ -48,18 +48,23 @@ class FetcherResponseTests {
},
).buildWithTestScope()

assertEmitsExactly(
store.stream(
StoreReadRequest.fresh(1),
),
listOf(
store.stream(StoreReadRequest.fresh(1)).test {
assertEquals(
StoreReadResponse.Loading(StoreReadResponseOrigin.Fetcher()),
awaitItem(),
)

assertEquals(
StoreReadResponse.Error.Exception(exception, StoreReadResponseOrigin.Fetcher()),
awaitItem(),
)

assertEquals(
StoreReadResponse.Data("1", StoreReadResponseOrigin.Fetcher()),
),
)
awaitItem(),
)
}
}
}

@Test
fun givenTransformerWhenRawValueThenUnwrappedValueReturnedAndValueIsCached() =
Expand All @@ -69,27 +74,32 @@ class FetcherResponseTests {
StoreBuilder
.from(fetcher).buildWithTestScope()

assertEmitsExactly(
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
listOf(
pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
assertEquals(
StoreReadResponse.Loading(
origin = StoreReadResponseOrigin.Fetcher(),
),
awaitItem(),
)

assertEquals(
StoreReadResponse.Data(
value = 9,
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
assertEmitsExactly(
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
listOf(
awaitItem(),
)
}

pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
assertEquals(
StoreReadResponse.Data(
value = 9,
origin = StoreReadResponseOrigin.Cache,
),
),
)
awaitItem(),
)
}
}

@Test
Expand All @@ -110,30 +120,39 @@ class FetcherResponseTests {
StoreBuilder.from(fetcher)
.buildWithTestScope()

assertEmitsExactly(
pipeline.stream(StoreReadRequest.fresh(3)),
listOf(
pipeline.stream(StoreReadRequest.fresh(3)).test {
assertEquals(
StoreReadResponse.Loading(
origin = StoreReadResponseOrigin.Fetcher(),
),
awaitItem(),
)

assertEquals(
StoreReadResponse.Error.Message(
message = "zero",
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
assertEmitsExactly(
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
listOf(
awaitItem(),
)
}

pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
assertEquals(
StoreReadResponse.Loading(
origin = StoreReadResponseOrigin.Fetcher(),
),
awaitItem(),
)

assertEquals(
StoreReadResponse.Data(
value = 1,
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
awaitItem(),
)
}
}

@Test
Expand All @@ -156,30 +175,39 @@ class FetcherResponseTests {
.from(fetcher)
.buildWithTestScope()

assertEmitsExactly(
pipeline.stream(StoreReadRequest.fresh(3)),
listOf(
pipeline.stream(StoreReadRequest.fresh(3)).test {
assertEquals(
StoreReadResponse.Loading(
origin = StoreReadResponseOrigin.Fetcher(),
),
awaitItem(),
)

assertEquals(
StoreReadResponse.Error.Exception(
error = e,
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
assertEmitsExactly(
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
listOf(
awaitItem(),
)
}

pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
assertEquals(
StoreReadResponse.Loading(
origin = StoreReadResponseOrigin.Fetcher(),
),
awaitItem(),
)

assertEquals(
StoreReadResponse.Data(
value = 1,
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
awaitItem(),
)
}
}

@Test
Expand All @@ -200,36 +228,46 @@ class FetcherResponseTests {
.from(fetcher = fetcher)
.buildWithTestScope()

assertEmitsExactly(
pipeline.stream(StoreReadRequest.fresh(3)),
listOf(
pipeline.stream(StoreReadRequest.fresh(3)).test {
assertEquals(
StoreReadResponse.Loading(
origin = StoreReadResponseOrigin.Fetcher(),
),
awaitItem(),
)

assertEquals(
StoreReadResponse.Error.Exception(
error = e,
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
assertEmitsExactly(
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
listOf(
awaitItem(),
)
}

pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
assertEquals(
StoreReadResponse.Loading(
origin = StoreReadResponseOrigin.Fetcher(),
),
awaitItem(),
)

assertEquals(
StoreReadResponse.Data(
value = 1,
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
awaitItem(),
)
}
}

@Test
fun givenAFetcherThatEmitsCustomErrorWhenStreamingThenCustomErrorShouldBeEmitted() =
testScope.runTest {
data class TestCustomError(val errorMessage: String)

val customError = TestCustomError("Test custom error")

val store =
Expand All @@ -242,16 +280,20 @@ class FetcherResponseTests {
},
).buildWithTestScope()

assertEmitsExactly(
store.stream(StoreReadRequest.fresh(1)),
listOf(
store.stream(StoreReadRequest.fresh(1)).test {
assertEquals(
StoreReadResponse.Loading(origin = StoreReadResponseOrigin.Fetcher()),
awaitItem(),
)

assertEquals(
StoreReadResponse.Error.Custom(
error = customError,
origin = StoreReadResponseOrigin.Fetcher(),
),
),
)
awaitItem(),
)
}
}

private fun <Key : Any, Output : Any> StoreBuilder<Key, Output>.buildWithTestScope() = scope(testScope).build()
Expand Down
Loading