mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-15 05:19:23 +00:00
kv rework, fixes in map keyvalue repo, tests
This commit is contained in:
@@ -13,5 +13,22 @@ kotlin {
|
||||
api internalProject("micro_utils.repos.common")
|
||||
}
|
||||
}
|
||||
jvmTest {
|
||||
dependencies {
|
||||
implementation internalProject("micro_utils.repos.common")
|
||||
implementation internalProject("micro_utils.repos.ktor.client")
|
||||
implementation internalProject("micro_utils.repos.ktor.server")
|
||||
implementation internalProject("micro_utils.repos.inmemory")
|
||||
implementation libs.kt.coroutines.test
|
||||
|
||||
implementation libs.ktor.server.cio
|
||||
implementation libs.ktor.client.cio
|
||||
implementation libs.ktor.server.content.negotiation
|
||||
implementation libs.ktor.serialization.kotlinx.json
|
||||
implementation libs.ktor.client.content.negotiation
|
||||
implementation libs.ktor.client.logging
|
||||
implementation libs.ktor.client.websockets
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.common
|
||||
|
||||
const val idParameterName = "id"
|
||||
const val keyParameterName = "key"
|
||||
const val valueParameterName = "value"
|
||||
const val reversedParameterName = "reversed"
|
||||
const val reversedParameterName = "reversed"
|
||||
|
89
repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt
Normal file
89
repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt
Normal file
@@ -0,0 +1,89 @@
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.ktor.client.crud.KtorStandardCrudRepoClient
|
||||
import dev.inmo.micro_utils.repos.ktor.server.crud.configureStandardCrudRepoRoutes
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.plugins.logging.Logging
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.serialization.kotlinx.KotlinxWebsocketSerializationConverter
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import io.ktor.server.application.install
|
||||
import io.ktor.server.cio.CIO
|
||||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.server.routing.routing
|
||||
import io.ktor.server.websocket.WebSockets
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class CRUDTests {
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@Test
|
||||
fun testCRUDFunctions() {
|
||||
runTest() {
|
||||
val map = mutableMapOf<Int, ComplexData>()
|
||||
val repo = MapCRUDRepo<ComplexData, Int, SimpleData>(
|
||||
map,
|
||||
{ newValue, id, oldValue ->
|
||||
oldValue.copy(title = newValue.title)
|
||||
}
|
||||
) {
|
||||
size to ComplexData(size, title = it.title)
|
||||
}
|
||||
val server = io.ktor.server.engine.embeddedServer(
|
||||
CIO,
|
||||
23456,
|
||||
"127.0.0.1"
|
||||
) {
|
||||
install(ContentNegotiation) {
|
||||
json()
|
||||
}
|
||||
install(WebSockets) {
|
||||
contentConverter = KotlinxWebsocketSerializationConverter(Json)
|
||||
}
|
||||
routing {
|
||||
configureStandardCrudRepoRoutes(
|
||||
repo
|
||||
) {
|
||||
it.toInt()
|
||||
}
|
||||
}
|
||||
}.start(false)
|
||||
val client = HttpClient {
|
||||
install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) {
|
||||
json()
|
||||
}
|
||||
install(Logging)
|
||||
install(io.ktor.client.plugins.websocket.WebSockets) {
|
||||
contentConverter = KotlinxWebsocketSerializationConverter(Json)
|
||||
}
|
||||
}
|
||||
val crudClient = KtorStandardCrudRepoClient<ComplexData, Int, SimpleData>(
|
||||
"http://127.0.0.1:23456",
|
||||
client,
|
||||
ContentType.Application.Json
|
||||
) {
|
||||
it.toString()
|
||||
}
|
||||
|
||||
val created = crudClient.create(SimpleData("Example")).single()
|
||||
assertEquals(map.size, 1)
|
||||
assertEquals(map.size.toLong(), crudClient.count())
|
||||
assertEquals(1, crudClient.count())
|
||||
assertEquals(map.getValue(map.keys.first()), created)
|
||||
|
||||
val updated = crudClient.update(created.id, SimpleData("Example2"))
|
||||
assertEquals(map.size, 1)
|
||||
assertEquals(map.size.toLong(), crudClient.count())
|
||||
assertEquals(1, crudClient.count())
|
||||
assertEquals(map.getValue(map.keys.first()), updated)
|
||||
|
||||
crudClient.deleteById(created.id)
|
||||
assertEquals(map.size, 0)
|
||||
assertEquals(map.size.toLong(), crudClient.count())
|
||||
assertEquals(0, crudClient.count())
|
||||
server.stop()
|
||||
}
|
||||
}
|
||||
}
|
10
repos/ktor/common/src/jvmTest/kotlin/ComplexData.kt
Normal file
10
repos/ktor/common/src/jvmTest/kotlin/ComplexData.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
import com.benasher44.uuid.uuid4
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ComplexData(
|
||||
val id: Int,
|
||||
val simple: SimpleData = SimpleData(),
|
||||
val simples: List<SimpleData> = (0 until 100).map { SimpleData(("$it")) },
|
||||
val title: String = uuid4().toString()
|
||||
)
|
139
repos/ktor/common/src/jvmTest/kotlin/KVTests.kt
Normal file
139
repos/ktor/common/src/jvmTest/kotlin/KVTests.kt
Normal file
@@ -0,0 +1,139 @@
|
||||
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
|
||||
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorStandardKeyValueRepoClient
|
||||
import dev.inmo.micro_utils.repos.ktor.server.key_value.configureStandardKeyValueRepoRoutes
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.plugins.logging.Logging
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.serialization.kotlinx.KotlinxWebsocketSerializationConverter
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import io.ktor.server.application.install
|
||||
import io.ktor.server.cio.CIO
|
||||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.server.routing.routing
|
||||
import io.ktor.server.websocket.WebSockets
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlin.test.*
|
||||
|
||||
class KVTests {
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@Test
|
||||
fun testCRUDFunctions() {
|
||||
runTest() {
|
||||
val map = mutableMapOf<Int, ComplexData>()
|
||||
val repo = MapKeyValueRepo<Int, ComplexData>(map)
|
||||
val server = io.ktor.server.engine.embeddedServer(
|
||||
CIO,
|
||||
23456,
|
||||
"127.0.0.1"
|
||||
) {
|
||||
install(ContentNegotiation) {
|
||||
json()
|
||||
}
|
||||
install(WebSockets) {
|
||||
contentConverter = KotlinxWebsocketSerializationConverter(Json)
|
||||
}
|
||||
routing {
|
||||
configureStandardKeyValueRepoRoutes(
|
||||
repo,
|
||||
Int.serializer(),
|
||||
ComplexData.serializer(),
|
||||
Json {}
|
||||
)
|
||||
}
|
||||
}.start(false)
|
||||
val client = HttpClient {
|
||||
install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) {
|
||||
json()
|
||||
}
|
||||
install(Logging)
|
||||
install(io.ktor.client.plugins.websocket.WebSockets) {
|
||||
contentConverter = KotlinxWebsocketSerializationConverter(Json)
|
||||
}
|
||||
}
|
||||
val crudClient = KtorStandardKeyValueRepoClient<Int, ComplexData>(
|
||||
"http://127.0.0.1:23456",
|
||||
client,
|
||||
ContentType.Application.Json,
|
||||
Int.serializer(),
|
||||
ComplexData.serializer(),
|
||||
Json
|
||||
)
|
||||
|
||||
val dataInOneKey = ComplexData(1, title = "Example1")
|
||||
val dataInMultipleKeys = ComplexData(2, title = "Example2")
|
||||
val repeatCount = 3
|
||||
|
||||
val dataList = listOf(
|
||||
1 to dataInOneKey
|
||||
) + (0 until repeatCount).map {
|
||||
(it + 2) to dataInMultipleKeys
|
||||
}
|
||||
|
||||
dataList.forEachIndexed { i, (id, data) ->
|
||||
crudClient.set(id, data)
|
||||
assertEquals(map.size, i + 1)
|
||||
assertEquals(map.size.toLong(), crudClient.count())
|
||||
assertEquals(i + 1L, crudClient.count())
|
||||
dataList.take(i + 1).forEach { (id, data) ->
|
||||
assertEquals(data, map[id])
|
||||
assertEquals(data, crudClient.get(id))
|
||||
assertEquals(map[id], crudClient.get(id))
|
||||
}
|
||||
}
|
||||
|
||||
dataList.forEach { (id, data) ->
|
||||
assertTrue(crudClient.contains(id))
|
||||
assertEquals(data, crudClient.get(id))
|
||||
}
|
||||
|
||||
assertEquals(
|
||||
dataList.mapNotNull { if (it.second == dataInMultipleKeys) it.first else null },
|
||||
getAllWithNextPaging(firstPageWithOneElementPagination) {
|
||||
crudClient.keys(dataInMultipleKeys, it)
|
||||
}
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
dataList.mapNotNull { if (it.second == dataInOneKey) it.first else null },
|
||||
getAllWithNextPaging(firstPageWithOneElementPagination) {
|
||||
crudClient.keys(dataInOneKey, it)
|
||||
}
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
dataList.map { it.first },
|
||||
getAllWithNextPaging(firstPageWithOneElementPagination) {
|
||||
crudClient.keys(it)
|
||||
}
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
dataList.map { it.second },
|
||||
getAllWithNextPaging(firstPageWithOneElementPagination) {
|
||||
crudClient.values(it)
|
||||
}
|
||||
)
|
||||
|
||||
assertEquals(dataList.size.toLong(), crudClient.count())
|
||||
|
||||
crudClient.unsetWithValues(dataInMultipleKeys)
|
||||
assertEquals(
|
||||
dataList.filter { it.second == dataInOneKey }.size.toLong(),
|
||||
crudClient.count()
|
||||
)
|
||||
|
||||
crudClient.unset(dataList.first { it.second == dataInOneKey }.first)
|
||||
assertEquals(
|
||||
0,
|
||||
crudClient.count()
|
||||
)
|
||||
|
||||
server.stop()
|
||||
}
|
||||
}
|
||||
}
|
7
repos/ktor/common/src/jvmTest/kotlin/SimpleData.kt
Normal file
7
repos/ktor/common/src/jvmTest/kotlin/SimpleData.kt
Normal file
@@ -0,0 +1,7 @@
|
||||
import com.benasher44.uuid.uuid4
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SimpleData(
|
||||
val title: String = uuid4().toString()
|
||||
)
|
Reference in New Issue
Block a user