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,37 @@
package org.mobilenativefoundation.store.store5

import kotlinx.coroutines.test.runTest
import org.mobilenativefoundation.store.store5.impl.extensions.freshOrNull
import org.mobilenativefoundation.store.store5.impl.extensions.freshOrResult
import org.mobilenativefoundation.store.store5.impl.extensions.getOrNull
import org.mobilenativefoundation.store.store5.impl.extensions.getOrResult
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

class SafeExtensionsDeviceTest {

@Test
fun safeGetAndFreshExtensionsRunOnAndroidDevice() = runTest {
val store = StoreBuilder.from(
Fetcher.of<String, String> {
if (it == "fail") throw IllegalStateException("Device error") else "android_$it"
},
).build()

val successResult = store.getOrResult("success")
assertTrue(successResult.isSuccess)
assertEquals("android_success", successResult.getOrNull())

val failureResult = store.getOrResult("fail")
assertTrue(failureResult.isFailure)
assertTrue(failureResult.exceptionOrNull() is IllegalStateException)

val nullValue = store.getOrNull("fail")
assertNull(nullValue)

val freshVal = store.freshOrNull("freshKey")
assertEquals("android_freshKey", freshVal)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mobilenativefoundation.store.store5.impl.extensions

import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.filterNot
import kotlinx.coroutines.flow.first
import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
Expand Down Expand Up @@ -63,6 +64,58 @@ suspend fun <Key : Any, Output : Any> Store<Key, Output>.fresh(key: Key) =
.first()
.requireData()

/**
* Returns data for [key] if cached, otherwise fresh data, wrapped in [Result].
* Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException].
*
* @param key The key to get cached data for.
* @return [Result] containing the data or the failure.
*/
suspend fun <Key : Any, Output : Any> Store<Key, Output>.getOrResult(key: Key): Result<Output> =
try {
Result.success(get(key))
} catch (cancellation: CancellationException) {
throw cancellation
} catch (e: Throwable) {
Result.failure(e)
}

/**
* Returns data for [key] if cached, otherwise fresh data.
* Returns `null` if an error occurs while fetching or reading data.
*
* @param key The key to get cached data for.
* @return The data associated with the key, or null if an error occurred.
*/
suspend fun <Key : Any, Output : Any> Store<Key, Output>.getOrNull(key: Key): Output? =
getOrResult(key).getOrNull()

/**
* Returns fresh data for [key] while updating caches, wrapped in [Result].
* Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException].
*
* @param key The key to fetch fresh data for.
* @return [Result] containing the fresh data or the failure.
*/
suspend fun <Key : Any, Output : Any> Store<Key, Output>.freshOrResult(key: Key): Result<Output> =
try {
Result.success(fresh(key))
} catch (cancellation: CancellationException) {
throw cancellation
} catch (e: Throwable) {
Result.failure(e)
}

/**
* Returns fresh data for [key] while updating caches.
* Returns `null` if an error occurs while fetching data.
*
* @param key The key to fetch fresh data for.
* @return The fresh data associated with the key, or null if an error occurred.
*/
suspend fun <Key : Any, Output : Any> Store<Key, Output>.freshOrNull(key: Key): Output? =
freshOrResult(key).getOrNull()

/**
* Extension function to convert a [Store] into a [MutableStore].
*
Expand Down Expand Up @@ -154,3 +207,59 @@ suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
.first()
.requireData()

/**
* Returns data for the given [key] if cached, otherwise fresh data, wrapped in [Result].
* Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException].
*
* @param key The key to get cached data for.
* @return [Result] containing the data or the failure.
*/
@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.getOrResult(key: Key): Result<Output> =
try {
Result.success(get<Key, Output, Response>(key))
} catch (cancellation: CancellationException) {
throw cancellation
} catch (e: Throwable) {
Result.failure(e)
}

/**
* Returns data for the given [key] if cached, otherwise fresh data.
* Returns `null` if an error occurs while fetching or reading data.
*
* @param key The key to get cached data for.
* @return The data associated with the key, or null if an error occurred.
*/
@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.getOrNull(key: Key): Output? =
getOrResult<Key, Output, Response>(key).getOrNull()

/**
* Returns fresh data for the given [key] while updating caches, wrapped in [Result].
* Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException].
*
* @param key The key to fetch fresh data for.
* @return [Result] containing the fresh data or the failure.
*/
@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.freshOrResult(key: Key): Result<Output> =
try {
Result.success(fresh<Key, Output, Response>(key))
} catch (cancellation: CancellationException) {
throw cancellation
} catch (e: Throwable) {
Result.failure(e)
}

/**
* Returns fresh data for the given [key] while updating caches.
* Returns `null` if an error occurs while fetching data.
*
* @param key The key to fetch fresh data for.
* @return The fresh data associated with the key, or null if an error occurred.
*/
@OptIn(ExperimentalStoreApi::class)
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.freshOrNull(key: Key): Output? =
freshOrResult<Key, Output, Response>(key).getOrNull()
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.mobilenativefoundation.store.store5

import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.mobilenativefoundation.store.store5.impl.extensions.freshOrNull
import org.mobilenativefoundation.store.store5.impl.extensions.freshOrResult
import org.mobilenativefoundation.store.store5.impl.extensions.getOrNull
import org.mobilenativefoundation.store.store5.impl.extensions.getOrResult
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
import kotlin.test.assertTrue

class StoreExtensionsTest {

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

val result = store.getOrResult("key1")
assertTrue(result.isSuccess)
assertEquals("fetched_key1", result.getOrNull())
}

@Test
fun getOrResult_returnsFailureWhenFetcherThrows() = runTest {
val store = StoreBuilder.from(
Fetcher.of<String, String> { throw IllegalStateException("Network error") },
).build()

val result = store.getOrResult("key1")
assertTrue(result.isFailure)
assertTrue(result.exceptionOrNull() is IllegalStateException)
assertEquals("Network error", result.exceptionOrNull()?.message)
}

@Test
fun getOrNull_returnsNullWhenFetcherThrows() = runTest {
val store = StoreBuilder.from(
Fetcher.of<String, String> { throw IllegalStateException("Network error") },
).build()

val value = store.getOrNull("key1")
assertNull(value)
}

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

val result = store.freshOrResult("key1")
assertTrue(result.isSuccess)
assertEquals("fresh_key1", result.getOrNull())
}

@Test
fun freshOrResult_returnsFailureWhenFetcherThrows() = runTest {
val store = StoreBuilder.from(
Fetcher.of<String, String> { throw RuntimeException("Failed to fetch fresh data") },
).build()

val result = store.freshOrResult("key1")
assertTrue(result.isFailure)
assertTrue(result.exceptionOrNull() is RuntimeException)
}

@Test
fun freshOrNull_returnsNullWhenFetcherThrows() = runTest {
val store = StoreBuilder.from(
Fetcher.of<String, String> { throw RuntimeException("Failed to fetch fresh data") },
).build()

val value = store.freshOrNull("key1")
assertNull(value)
}

@Test
fun getOrResult_rethrowsCancellationException() = runTest {
val store = StoreBuilder.from(
Fetcher.ofFlow<String, String> { key ->
flow {
throw CancellationException("Cancelled")
}
},
).build()

assertFailsWith<CancellationException> {
store.getOrResult("key1")
}
}
}