From b304283b4de18a7d35f0fc083d777ab294d9806c Mon Sep 17 00:00:00 2001 From: makeevrserg Date: Wed, 26 Jun 2024 13:11:35 +0300 Subject: [PATCH 1/2] fix tests --- kstorage/build.gradle.kts | 2 +- .../kstorage/api/impl/DefaultMutableKrate.kt | 1 - .../suspend/impl/DefaultFlowMutableKrate.kt | 1 - .../impl/DefaultSuspendMutableKrate.kt | 1 - .../klibs/kstorage/MutableKrateTest.kt | 102 ++++++++++++---- .../kstorage/StateFlowMutableKrateTest.kt | 109 ++++++++++++++---- .../klibs/kstorage/SuspendMutableKrateTest.kt | 104 +++++++++++++++++ .../klibs/kstorage/test/StoreMutableKrate.kt | 15 --- .../test/StoreStateFlowMutableKrate.kt | 15 --- .../kstorage/suspend/FlowMutableKrateTest.kt | 72 ++++++++++-- .../suspend/SuspendMutableKrateTest.kt | 24 ---- .../suspend/test/StoreSuspendMutableKrate.kt | 16 --- 12 files changed, 330 insertions(+), 132 deletions(-) create mode 100644 kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/SuspendMutableKrateTest.kt delete mode 100644 kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreMutableKrate.kt delete mode 100644 kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreStateFlowMutableKrate.kt delete mode 100644 kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/SuspendMutableKrateTest.kt delete mode 100644 kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/test/StoreSuspendMutableKrate.kt diff --git a/kstorage/build.gradle.kts b/kstorage/build.gradle.kts index 9d3a06c..5fc367f 100644 --- a/kstorage/build.gradle.kts +++ b/kstorage/build.gradle.kts @@ -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) } diff --git a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt index 14ac190..7501337 100644 --- a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt +++ b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt @@ -17,7 +17,6 @@ class DefaultMutableKrate( ) : Krate.Mutable { private var _cachedValue: T = loader.loadAndGet() ?: factory.create() - override val cachedValue: T get() = _cachedValue diff --git a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultFlowMutableKrate.kt b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultFlowMutableKrate.kt index acd1a9f..007a163 100644 --- a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultFlowMutableKrate.kt +++ b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultFlowMutableKrate.kt @@ -43,7 +43,6 @@ class DefaultFlowMutableKrate( } override suspend fun save(value: T) { - if (saver is SuspendValueSaver.Empty) return saver.save(value) _cachedValue = value } diff --git a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt index 8bc55e7..a22119e 100644 --- a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt +++ b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt @@ -23,7 +23,6 @@ class DefaultSuspendMutableKrate( } override suspend fun save(value: T) { - if (saver is SuspendValueSaver.Empty) return saver.save(value) _cachedStateFlow.value = value } diff --git a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/MutableKrateTest.kt b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/MutableKrateTest.kt index 3ba355d..ecaf2c2 100644 --- a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/MutableKrateTest.kt +++ b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/MutableKrateTest.kt @@ -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(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()) } } diff --git a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/StateFlowMutableKrateTest.kt b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/StateFlowMutableKrateTest.kt index 66fef98..7e7fcda 100644 --- a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/StateFlowMutableKrateTest.kt +++ b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/StateFlowMutableKrateTest.kt @@ -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( + 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(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()) } } diff --git a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/SuspendMutableKrateTest.kt b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/SuspendMutableKrateTest.kt new file mode 100644 index 0000000..e06cbe7 --- /dev/null +++ b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/SuspendMutableKrateTest.kt @@ -0,0 +1,104 @@ +package ru.astrainteractive.klibs.kstorage + +import kotlinx.coroutines.test.runTest +import ru.astrainteractive.klibs.kstorage.suspend.impl.DefaultSuspendMutableKrate +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 SuspendMutableKrateTest { + @Test + fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() = runTest { + val factoryValue = 10 + val store = SampleStore() + val krate = DefaultSuspendMutableKrate( + factory = { factoryValue }, + saver = { store.put("KEY", it) }, + loader = { null } + ) + assertEquals(factoryValue, krate.cachedValue) + assertEquals(factoryValue, krate.loadAndGet()) + assertEquals(factoryValue, krate.cachedValue) + } + + @Test + fun GIVEN_null_as_default_10_as_loader_WHEN_load_THEN_return_loader() = runTest { + val loaderValue = 10 + val store = SampleStore() + val krate = DefaultSuspendMutableKrate( + factory = { null }, + saver = { store.put("KEY", it) }, + loader = { loaderValue } + ) + assertEquals(null, 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 = DefaultSuspendMutableKrate( + factory = { factoryValue }, + saver = { store.put("KEY", it) }, + loader = { loaderValue } + ) + assertEquals(factoryValue, 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 = DefaultSuspendMutableKrate( + 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 = DefaultSuspendMutableKrate( + factory = { factoryValue }, + saver = { store.put("KEY", it) }, + loader = { store.get("KEY") } + ) + assertEquals(factoryValue, krate.cachedValue) + assertEquals(defaultStoreValue, krate.loadAndGet()) + assertEquals(defaultStoreValue, krate.cachedValue) + 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()) + } +} diff --git a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreMutableKrate.kt b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreMutableKrate.kt deleted file mode 100644 index 80c09d8..0000000 --- a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreMutableKrate.kt +++ /dev/null @@ -1,15 +0,0 @@ -package ru.astrainteractive.klibs.kstorage.test - -import ru.astrainteractive.klibs.kstorage.api.Krate -import ru.astrainteractive.klibs.kstorage.api.impl.DefaultMutableKrate -import ru.astrainteractive.klibs.kstorage.api.provider.ValueFactory - -internal class StoreMutableKrate( - factory: ValueFactory, - key: String = "key", - store: SampleStore = SampleStore() -) : Krate.Mutable by DefaultMutableKrate( - factory = factory, - saver = { store.put(key, it) }, - loader = { store.get(key) } -) diff --git a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreStateFlowMutableKrate.kt b/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreStateFlowMutableKrate.kt deleted file mode 100644 index 76c66e7..0000000 --- a/kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/test/StoreStateFlowMutableKrate.kt +++ /dev/null @@ -1,15 +0,0 @@ -package ru.astrainteractive.klibs.kstorage.test - -import ru.astrainteractive.klibs.kstorage.api.StateFlowKrate -import ru.astrainteractive.klibs.kstorage.api.impl.DefaultStateFlowMutableKrate -import ru.astrainteractive.klibs.kstorage.api.provider.ValueFactory - -internal class StoreStateFlowMutableKrate( - factory: ValueFactory, - key: String = "key", - store: SampleStore = SampleStore() -) : StateFlowKrate.Mutable by DefaultStateFlowMutableKrate( - factory = factory, - saver = { store.put(key, it) }, - loader = { store.get(key) } -) diff --git a/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/FlowMutableKrateTest.kt b/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/FlowMutableKrateTest.kt index 97ce435..e653dd9 100644 --- a/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/FlowMutableKrateTest.kt +++ b/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/FlowMutableKrateTest.kt @@ -1,27 +1,79 @@ package ru.astrainteractive.klibs.kstorage.suspend import androidx.datastore.preferences.core.intPreferencesKey -import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest import ru.astrainteractive.klibs.kstorage.suspend.test.DataStoreFlowMutableKrate 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 FlowMutableKrateTest { @Test - fun test(): Unit = runTest { - val initialValue = 10 + fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() = runTest { + val factoryValue = 10 val krate = DataStoreFlowMutableKrate( - key = intPreferencesKey("some_int_key"), - factory = { initialValue } + factory = { factoryValue }, + key = intPreferencesKey("KEY_1") ) - assertEquals(initialValue, krate.flow.first()) - assertEquals(initialValue, krate.loadAndGet()) - 15.let { newValue -> + assertEquals(factoryValue, krate.cachedValue) + assertEquals(factoryValue, krate.loadAndGet()) + assertEquals(factoryValue, krate.cachedValue) + } + + @Test + fun GIVEN_one_as_default_another_as_loader_WHEN_load_THEN_return_loader() = runTest { + val factoryValue = 15 + val krate = DataStoreFlowMutableKrate( + factory = { factoryValue }, + key = intPreferencesKey("KEY_2") + ) + assertEquals(factoryValue, krate.cachedValue) + assertEquals(factoryValue, krate.loadAndGet()) + assertEquals(factoryValue, krate.cachedValue) + } + + @Test + fun GIVEN_empty_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest { + val factoryValue = 10 + val krate = DataStoreFlowMutableKrate( + factory = { factoryValue }, + key = intPreferencesKey("KEY_3") + ) + 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 krate = DataStoreFlowMutableKrate( + factory = { factoryValue }, + key = intPreferencesKey("KEY_4") + ) + assertEquals(factoryValue, krate.cachedValue) + assertEquals(factoryValue, krate.loadAndGet()) + assertEquals(factoryValue, krate.cachedValue) + 11.let { newValue -> krate.save(newValue) - assertEquals(newValue, krate.flow.first()) + assertEquals(newValue, krate.cachedValue) + assertEquals(newValue, krate.loadAndGet()) } krate.reset() - assertEquals(initialValue, krate.flow.first()) + assertEquals(factoryValue, krate.cachedValue) + assertEquals(factoryValue, krate.loadAndGet()) } } diff --git a/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/SuspendMutableKrateTest.kt b/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/SuspendMutableKrateTest.kt deleted file mode 100644 index 8c60978..0000000 --- a/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/SuspendMutableKrateTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package ru.astrainteractive.klibs.kstorage.suspend - -import kotlinx.coroutines.test.runTest -import ru.astrainteractive.klibs.kstorage.suspend.test.StoreSuspendMutableKrate -import kotlin.test.Test -import kotlin.test.assertEquals - -internal class SuspendMutableKrateTest { - @Test - fun test(): Unit = runTest { - val initialValue = 10 - val krate = StoreSuspendMutableKrate( - key = "some_int_key", - factory = { initialValue } - ) - assertEquals(initialValue, krate.loadAndGet()) - 15.let { newValue -> - krate.save(newValue) - assertEquals(newValue, krate.loadAndGet()) - } - krate.reset() - assertEquals(initialValue, krate.loadAndGet()) - } -} diff --git a/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/test/StoreSuspendMutableKrate.kt b/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/test/StoreSuspendMutableKrate.kt deleted file mode 100644 index d50899b..0000000 --- a/kstorage/src/jvmTest/kotlin/ru/astrainteractive/klibs/kstorage/suspend/test/StoreSuspendMutableKrate.kt +++ /dev/null @@ -1,16 +0,0 @@ -package ru.astrainteractive.klibs.kstorage.suspend.test - -import ru.astrainteractive.klibs.kstorage.api.provider.ValueFactory -import ru.astrainteractive.klibs.kstorage.suspend.SuspendKrate -import ru.astrainteractive.klibs.kstorage.suspend.impl.DefaultSuspendMutableKrate -import ru.astrainteractive.klibs.kstorage.test.SampleStore - -internal class StoreSuspendMutableKrate( - factory: ValueFactory, - key: String = "key", - store: SampleStore = SampleStore() -) : SuspendKrate.Mutable by DefaultSuspendMutableKrate( - factory = factory, - saver = { store.put(key, it) }, - loader = { store.get(key) } -) From fba7d09dd36fb87cc38c6482fa3ef6134a995cb0 Mon Sep 17 00:00:00 2001 From: makeevrserg Date: Wed, 26 Jun 2024 14:26:43 +0300 Subject: [PATCH 2/2] fix state flow update --- gradle.properties | 2 +- .../klibs/kstorage/api/impl/DefaultMutableKrate.kt | 1 - .../klibs/kstorage/api/impl/DefaultStateFlowMutableKrate.kt | 6 +++--- .../klibs/kstorage/suspend/StateFlowSuspendKrate.kt | 6 +++++- .../kstorage/suspend/impl/DefaultSuspendMutableKrate.kt | 5 +++-- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/gradle.properties b/gradle.properties index 2325cff..f450b23 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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|makeevrserg@gmail.com makeevrserg.project.url=https://github.com/makeevrserg/klibs.kstorage/ diff --git a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt index 7501337..f2c3e37 100644 --- a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt +++ b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultMutableKrate.kt @@ -27,7 +27,6 @@ class DefaultMutableKrate( } override fun save(value: T) { - if (saver is ValueSaver.Empty) return saver.save(value) _cachedValue = value } diff --git a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultStateFlowMutableKrate.kt b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultStateFlowMutableKrate.kt index 788dd5e..6a70673 100644 --- a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultStateFlowMutableKrate.kt +++ b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/api/impl/DefaultStateFlowMutableKrate.kt @@ -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 @@ -24,13 +25,12 @@ class DefaultStateFlowMutableKrate( override val cachedStateFlow: StateFlow = _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 } diff --git a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/StateFlowSuspendKrate.kt b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/StateFlowSuspendKrate.kt index c8bf3ac..7002a6c 100644 --- a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/StateFlowSuspendKrate.kt +++ b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/StateFlowSuspendKrate.kt @@ -8,5 +8,9 @@ interface StateFlowSuspendKrate : SuspendKrate, CachedKrate.Coroutine { override val cachedValue: T get() = cachedStateFlow.value - interface Mutable : StateFlowSuspendKrate, SuspendKrate.Mutable + interface Mutable : StateFlowSuspendKrate, SuspendKrate.Mutable { + override val cachedStateFlow: StateFlow + override val cachedValue: T + get() = cachedStateFlow.value + } } diff --git a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt index a22119e..e69db1e 100644 --- a/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt +++ b/kstorage/src/commonMain/kotlin/ru/astrainteractive/klibs/kstorage/suspend/impl/DefaultSuspendMutableKrate.kt @@ -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 @@ -18,13 +19,13 @@ class DefaultSuspendMutableKrate( 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) { saver.save(value) - _cachedStateFlow.value = value + _cachedStateFlow.update { value } } override suspend fun reset() {