Skip to content

Commit

Permalink
Merge pull request #11 from makeevrserg/fix-tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
makeevrserg authored Jun 26, 2024
2 parents 67d7a37 + fba7d09 commit 12b061d
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 140 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ makeevrserg.java.ktarget=17
# Project
makeevrserg.project.name=KStorage
makeevrserg.project.group=ru.astrainteractive.klibs
makeevrserg.project.version.string=2.4.0
makeevrserg.project.version.string=2.4.1
makeevrserg.project.description=Kotlin wrapper for key-value storage libraries
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://github.com/makeevrserg/klibs.kstorage/
Expand Down
2 changes: 1 addition & 1 deletion kstorage/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ kotlin {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(libs.kotlin.coroutines.test)
}
}
val jvmTest by getting {
dependencies {
implementation(libs.kotlin.coroutines.test)
implementation(libs.androidx.datastore.preferences.core)
implementation(libs.androidx.datastore.core.okio)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class DefaultMutableKrate<T>(
) : Krate.Mutable<T> {

private var _cachedValue: T = loader.loadAndGet() ?: factory.create()

override val cachedValue: T
get() = _cachedValue

Expand All @@ -28,7 +27,6 @@ class DefaultMutableKrate<T>(
}

override fun save(value: T) {
if (saver is ValueSaver.Empty) return
saver.save(value)
_cachedValue = value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.astrainteractive.klibs.kstorage.api.impl
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import ru.astrainteractive.klibs.kstorage.api.StateFlowKrate
import ru.astrainteractive.klibs.kstorage.api.provider.ValueFactory
import ru.astrainteractive.klibs.kstorage.api.provider.ValueLoader
Expand All @@ -24,13 +25,12 @@ class DefaultStateFlowMutableKrate<T>(
override val cachedStateFlow: StateFlow<T> = _stateFlow.asStateFlow()

override fun save(value: T) {
if (saver is ValueSaver.Empty) return
saver.save(value)
_stateFlow.value = value
_stateFlow.update { value }
}

override fun loadAndGet(): T {
_stateFlow.value = loader.loadAndGet() ?: factory.create()
_stateFlow.update { loader.loadAndGet() ?: factory.create() }
return cachedValue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ interface StateFlowSuspendKrate<T> : SuspendKrate<T>, CachedKrate.Coroutine<T> {
override val cachedValue: T
get() = cachedStateFlow.value

interface Mutable<T> : StateFlowSuspendKrate<T>, SuspendKrate.Mutable<T>
interface Mutable<T> : StateFlowSuspendKrate<T>, SuspendKrate.Mutable<T> {
override val cachedStateFlow: StateFlow<T>
override val cachedValue: T
get() = cachedStateFlow.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class DefaultFlowMutableKrate<T>(
}

override suspend fun save(value: T) {
if (saver is SuspendValueSaver.Empty) return
saver.save(value)
_cachedValue = value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.astrainteractive.klibs.kstorage.suspend.impl
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import ru.astrainteractive.klibs.kstorage.api.provider.ValueFactory
import ru.astrainteractive.klibs.kstorage.suspend.StateFlowSuspendKrate
import ru.astrainteractive.klibs.kstorage.suspend.provider.SuspendValueLoader
Expand All @@ -18,14 +19,13 @@ class DefaultSuspendMutableKrate<T>(

override suspend fun loadAndGet(): T {
val value = loader.loadAndGet() ?: factory.create()
_cachedStateFlow.value = value
_cachedStateFlow.update { value }
return value
}

override suspend fun save(value: T) {
if (saver is SuspendValueSaver.Empty) return
saver.save(value)
_cachedStateFlow.value = value
_cachedStateFlow.update { value }
}

override suspend fun reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,103 @@
package ru.astrainteractive.klibs.kstorage

import ru.astrainteractive.klibs.kstorage.test.StoreMutableKrate
import ru.astrainteractive.klibs.kstorage.util.KrateDefaultExt.withDefault
import kotlinx.coroutines.test.runTest
import ru.astrainteractive.klibs.kstorage.api.impl.DefaultMutableKrate
import ru.astrainteractive.klibs.kstorage.test.SampleStore
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Test cases:
* 1. Factory value not null, loader is null.
* 2. Factory value is null, loader not null
* 3. Factory value is one, the loader is another
* 4. Factory value is null,m the loader is null
*/
internal class MutableKrateTest {
@Test
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() {
val expectValue = 10
val krate = StoreMutableKrate(factory = { expectValue })
assertEquals(expectValue, krate.cachedValue)
assertEquals(expectValue, krate.loadAndGet())
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() = runTest {
val factoryValue = 10
val store = SampleStore()
val krate = DefaultMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { null }
)
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
assertEquals(factoryValue, krate.cachedValue)
}

@Test
fun GIVEN_10_as_loader_value_and_default_null_WHEN_load_THEN_return_loader() {
val defaultValue = 10
val krate = StoreMutableKrate(factory = { defaultValue })
assertEquals(defaultValue, krate.cachedValue)
assertEquals(defaultValue, krate.loadAndGet())
fun GIVEN_null_as_default_10_as_loader_WHEN_load_THEN_return_loader() = runTest {
val loaderValue = 10
val store = SampleStore()
val krate = DefaultMutableKrate(
factory = { null },
saver = { store.put("KEY", it) },
loader = { loaderValue }
)
assertEquals(loaderValue, krate.cachedValue)
assertEquals(loaderValue, krate.loadAndGet())
assertEquals(loaderValue, krate.cachedValue)
}

@Test
fun GIVEN_saved_and_resed_WHEN_save_and_reset_THEN_saved_and_reset() {
val defaultValue = 10
val krate = StoreMutableKrate(factory = { defaultValue })
fun GIVEN_one_as_default_another_as_loader_WHEN_load_THEN_return_loader() = runTest {
val loaderValue = 10
val factoryValue = 15
val store = SampleStore()
val krate = DefaultMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { loaderValue }
)
assertEquals(loaderValue, krate.cachedValue)
assertEquals(loaderValue, krate.loadAndGet())
assertEquals(loaderValue, krate.cachedValue)
}

@Test
fun GIVEN_empty_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest {
val factoryValue = 10
val store = SampleStore()
val krate = DefaultMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { store.get("KEY") }
)
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
11.let { newValue ->
krate.save(newValue)
assertEquals(newValue, krate.cachedValue)
assertEquals(newValue, krate.loadAndGet())
}
krate.reset()
assertEquals(defaultValue, krate.cachedValue)
assertEquals(defaultValue, krate.loadAndGet())
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
}

@Test
fun GIVEN_null_then_with_default_WHEN_get_THEN_default() {
val defaultValue = 10
val krate = StoreMutableKrate<Int?>(factory = { null })
.withDefault(factory = { defaultValue })
assertEquals(defaultValue, krate.cachedValue)
assertEquals(defaultValue, krate.loadAndGet())
fun GIVEN_prefilled_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest {
val factoryValue = 10
val defaultStoreValue = 15
val store = SampleStore(mapOf("KEY" to defaultStoreValue))
val krate = DefaultMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { store.get("KEY") }
)
assertEquals(defaultStoreValue, krate.cachedValue)
assertEquals(defaultStoreValue, krate.loadAndGet())
11.let { newValue ->
krate.save(newValue)
assertEquals(newValue, krate.cachedValue)
assertEquals(newValue, krate.loadAndGet())
}
store.put("KEY", null)
krate.reset()
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,103 @@
package ru.astrainteractive.klibs.kstorage

import kotlinx.coroutines.test.runTest
import ru.astrainteractive.klibs.kstorage.api.impl.DefaultStateFlowMutableKrate
import ru.astrainteractive.klibs.kstorage.test.SampleStore
import ru.astrainteractive.klibs.kstorage.test.StoreStateFlowMutableKrate
import ru.astrainteractive.klibs.kstorage.util.KrateDefaultExt.withDefault
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Test cases:
* 1. Factory value not null, loader is null.
* 2. Factory value is null, loader not null
* 3. Factory value is one, the loader is another
* 4. Factory value is null,m the loader is null
*/
internal class StateFlowMutableKrateTest {

@Test
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() {
val expectValue = 10
val krate = StoreStateFlowMutableKrate(
factory = { expectValue }
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() = runTest {
val factoryValue = 10
val store = SampleStore()
val krate = DefaultStateFlowMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { null }
)
assertEquals(expectValue, krate.cachedValue)
assertEquals(expectValue, krate.loadAndGet())
assertEquals(expectValue, krate.cachedStateFlow.value)
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
assertEquals(factoryValue, krate.cachedValue)
}

@Test
fun GIVEN_10_as_loader_value_and_default_null_WHEN_load_THEN_return_loader() {
val expectValue = 10
val krate = StoreStateFlowMutableKrate<Int?>(
fun GIVEN_null_as_default_10_as_loader_WHEN_load_THEN_return_loader() = runTest {
val loaderValue = 10
val store = SampleStore()
val krate = DefaultStateFlowMutableKrate(
factory = { null },
key = "key",
store = SampleStore(mapOf("key" to expectValue))
saver = { store.put("KEY", it) },
loader = { loaderValue }
)
assertEquals(loaderValue, krate.cachedValue)
assertEquals(loaderValue, krate.loadAndGet())
assertEquals(loaderValue, krate.cachedValue)
}

@Test
fun GIVEN_one_as_default_another_as_loader_WHEN_load_THEN_return_loader() = runTest {
val loaderValue = 10
val factoryValue = 15
val store = SampleStore()
val krate = DefaultStateFlowMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { loaderValue }
)
assertEquals(expectValue, krate.cachedValue)
assertEquals(expectValue, krate.loadAndGet())
assertEquals(expectValue, krate.cachedStateFlow.value)
assertEquals(loaderValue, krate.cachedValue)
assertEquals(loaderValue, krate.loadAndGet())
assertEquals(loaderValue, krate.cachedValue)
}

@Test
fun GIVEN_null_then_with_default_WHEN_get_THEN_default() {
val defaultValue = 10
val krate = StoreStateFlowMutableKrate<Int?>(factory = { null })
.withDefault(factory = { defaultValue })
assertEquals(defaultValue, krate.cachedValue)
assertEquals(defaultValue, krate.loadAndGet())
assertEquals(defaultValue, krate.cachedStateFlow.value)
fun GIVEN_empty_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest {
val factoryValue = 10
val store = SampleStore()
val krate = DefaultStateFlowMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { store.get("KEY") }
)
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
11.let { newValue ->
krate.save(newValue)
assertEquals(newValue, krate.cachedValue)
assertEquals(newValue, krate.loadAndGet())
}
krate.reset()
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
}

@Test
fun GIVEN_prefilled_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest {
val factoryValue = 10
val defaultStoreValue = 15
val store = SampleStore(mapOf("KEY" to defaultStoreValue))
val krate = DefaultStateFlowMutableKrate(
factory = { factoryValue },
saver = { store.put("KEY", it) },
loader = { store.get("KEY") }
)
assertEquals(defaultStoreValue, krate.cachedValue)
assertEquals(defaultStoreValue, krate.loadAndGet())
11.let { newValue ->
krate.save(newValue)
assertEquals(newValue, krate.cachedValue)
assertEquals(newValue, krate.loadAndGet())
}
store.put("KEY", null)
krate.reset()
assertEquals(factoryValue, krate.cachedValue)
assertEquals(factoryValue, krate.loadAndGet())
}
}
Loading

0 comments on commit 12b061d

Please sign in to comment.