2024-08-02 14:14:23 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import com.benasher44.uuid.uuid4
|
|
|
|
import dev.inmo.micro_utils.repos.*
|
|
|
|
import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo
|
2024-08-07 10:30:43 +00:00
|
|
|
import korlibs.time.seconds
|
2024-08-02 14:14:23 +00:00
|
|
|
import kotlinx.coroutines.test.runTest
|
|
|
|
import kotlin.test.Test
|
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertFalse
|
|
|
|
import kotlin.test.assertTrue
|
|
|
|
|
|
|
|
class FullKeyValueCacheRepoTests {
|
|
|
|
@Test
|
2024-08-07 10:30:43 +00:00
|
|
|
fun creatingWorksProperly() = runTest(timeout = 120.seconds) {
|
2024-08-02 14:14:23 +00:00
|
|
|
val testData = (0 until 1000).associate {
|
2024-08-02 16:41:17 +00:00
|
|
|
("$it-" + uuid4().toString()) to "$it-" + uuid4().toString()
|
2024-08-02 14:14:23 +00:00
|
|
|
}
|
|
|
|
val updatedTestData = testData.keys.associateWith {
|
2024-08-02 16:41:17 +00:00
|
|
|
"$it-" + uuid4().toString()
|
2024-08-02 14:14:23 +00:00
|
|
|
}
|
|
|
|
val kvCache = MapKeyValueRepo<String, String>()
|
|
|
|
val kvRepo = MapKeyValueRepo<String, String>()
|
|
|
|
|
|
|
|
val cacheRepo = FullKeyValueCacheRepo(
|
|
|
|
kvRepo,
|
2024-08-07 10:30:43 +00:00
|
|
|
kvCache
|
2024-08-02 14:14:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
testData.forEach {
|
|
|
|
cacheRepo.set(it.key, it.value)
|
|
|
|
assertEquals(it.value, cacheRepo.get(it.key))
|
|
|
|
assertEquals(it.value, kvRepo.get(it.key))
|
|
|
|
assertEquals(it.value, kvCache.get(it.key))
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedTestData.forEach {
|
|
|
|
cacheRepo.set(it.key, it.value)
|
|
|
|
assertEquals(cacheRepo.get(it.key), it.value)
|
|
|
|
assertEquals(kvRepo.get(it.key), it.value)
|
|
|
|
assertEquals(kvCache.get(it.key), it.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun precachingWorksProperly() = runTest {
|
|
|
|
val testData = (0 until 1000).associate {
|
|
|
|
(it.toString() + uuid4().toString()) to uuid4().toString()
|
|
|
|
}
|
|
|
|
val kvCache = MapKeyValueRepo<String, String>()
|
|
|
|
val kvRepo = MapKeyValueRepo<String, String>()
|
|
|
|
kvRepo.set(testData)
|
|
|
|
|
|
|
|
val cacheRepo = FullKeyValueCacheRepo(
|
|
|
|
kvRepo,
|
|
|
|
kvCache
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
cacheRepo.getAll().forEach { (id, value) ->
|
|
|
|
assertTrue {
|
|
|
|
testData[id] == value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun unsettingWorksProperly() = runTest {
|
|
|
|
val testData = (0 until 1000).associate {
|
|
|
|
(it.toString() + uuid4().toString()) to uuid4().toString()
|
|
|
|
}
|
|
|
|
val kvCache = MapKeyValueRepo<String, String>()
|
|
|
|
val kvRepo = MapKeyValueRepo<String, String>()
|
|
|
|
kvRepo.set(testData)
|
|
|
|
|
|
|
|
val cacheRepo = FullKeyValueCacheRepo(
|
|
|
|
kvRepo,
|
|
|
|
kvCache
|
|
|
|
)
|
|
|
|
|
|
|
|
testData.forEach {
|
|
|
|
val id = it.key
|
|
|
|
assertTrue {
|
|
|
|
cacheRepo.getAll()[id] == it.value
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheRepo.unset(id)
|
|
|
|
assertFalse {
|
|
|
|
cacheRepo.contains(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(0, cacheRepo.count())
|
|
|
|
}
|
|
|
|
}
|