mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-07 16:30:28 +00:00
improve tests of repos
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
package dev.inmo.micro_utils.repos.ktor
|
||||
|
||||
import com.benasher44.uuid.uuid4
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
|
@@ -1,4 +1,8 @@
|
||||
package dev.inmo.micro_utils.repos.ktor
|
||||
|
||||
import com.benasher44.uuid.uuid4
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.common.tests.CommonCRUDRepoTests
|
||||
import dev.inmo.micro_utils.repos.ktor.client.crud.KtorCRUDRepoClient
|
||||
import dev.inmo.micro_utils.repos.ktor.server.crud.configureCRUDRepoRoutes
|
||||
import io.ktor.client.HttpClient
|
||||
@@ -8,17 +12,53 @@ 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.engine.*
|
||||
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.TestResult
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class CRUDTests {
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class KtorCRUDRepoTests : CommonCRUDRepoTests() {
|
||||
private var engine: ApplicationEngine? = null
|
||||
|
||||
@BeforeTest
|
||||
fun beforeTest() {
|
||||
engine = KtorRepoTestsHelper.beforeTest {
|
||||
configureCRUDRepoRoutes(
|
||||
MapCRUDRepo<Registered, String, New>(
|
||||
{ new, id, old ->
|
||||
Registered(id, new.data)
|
||||
}
|
||||
) {
|
||||
val id = uuid4().toString()
|
||||
id to Registered(id, it.data)
|
||||
}
|
||||
) {
|
||||
it
|
||||
}
|
||||
}
|
||||
}
|
||||
@AfterTest
|
||||
fun afterTest() {
|
||||
engine ?.let(KtorRepoTestsHelper::afterTest)
|
||||
}
|
||||
|
||||
override val repoCreator: suspend () -> CRUDRepo<Registered, String, New> = {
|
||||
KtorCRUDRepoClient<Registered, String, New>(
|
||||
"http://127.0.0.1:23456",
|
||||
KtorRepoTestsHelper.client(),
|
||||
ContentType.Application.Json
|
||||
) {
|
||||
it
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCRUDFunctions() {
|
||||
runTest {
|
||||
@@ -33,7 +73,7 @@ class CRUDTests {
|
||||
}
|
||||
val server = io.ktor.server.engine.embeddedServer(
|
||||
CIO,
|
||||
23456,
|
||||
34567,
|
||||
"127.0.0.1"
|
||||
) {
|
||||
install(ContentNegotiation) {
|
||||
@@ -60,7 +100,7 @@ class CRUDTests {
|
||||
}
|
||||
}
|
||||
val crudClient = KtorCRUDRepoClient<ComplexData, Int, SimpleData>(
|
||||
"http://127.0.0.1:23456",
|
||||
"http://127.0.0.1:34567",
|
||||
client,
|
||||
ContentType.Application.Json
|
||||
) {
|
@@ -1,8 +1,13 @@
|
||||
package dev.inmo.micro_utils.repos.ktor
|
||||
|
||||
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.common.tests.CommonKeyValueRepoTests
|
||||
import dev.inmo.micro_utils.repos.ktor.client.key.value.KtorKeyValueRepoClient
|
||||
import dev.inmo.micro_utils.repos.ktor.client.key.values.KtorKeyValuesRepoClient
|
||||
import dev.inmo.micro_utils.repos.ktor.server.key.value.configureKeyValueRepoRoutes
|
||||
import dev.inmo.micro_utils.repos.ktor.server.key.values.configureKeyValuesRepoRoutes
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.plugins.logging.Logging
|
||||
import io.ktor.http.ContentType
|
||||
@@ -10,17 +15,47 @@ 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.engine.*
|
||||
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.TestResult
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlin.test.*
|
||||
|
||||
class KVTests {
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class KtorKeyValueRepoTests : CommonKeyValueRepoTests() {
|
||||
private var engine: ApplicationEngine? = null
|
||||
|
||||
override val repoCreator: suspend () -> KeyValueRepo<String, String> = {
|
||||
KtorKeyValueRepoClient(
|
||||
"http://127.0.0.1:23456",
|
||||
KtorRepoTestsHelper.client(),
|
||||
ContentType.Application.Json,
|
||||
String.serializer(),
|
||||
String.serializer(),
|
||||
Json
|
||||
)
|
||||
}
|
||||
|
||||
@BeforeTest
|
||||
fun beforeTest() {
|
||||
engine = KtorRepoTestsHelper.beforeTest {
|
||||
configureKeyValueRepoRoutes(
|
||||
MapKeyValueRepo(),
|
||||
String.serializer(),
|
||||
String.serializer(),
|
||||
Json
|
||||
)
|
||||
}
|
||||
}
|
||||
@AfterTest
|
||||
fun afterTest() {
|
||||
engine ?.let(KtorRepoTestsHelper::afterTest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testKVFunctions() {
|
||||
runTest {
|
||||
@@ -28,7 +63,7 @@ class KVTests {
|
||||
val repo = MapKeyValueRepo<Int, ComplexData>(map)
|
||||
val server = io.ktor.server.engine.embeddedServer(
|
||||
CIO,
|
||||
23456,
|
||||
34567,
|
||||
"127.0.0.1"
|
||||
) {
|
||||
install(ContentNegotiation) {
|
||||
@@ -56,7 +91,7 @@ class KVTests {
|
||||
}
|
||||
}
|
||||
val crudClient = KtorKeyValueRepoClient<Int, ComplexData>(
|
||||
"http://127.0.0.1:23456",
|
||||
"http://127.0.0.1:34567",
|
||||
client,
|
||||
ContentType.Application.Json,
|
||||
Int.serializer(),
|
@@ -1,26 +1,59 @@
|
||||
package dev.inmo.micro_utils.repos.ktor
|
||||
|
||||
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.common.tests.CommonKeyValuesRepoTests
|
||||
import dev.inmo.micro_utils.repos.ktor.client.key.values.KtorKeyValuesRepoClient
|
||||
import dev.inmo.micro_utils.repos.ktor.server.key.values.configureKeyValuesRepoRoutes
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.plugins.logging.Logging
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.plugins.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 io.ktor.serialization.kotlinx.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.cio.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.plugins.contentnegotiation.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.websocket.*
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlin.test.*
|
||||
|
||||
class KVsTests {
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class KtorKeyValuesRepoTests : CommonKeyValuesRepoTests() {
|
||||
private var engine: ApplicationEngine? = null
|
||||
override val testSequencesSize: Int
|
||||
get() = 100
|
||||
|
||||
override val repoCreator: suspend () -> KeyValuesRepo<String, String> = {
|
||||
KtorKeyValuesRepoClient(
|
||||
"http://127.0.0.1:23456",
|
||||
KtorRepoTestsHelper.client(),
|
||||
ContentType.Application.Json,
|
||||
String.serializer(),
|
||||
String.serializer(),
|
||||
Json
|
||||
)
|
||||
}
|
||||
|
||||
@BeforeTest
|
||||
fun beforeTest() {
|
||||
engine = KtorRepoTestsHelper.beforeTest {
|
||||
configureKeyValuesRepoRoutes(
|
||||
MapKeyValuesRepo(),
|
||||
String.serializer(),
|
||||
String.serializer(),
|
||||
Json
|
||||
)
|
||||
}
|
||||
}
|
||||
@AfterTest
|
||||
fun afterTest() {
|
||||
engine ?.let(KtorRepoTestsHelper::afterTest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testKVsFunctions() {
|
||||
runTest {
|
||||
@@ -28,7 +61,7 @@ class KVsTests {
|
||||
val repo = MapKeyValuesRepo(map)
|
||||
val server = io.ktor.server.engine.embeddedServer(
|
||||
CIO,
|
||||
23456,
|
||||
34567,
|
||||
"127.0.0.1"
|
||||
) {
|
||||
install(ContentNegotiation) {
|
||||
@@ -42,7 +75,7 @@ class KVsTests {
|
||||
repo,
|
||||
Int.serializer(),
|
||||
ComplexData.serializer(),
|
||||
Json {}
|
||||
Json
|
||||
)
|
||||
}
|
||||
}.start(false)
|
||||
@@ -56,7 +89,7 @@ class KVsTests {
|
||||
}
|
||||
}
|
||||
val crudClient = KtorKeyValuesRepoClient(
|
||||
"http://127.0.0.1:23456",
|
||||
"http://127.0.0.1:34567",
|
||||
client,
|
||||
ContentType.Application.Json,
|
||||
Int.serializer(),
|
44
repos/ktor/common/src/jvmTest/kotlin/KtorRepoTestsHelper.kt
Normal file
44
repos/ktor/common/src/jvmTest/kotlin/KtorRepoTestsHelper.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
package dev.inmo.micro_utils.repos.ktor
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.plugins.logging.*
|
||||
import io.ktor.serialization.kotlinx.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.cio.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.plugins.contentnegotiation.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.websocket.*
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
object KtorRepoTestsHelper {
|
||||
fun beforeTest(routingConfigurator: Routing.() -> Unit): ApplicationEngine {
|
||||
return embeddedServer(
|
||||
CIO,
|
||||
23456,
|
||||
"127.0.0.1"
|
||||
) {
|
||||
install(ContentNegotiation) {
|
||||
json()
|
||||
}
|
||||
install(WebSockets) {
|
||||
contentConverter = KotlinxWebsocketSerializationConverter(Json)
|
||||
}
|
||||
routing(routingConfigurator)
|
||||
}.start(false)
|
||||
}
|
||||
fun afterTest(engine: ApplicationEngine) {
|
||||
engine.stop()
|
||||
}
|
||||
fun client(): HttpClient = HttpClient {
|
||||
install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) {
|
||||
json()
|
||||
}
|
||||
install(Logging)
|
||||
install(io.ktor.client.plugins.websocket.WebSockets) {
|
||||
contentConverter = KotlinxWebsocketSerializationConverter(Json)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
package dev.inmo.micro_utils.repos.ktor
|
||||
|
||||
import com.benasher44.uuid.uuid4
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
|
Reference in New Issue
Block a user