Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions store/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ kotlin {
implementation(libs.turbine)
}
}

val androidDeviceTest by getting {
dependencies {
implementation(libs.junit)
implementation(libs.kotlinx.coroutines.test)
implementation(kotlin("test"))
implementation("androidx.test:runner:1.6.2")
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.mobilenativefoundation.store.store5

import kotlinx.coroutines.test.runTest
import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
import org.mobilenativefoundation.store.store5.impl.extensions.asMutableStore
import org.mobilenativefoundation.store.store5.impl.extensions.fresh
import org.mobilenativefoundation.store.store5.impl.extensions.get
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalStoreApi::class)
class MutableStoreDeviceTest {

@Test
fun mutableStoreGetAndFreshRunOnAndroidDeviceWithoutExplicitResponseGeneric() = runTest {
val store = StoreBuilder.from(
Fetcher.of<String, String> { "remote_$it" },
).build()

val mutableStore: MutableStore<String, String> = store.asMutableStore<String, String, String, String, String>(
updater = Updater.by(
post = { key: String, value: String ->
UpdaterResult.Success.Untyped("saved")
},
),
bookkeeper = null,
)

// Type inference: no <String, String, Unit> generic parameters required!
val getResult = mutableStore.get("item1")
assertEquals("remote_item1", getResult)

val freshResult = mutableStore.fresh("item1")
assertEquals("remote_item1", freshResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,19 @@ fun <Key : Any, Network : Any, Output : Any, Local : Any, Response : Any> Store<
*
* ```
* try {
* store.get<Key, Common, WriteResponse>(Key.Read.All)
* store.get(key)
* } catch (e: Exception) {
* // handle exception
* }
* ```
*
* @param Key The key to get cached data for.
* @param Output The common representation of the data.
* @param Response The updater result write response type.
* @return The data associated with the key.
*/
@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.get(key: Key) =
stream<Response>(StoreReadRequest.cached(key, refresh = false))
suspend fun <Key : Any, Output : Any> MutableStore<Key, Output>.get(key: Key): Output =
stream<Unit>(StoreReadRequest.cached(key, refresh = false))
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
.first()
.requireData()
Expand All @@ -137,20 +136,19 @@ suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.
*
* ```
* try {
* store.fresh<Key, Common, WriteResponse>(Key.Read.All)
* store.fresh(key)
* } catch (e: Exception) {
* // handle exception
* }
* ```
*
* @param Key The key to fetch fresh data for.
* @param Output The common representation of the data.
* @param Response The updater result write response type.
* @return The fresh data associated with the key.
*/
@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.fresh(key: Key) =
stream<Response>(StoreReadRequest.fresh(key))
suspend fun <Key : Any, Output : Any> MutableStore<Key, Output>.fresh(key: Key): Output =
stream<Unit>(StoreReadRequest.fresh(key))
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
.first()
.requireData()
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import org.mobilenativefoundation.store.store5.StoreWriteRequest
import org.mobilenativefoundation.store.store5.StoreWriteResponse
import org.mobilenativefoundation.store.store5.impl.RealMutableStore
import org.mobilenativefoundation.store.store5.impl.RealStore
import org.mobilenativefoundation.store.store5.impl.extensions.fresh
import org.mobilenativefoundation.store.store5.impl.extensions.get
import org.mobilenativefoundation.store.store5.mutablestore.util.TestCache
import org.mobilenativefoundation.store.store5.mutablestore.util.TestConverter
import org.mobilenativefoundation.store.store5.mutablestore.util.TestFetcher
Expand Down Expand Up @@ -66,7 +68,7 @@ class RealMutableStoreTest {
defaultOutputToLocalConverter = { defaultLocalValue },
)
testValidator = TestValidator()
testSourceOfTruth = TestSourceOfTruth()
testSourceOfTruth = TestSourceOfTruth(localToOutput = { Note(it.id, it.content) })
testCache = TestCache()

testFetcher.whenever("key1") {
Expand Down Expand Up @@ -482,4 +484,21 @@ class RealMutableStoreTest {
assertIs<StoreWriteResponse.Success>(response)
assertEquals("someContent", delegateStore.latestOrNull("newKey")?.content)
}

@Test
fun get_givenCachedData_returnsDataDirectlyWithoutExplicitResponseGeneric() =
runTest {
delegateStore.write("testKey", Note("testId", "testContent"))
// Invoking without explicit <Response> type argument
val result = mutableStore.get("testKey")
assertEquals("testContent", result.content)
}

@Test
fun fresh_givenFetcherData_returnsFreshDataDirectlyWithoutExplicitResponseGeneric() =
runTest {
// Invoking without explicit <Response> type argument
val result = mutableStore.fresh("key1")
assertEquals("defaultLocalContent", result.content)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import kotlinx.coroutines.flow.flow
import org.mobilenativefoundation.store.store5.SourceOfTruth

@Suppress("UNCHECKED_CAST")
class TestSourceOfTruth<Key : Any, Local : Any, Output : Any> : SourceOfTruth<Key, Local, Output> {
class TestSourceOfTruth<Key : Any, Local : Any, Output : Any>(
private val localToOutput: ((Local) -> Output)? = null,
) : SourceOfTruth<Key, Local, Output> {
private val storage = HashMap<Key, Local?>()
private val flows = HashMap<Key, MutableSharedFlow<Output?>>()
private var readError: Throwable? = null
Expand Down Expand Up @@ -40,7 +42,8 @@ class TestSourceOfTruth<Key : Any, Local : Any, Output : Any> : SourceOfTruth<Ke
flow {
readError?.let { throw SourceOfTruth.ReadException(key, it) }
val sharedFlow = flows.getOrPut(key) { MutableSharedFlow(replay = 1) }
emit(storage[key] as Output?)
val item = storage[key]?.let { localToOutput?.invoke(it) ?: (it as Output) }
emit(item)
emitAll(sharedFlow)
}

Expand All @@ -50,7 +53,8 @@ class TestSourceOfTruth<Key : Any, Local : Any, Output : Any> : SourceOfTruth<Ke
) {
writeError?.let { throw SourceOfTruth.WriteException(key, value, it) }
storage[key] = value
flows[key]?.emit(value as Output?)
val item = localToOutput?.invoke(value) ?: (value as Output)
flows[key]?.emit(item)
}

override suspend fun delete(key: Key) {
Expand Down