From 63e8b672c5a020db04cbd6baa01c36e99de4db71 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 2 Aug 2024 20:14:23 +0600 Subject: [PATCH] start add cache repos tests --- .../kotlin/full/FullCRUDCacheRepoTests.kt | 142 ++++++++++++++++++ .../kotlin/full/FullKeyValueCacheRepoTests.kt | 91 +++++++++++ 2 files changed, 233 insertions(+) create mode 100644 repos/cache/src/commonTest/kotlin/full/FullCRUDCacheRepoTests.kt create mode 100644 repos/cache/src/commonTest/kotlin/full/FullKeyValueCacheRepoTests.kt diff --git a/repos/cache/src/commonTest/kotlin/full/FullCRUDCacheRepoTests.kt b/repos/cache/src/commonTest/kotlin/full/FullCRUDCacheRepoTests.kt new file mode 100644 index 00000000000..b76b6f77481 --- /dev/null +++ b/repos/cache/src/commonTest/kotlin/full/FullCRUDCacheRepoTests.kt @@ -0,0 +1,142 @@ +package full + +import com.benasher44.uuid.uuid4 +import dev.inmo.micro_utils.repos.MapCRUDRepo +import dev.inmo.micro_utils.repos.MapKeyValueRepo +import dev.inmo.micro_utils.repos.cache.full.FullCRUDCacheRepo +import dev.inmo.micro_utils.repos.create +import dev.inmo.micro_utils.repos.deleteById +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class FullCRUDCacheRepoTests { + data class New( + val data: String + ) + data class Registered( + val id: String, + val data: String + ) + @Test + fun creatingWorksProperly() = runTest { + val testData = (0 until 1000).map { + (it.toString() + uuid4().toString()) + } + val updatedTestData = (0 until 1000).map { + (it.toString() + uuid4().toString()) + } + val kvCache = MapKeyValueRepo() + val crudRepo = MapCRUDRepo( + { new, id, old -> + Registered(id, new.data) + } + ) { + val id = uuid4().toString() + id to Registered(id, it.data) + } + + val cacheRepo = FullCRUDCacheRepo( + crudRepo, + kvCache, + idGetter = { it.id } + ) + + val registereds = testData.map { + val created = cacheRepo.create(New(it)).first() + assertEquals(it, created.data) + assertEquals(kvCache.get(created.id), created) + assertEquals(crudRepo.getById(created.id), created) + created + } + + cacheRepo.getAll().forEach { (id, value) -> + assertTrue { + registereds.first { + it.id == id + } == value + } + } + val updatedRegistereds = registereds.mapIndexed { i, it -> + val updated = cacheRepo.update(it.id, New(updatedTestData[i])) ?: error("Unable to update data for $it") + assertEquals(updatedTestData[i], updated.data) + assertEquals(kvCache.get(updated.id), updated) + assertEquals(crudRepo.getById(updated.id), updated) + updated + } + cacheRepo.getAll().forEach { (id, value) -> + assertTrue { + updatedRegistereds.first { + it.id == id + } == value + } + } + } + @Test + fun precachingWorksProperly() = runTest { + val testData = (0 until 1000).map { + (it.toString() + uuid4().toString()) + } + val kvCache = MapKeyValueRepo() + val crudRepo = MapCRUDRepo( + { new, id, old -> + Registered(id, new.data) + } + ) { + val id = uuid4().toString() + id to Registered(id, it.data) + } + val registereds = crudRepo.create(testData.map { New(it) }) + + val cacheRepo = FullCRUDCacheRepo( + crudRepo, + kvCache, + idGetter = { it.id } + ) + + cacheRepo.getAll().forEach { (id, value) -> + assertTrue { + registereds.first { + it.id == id + } == value + } + } + } + @Test + fun removingWorksProperly() = runTest { + val testData = (0 until 1000).map { + (it.toString() + uuid4().toString()) + } + val kvCache = MapKeyValueRepo() + val crudRepo = MapCRUDRepo( + { new, id, old -> + Registered(id, new.data) + } + ) { + val id = uuid4().toString() + id to Registered(id, it.data) + } + val registereds = crudRepo.create(testData.map { New(it) }) + + val cacheRepo = FullCRUDCacheRepo( + crudRepo, + kvCache, + idGetter = { it.id } + ) + + registereds.forEach { + val id = it.id + assertTrue { + cacheRepo.getAll()[id] == it + } + + cacheRepo.deleteById(id) + assertFalse { + cacheRepo.contains(id) + } + } + assertEquals(0, cacheRepo.count()) + } +} diff --git a/repos/cache/src/commonTest/kotlin/full/FullKeyValueCacheRepoTests.kt b/repos/cache/src/commonTest/kotlin/full/FullKeyValueCacheRepoTests.kt new file mode 100644 index 00000000000..8a541608811 --- /dev/null +++ b/repos/cache/src/commonTest/kotlin/full/FullKeyValueCacheRepoTests.kt @@ -0,0 +1,91 @@ +package full + +import com.benasher44.uuid.uuid4 +import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class FullKeyValueCacheRepoTests { + @Test + fun creatingWorksProperly() = runTest { + val testData = (0 until 1000).associate { + (it.toString() + uuid4().toString()) to uuid4().toString() + } + val updatedTestData = testData.keys.associateWith { + uuid4().toString() + } + val kvCache = MapKeyValueRepo() + val kvRepo = MapKeyValueRepo() + + val cacheRepo = FullKeyValueCacheRepo( + kvRepo, + kvCache + ) + + 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() + val kvRepo = MapKeyValueRepo() + 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() + val kvRepo = MapKeyValueRepo() + 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()) + } +}