|
| 1 | +package net.thunderbird.core.configstore.testing |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.isEqualTo |
| 5 | +import assertk.assertions.isNull |
| 6 | +import kotlin.test.Test |
| 7 | +import kotlinx.coroutines.flow.first |
| 8 | +import kotlinx.coroutines.test.runTest |
| 9 | +import net.thunderbird.core.configstore.Config |
| 10 | +import net.thunderbird.core.configstore.ConfigKey |
| 11 | + |
| 12 | +class TestConfigBackendTest { |
| 13 | + |
| 14 | + private val booleanKey = ConfigKey.BooleanKey("boolean") |
| 15 | + private val intKey = ConfigKey.IntKey("int") |
| 16 | + private val stringKey = ConfigKey.StringKey("string") |
| 17 | + |
| 18 | + @Test |
| 19 | + fun `read should return initial config`() = runTest { |
| 20 | + val initialConfig = Config().apply { |
| 21 | + set(booleanKey, true) |
| 22 | + } |
| 23 | + val backend = TestConfigBackend(initialConfig) |
| 24 | + |
| 25 | + val config = backend.read(listOf(booleanKey)).first() |
| 26 | + |
| 27 | + assertThat(config[booleanKey]).isEqualTo(true) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `update should modify config`() = runTest { |
| 32 | + val backend = TestConfigBackend() |
| 33 | + |
| 34 | + backend.update(listOf(intKey)) { config -> |
| 35 | + val newConfig = config.copy() |
| 36 | + newConfig[intKey] = 42 |
| 37 | + newConfig |
| 38 | + } |
| 39 | + |
| 40 | + val config = backend.read(listOf(intKey)).first() |
| 41 | + assertThat(config[intKey]).isEqualTo(42) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `clear should reset config`() = runTest { |
| 46 | + val initialConfig = Config().apply { |
| 47 | + set(stringKey, "value") |
| 48 | + } |
| 49 | + val backend = TestConfigBackend(initialConfig) |
| 50 | + |
| 51 | + backend.clear() |
| 52 | + |
| 53 | + val config = backend.read(listOf(stringKey)).first() |
| 54 | + assertThat(config[stringKey]).isNull() |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `readVersion should return 0 by default`() = runTest { |
| 59 | + val backend = TestConfigBackend() |
| 60 | + |
| 61 | + val version = backend.readVersion("version_key") |
| 62 | + |
| 63 | + assertThat(version).isEqualTo(0) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `writeVersion should update version`() = runTest { |
| 68 | + val backend = TestConfigBackend() |
| 69 | + |
| 70 | + backend.writeVersion("version_key", 5) |
| 71 | + |
| 72 | + val version = backend.readVersion("version_key") |
| 73 | + assertThat(version).isEqualTo(5) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `removeKeys should remove specified keys`() = runTest { |
| 78 | + val initialConfig = Config().apply { |
| 79 | + set(booleanKey, true) |
| 80 | + set(intKey, 42) |
| 81 | + } |
| 82 | + val backend = TestConfigBackend(initialConfig) |
| 83 | + |
| 84 | + backend.removeKeys(setOf(booleanKey)) |
| 85 | + |
| 86 | + val config = backend.read(listOf(booleanKey, intKey)).first() |
| 87 | + assertThat(config[booleanKey]).isNull() |
| 88 | + assertThat(config[intKey]).isEqualTo(42) |
| 89 | + } |
| 90 | +} |
0 commit comments