start add cache repos tests

This commit is contained in:
InsanusMokrassar 2024-08-02 20:14:23 +06:00
parent 5ed53d7f73
commit 63e8b672c5
2 changed files with 233 additions and 0 deletions

View File

@ -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<String, Registered>()
val crudRepo = MapCRUDRepo<Registered, String, New>(
{ 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<String, Registered>()
val crudRepo = MapCRUDRepo<Registered, String, New>(
{ 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<String, Registered>()
val crudRepo = MapCRUDRepo<Registered, String, New>(
{ 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())
}
}

View File

@ -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<String, String>()
val kvRepo = MapKeyValueRepo<String, String>()
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<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())
}
}