improve tests of repos

This commit is contained in:
InsanusMokrassar 2024-08-09 21:58:09 +06:00
parent b0569f8421
commit 27f5549f56
17 changed files with 229 additions and 117 deletions

View File

@ -1,21 +1,27 @@
package dev.inmo.micro_utils.repos.common.tests
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.CRUDRepo import dev.inmo.micro_utils.repos.CRUDRepo
import dev.inmo.micro_utils.repos.create import dev.inmo.micro_utils.repos.create
import dev.inmo.micro_utils.repos.deleteById import dev.inmo.micro_utils.repos.deleteById
import korlibs.time.seconds import korlibs.time.seconds
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import kotlinx.serialization.Serializable
import kotlin.test.* import kotlin.test.*
abstract class CommonCRUDRepoTests : CommonRepoTests<CRUDRepo<CommonCRUDRepoTests.Registered, String, CommonCRUDRepoTests.New>>() { abstract class CommonCRUDRepoTests : CommonRepoTests<CRUDRepo<CommonCRUDRepoTests.Registered, String, CommonCRUDRepoTests.New>>() {
@Serializable
data class New( data class New(
val data: String val data: String
) )
@Serializable
data class Registered( data class Registered(
val id: String, val id: String,
val data: String val data: String
) )
open fun creatingWorksProperly() = runTest(timeout = 120.seconds) { @Test
fun creatingWorksProperly() = runTest(timeout = 120.seconds) {
val crudRepo = repoCreator() val crudRepo = repoCreator()
val testData = (0 until testSequencesSize).map { val testData = (0 until testSequencesSize).map {
("$it-" + uuid4().toString()) ("$it-" + uuid4().toString())
@ -53,7 +59,7 @@ abstract class CommonCRUDRepoTests : CommonRepoTests<CRUDRepo<CommonCRUDRepoTest
} }
} }
@Test @Test
open fun removingWorksProperly() = runTest { fun removingWorksProperly() = runTest {
val crudRepo = repoCreator() val crudRepo = repoCreator()
val testData = (0 until testSequencesSize).map { val testData = (0 until testSequencesSize).map {
(it.toString() + uuid4().toString()) (it.toString() + uuid4().toString())

View File

@ -1,13 +1,17 @@
package dev.inmo.micro_utils.repos.common.tests
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import korlibs.time.seconds import korlibs.time.seconds
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFalse import kotlin.test.assertFalse
import kotlin.test.assertTrue import kotlin.test.assertTrue
abstract class CommonKeyValueRepoTests : CommonRepoTests<KeyValueRepo<String, String>>() { abstract class CommonKeyValueRepoTests : CommonRepoTests<KeyValueRepo<String, String>>() {
open fun creatingWorksProperly() = runTest(timeout = 120.seconds) { @Test
fun creatingWorksProperly() = runTest(timeout = 120.seconds) {
val repo = repoCreator() val repo = repoCreator()
val testData = (0 until testSequencesSize).associate { val testData = (0 until testSequencesSize).associate {
("$it-" + uuid4().toString()) to "$it-" + uuid4().toString() ("$it-" + uuid4().toString()) to "$it-" + uuid4().toString()
@ -26,7 +30,8 @@ abstract class CommonKeyValueRepoTests : CommonRepoTests<KeyValueRepo<String, St
assertEquals(repo.get(it.key), it.value) assertEquals(repo.get(it.key), it.value)
} }
} }
open fun unsettingWorksProperly() = runTest { @Test
fun unsettingWorksProperly() = runTest {
val repo = repoCreator() val repo = repoCreator()
val testData = (0 until testSequencesSize).associate { val testData = (0 until testSequencesSize).associate {
(it.toString() + uuid4().toString()) to uuid4().toString() (it.toString() + uuid4().toString()) to uuid4().toString()

View File

@ -1,3 +1,5 @@
package dev.inmo.micro_utils.repos.common.tests
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import korlibs.time.seconds import korlibs.time.seconds
@ -7,7 +9,8 @@ import kotlinx.coroutines.test.runTest
import kotlin.test.* import kotlin.test.*
abstract class CommonKeyValuesRepoTests : CommonRepoTests<KeyValuesRepo<String, String>>() { abstract class CommonKeyValuesRepoTests : CommonRepoTests<KeyValuesRepo<String, String>>() {
open fun creatingWorksProperly() = runTest(timeout = 120.seconds) { @Test
fun creatingWorksProperly() = runTest(timeout = 120.seconds) {
val repo = repoCreator() val repo = repoCreator()
val testData = (0 until testSequencesSize).associate { val testData = (0 until testSequencesSize).associate {
("$it-" + uuid4().toString()) to (0 until 1000).map { ("$it-" + uuid4().toString()) to (0 until 1000).map {

View File

@ -1,4 +1,4 @@
import dev.inmo.micro_utils.repos.CRUDRepo package dev.inmo.micro_utils.repos.common.tests
abstract class CommonRepoTests<T> { abstract class CommonRepoTests<T> {
protected open val testSequencesSize = 1000 protected open val testSequencesSize = 1000

View File

@ -1,15 +1,10 @@
package full package full
import CommonCRUDRepoTests
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.CRUDRepo import dev.inmo.micro_utils.repos.CRUDRepo
import dev.inmo.micro_utils.repos.create import dev.inmo.micro_utils.repos.common.tests.CommonCRUDRepoTests
import dev.inmo.micro_utils.repos.deleteById
import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo
import dev.inmo.micro_utils.repos.exposed.initTable import dev.inmo.micro_utils.repos.exposed.initTable
import korlibs.time.seconds
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.ISqlExpressionBuilder import org.jetbrains.exposed.sql.ISqlExpressionBuilder
import org.jetbrains.exposed.sql.Op import org.jetbrains.exposed.sql.Op
@ -17,11 +12,11 @@ import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateBuilder import org.jetbrains.exposed.sql.statements.UpdateBuilder
import java.io.File import java.io.File
import javax.xml.crypto.Data import kotlin.test.AfterTest
import kotlin.test.* import kotlin.test.BeforeTest
class ExposedCRUDRepoTests : CommonCRUDRepoTests() { class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
class Repo(override val database: Database) : AbstractExposedCRUDRepo<CommonCRUDRepoTests.Registered, String, CommonCRUDRepoTests.New>() { class Repo(override val database: Database) : AbstractExposedCRUDRepo<Registered, String, New>() {
val idColumn = text("_id") val idColumn = text("_id")
val dataColumn = text("data") val dataColumn = text("data")
@ -29,8 +24,8 @@ class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
override val ResultRow.asId: String override val ResultRow.asId: String
get() = get(idColumn) get() = get(idColumn)
override val ResultRow.asObject: CommonCRUDRepoTests.Registered override val ResultRow.asObject: Registered
get() = CommonCRUDRepoTests.Registered( get() = Registered(
asId, asId,
get(dataColumn) get(dataColumn)
) )
@ -40,13 +35,13 @@ class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
initTable() initTable()
} }
override fun update(id: String?, value: CommonCRUDRepoTests.New, it: UpdateBuilder<Int>) { override fun update(id: String?, value: New, it: UpdateBuilder<Int>) {
it[idColumn] = id ?: uuid4().toString() it[idColumn] = id ?: uuid4().toString()
it[dataColumn] = value.data it[dataColumn] = value.data
} }
override fun InsertStatement<Number>.asObject(value: CommonCRUDRepoTests.New): CommonCRUDRepoTests.Registered { override fun InsertStatement<Number>.asObject(value: New): Registered {
return CommonCRUDRepoTests.Registered( return Registered(
get(idColumn), get(idColumn),
get(dataColumn) get(dataColumn)
) )
@ -64,14 +59,4 @@ class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
database = null database = null
File(filename).delete() File(filename).delete()
} }
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
@Test
override fun removingWorksProperly(): TestResult {
return super.removingWorksProperly()
}
} }

View File

@ -1,18 +1,17 @@
package full package full
import CommonKeyValueRepoTests import dev.inmo.micro_utils.repos.KeyValueRepo
import com.benasher44.uuid.uuid4 import dev.inmo.micro_utils.repos.common.tests.CommonKeyValueRepoTests
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.exposed.initTable import dev.inmo.micro_utils.repos.exposed.initTable
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
import korlibs.time.seconds import org.jetbrains.exposed.sql.Database
import kotlinx.coroutines.test.TestResult import org.jetbrains.exposed.sql.ISqlExpressionBuilder
import kotlinx.coroutines.test.runTest import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateBuilder import org.jetbrains.exposed.sql.statements.UpdateBuilder
import java.io.File import java.io.File
import kotlin.test.* import kotlin.test.AfterTest
import kotlin.test.BeforeTest
class ExposedKeyValueRepoTests : CommonKeyValueRepoTests() { class ExposedKeyValueRepoTests : CommonKeyValueRepoTests() {
class Repo(override val database: Database) : AbstractExposedKeyValueRepo<String, String>(database) { class Repo(override val database: Database) : AbstractExposedKeyValueRepo<String, String>(database) {
@ -54,14 +53,4 @@ class ExposedKeyValueRepoTests : CommonKeyValueRepoTests() {
} }
override val repoCreator: suspend () -> KeyValueRepo<String, String> = { Repo(database!!) } override val repoCreator: suspend () -> KeyValueRepo<String, String> = { Repo(database!!) }
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
@Test
override fun unsettingWorksProperly(): TestResult {
return super.unsettingWorksProperly()
}
} }

View File

@ -1,17 +1,17 @@
package full package full
import CommonKeyValuesRepoTests import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.common.tests.CommonKeyValuesRepoTests
import dev.inmo.micro_utils.repos.exposed.initTable import dev.inmo.micro_utils.repos.exposed.initTable
import dev.inmo.micro_utils.repos.exposed.onetomany.AbstractExposedKeyValuesRepo import dev.inmo.micro_utils.repos.exposed.onetomany.AbstractExposedKeyValuesRepo
import kotlinx.coroutines.test.TestResult
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.ISqlExpressionBuilder import org.jetbrains.exposed.sql.ISqlExpressionBuilder
import org.jetbrains.exposed.sql.Op import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.UpdateBuilder import org.jetbrains.exposed.sql.statements.UpdateBuilder
import java.io.File import java.io.File
import kotlin.test.* import kotlin.test.AfterTest
import kotlin.test.BeforeTest
class ExposedKeyValuesRepoTests : CommonKeyValuesRepoTests() { class ExposedKeyValuesRepoTests : CommonKeyValuesRepoTests() {
override val testSequencesSize: Int = 100 override val testSequencesSize: Int = 100
@ -49,9 +49,4 @@ class ExposedKeyValuesRepoTests : CommonKeyValuesRepoTests() {
} }
override val repoCreator: suspend () -> KeyValuesRepo<String, String> = { Repo(database!!) } override val repoCreator: suspend () -> KeyValuesRepo<String, String> = { Repo(database!!) }
@Test
override fun creatingWorksProperly(): TestResult {
super.creatingWorksProperly()
}
} }

View File

@ -1,11 +1,9 @@
package full package full
import CommonCRUDRepoTests
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.CRUDRepo import dev.inmo.micro_utils.repos.CRUDRepo
import dev.inmo.micro_utils.repos.MapCRUDRepo import dev.inmo.micro_utils.repos.MapCRUDRepo
import kotlinx.coroutines.test.TestResult import dev.inmo.micro_utils.repos.common.tests.CommonCRUDRepoTests
import kotlin.test.*
class InMemoryCRUDRepoTests : CommonCRUDRepoTests() { class InMemoryCRUDRepoTests : CommonCRUDRepoTests() {
override val repoCreator: suspend () -> CRUDRepo<Registered, String, New> = { override val repoCreator: suspend () -> CRUDRepo<Registered, String, New> = {
@ -18,14 +16,4 @@ class InMemoryCRUDRepoTests : CommonCRUDRepoTests() {
id to Registered(id, it.data) id to Registered(id, it.data)
} }
} }
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
@Test
override fun removingWorksProperly(): TestResult {
return super.removingWorksProperly()
}
} }

View File

@ -1,20 +1,9 @@
package full package full
import CommonKeyValueRepoTests import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.MapKeyValueRepo
import kotlinx.coroutines.test.TestResult import dev.inmo.micro_utils.repos.common.tests.CommonKeyValueRepoTests
import kotlin.test.*
class InMemoryKeyValueRepoTests : CommonKeyValueRepoTests() { class InMemoryKeyValueRepoTests : CommonKeyValueRepoTests() {
override val repoCreator: suspend () -> KeyValueRepo<String, String> = { MapKeyValueRepo() } override val repoCreator: suspend () -> KeyValueRepo<String, String> = { MapKeyValueRepo() }
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
@Test
override fun unsettingWorksProperly(): TestResult {
return super.unsettingWorksProperly()
}
} }

View File

@ -1,14 +1,9 @@
package full package full
import CommonKeyValuesRepoTests import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.MapKeyValuesRepo
import kotlinx.coroutines.test.TestResult import dev.inmo.micro_utils.repos.common.tests.CommonKeyValuesRepoTests
import kotlin.test.*
class InMemoryKeyValuesRepoTests : CommonKeyValuesRepoTests() { class InMemoryKeyValuesRepoTests : CommonKeyValuesRepoTests() {
override val repoCreator: suspend () -> KeyValuesRepo<String, String> = { MapKeyValuesRepo() } override val repoCreator: suspend () -> KeyValuesRepo<String, String> = { MapKeyValuesRepo() }
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
} }

View File

@ -16,6 +16,7 @@ kotlin {
jvmTest { jvmTest {
dependencies { dependencies {
implementation internalProject("micro_utils.repos.common") implementation internalProject("micro_utils.repos.common")
implementation internalProject("micro_utils.repos.common.tests")
implementation internalProject("micro_utils.repos.ktor.client") implementation internalProject("micro_utils.repos.ktor.client")
implementation internalProject("micro_utils.repos.ktor.server") implementation internalProject("micro_utils.repos.ktor.server")
implementation internalProject("micro_utils.repos.inmemory") implementation internalProject("micro_utils.repos.inmemory")

View File

@ -1,3 +1,5 @@
package dev.inmo.micro_utils.repos.ktor
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable

View File

@ -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.*
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.client.crud.KtorCRUDRepoClient
import dev.inmo.micro_utils.repos.ktor.server.crud.configureCRUDRepoRoutes import dev.inmo.micro_utils.repos.ktor.server.crud.configureCRUDRepoRoutes
import io.ktor.client.HttpClient 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.serialization.kotlinx.json.json
import io.ktor.server.application.install import io.ktor.server.application.install
import io.ktor.server.cio.CIO import io.ktor.server.cio.CIO
import io.ktor.server.engine.*
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.routing.routing import io.ktor.server.routing.routing
import io.ktor.server.websocket.WebSockets import io.ktor.server.websocket.WebSockets
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
class CRUDTests { class KtorCRUDRepoTests : CommonCRUDRepoTests() {
@OptIn(ExperimentalCoroutinesApi::class) 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 @Test
fun testCRUDFunctions() { fun testCRUDFunctions() {
runTest { runTest {
@ -33,7 +73,7 @@ class CRUDTests {
} }
val server = io.ktor.server.engine.embeddedServer( val server = io.ktor.server.engine.embeddedServer(
CIO, CIO,
23456, 34567,
"127.0.0.1" "127.0.0.1"
) { ) {
install(ContentNegotiation) { install(ContentNegotiation) {
@ -60,7 +100,7 @@ class CRUDTests {
} }
} }
val crudClient = KtorCRUDRepoClient<ComplexData, Int, SimpleData>( val crudClient = KtorCRUDRepoClient<ComplexData, Int, SimpleData>(
"http://127.0.0.1:23456", "http://127.0.0.1:34567",
client, client,
ContentType.Application.Json ContentType.Application.Json
) { ) {

View File

@ -1,8 +1,13 @@
package dev.inmo.micro_utils.repos.ktor
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
import dev.inmo.micro_utils.repos.* 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.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.value.configureKeyValueRepoRoutes
import dev.inmo.micro_utils.repos.ktor.server.key.values.configureKeyValuesRepoRoutes
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.plugins.logging.Logging import io.ktor.client.plugins.logging.Logging
import io.ktor.http.ContentType 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.serialization.kotlinx.json.json
import io.ktor.server.application.install import io.ktor.server.application.install
import io.ktor.server.cio.CIO import io.ktor.server.cio.CIO
import io.ktor.server.engine.*
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.routing.routing import io.ktor.server.routing.routing
import io.ktor.server.websocket.WebSockets import io.ktor.server.websocket.WebSockets
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlin.test.* import kotlin.test.*
class KVTests { class KtorKeyValueRepoTests : CommonKeyValueRepoTests() {
@OptIn(ExperimentalCoroutinesApi::class) 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 @Test
fun testKVFunctions() { fun testKVFunctions() {
runTest { runTest {
@ -28,7 +63,7 @@ class KVTests {
val repo = MapKeyValueRepo<Int, ComplexData>(map) val repo = MapKeyValueRepo<Int, ComplexData>(map)
val server = io.ktor.server.engine.embeddedServer( val server = io.ktor.server.engine.embeddedServer(
CIO, CIO,
23456, 34567,
"127.0.0.1" "127.0.0.1"
) { ) {
install(ContentNegotiation) { install(ContentNegotiation) {
@ -56,7 +91,7 @@ class KVTests {
} }
} }
val crudClient = KtorKeyValueRepoClient<Int, ComplexData>( val crudClient = KtorKeyValueRepoClient<Int, ComplexData>(
"http://127.0.0.1:23456", "http://127.0.0.1:34567",
client, client,
ContentType.Application.Json, ContentType.Application.Json,
Int.serializer(), Int.serializer(),

View File

@ -1,26 +1,59 @@
package dev.inmo.micro_utils.repos.ktor
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
import dev.inmo.micro_utils.repos.* 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.client.key.values.KtorKeyValuesRepoClient
import dev.inmo.micro_utils.repos.ktor.server.key.values.configureKeyValuesRepoRoutes import dev.inmo.micro_utils.repos.ktor.server.key.values.configureKeyValuesRepoRoutes
import io.ktor.client.HttpClient import io.ktor.client.*
import io.ktor.client.plugins.logging.Logging import io.ktor.client.plugins.logging.*
import io.ktor.http.ContentType import io.ktor.http.ContentType
import io.ktor.serialization.kotlinx.KotlinxWebsocketSerializationConverter import io.ktor.serialization.kotlinx.*
import io.ktor.serialization.kotlinx.json.json import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.install import io.ktor.server.application.*
import io.ktor.server.cio.CIO import io.ktor.server.cio.*
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation import io.ktor.server.engine.*
import io.ktor.server.routing.routing import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.websocket.WebSockets import io.ktor.server.routing.*
import kotlinx.coroutines.ExperimentalCoroutinesApi import io.ktor.server.websocket.*
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlin.test.* import kotlin.test.*
class KVsTests { class KtorKeyValuesRepoTests : CommonKeyValuesRepoTests() {
@OptIn(ExperimentalCoroutinesApi::class) 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 @Test
fun testKVsFunctions() { fun testKVsFunctions() {
runTest { runTest {
@ -28,7 +61,7 @@ class KVsTests {
val repo = MapKeyValuesRepo(map) val repo = MapKeyValuesRepo(map)
val server = io.ktor.server.engine.embeddedServer( val server = io.ktor.server.engine.embeddedServer(
CIO, CIO,
23456, 34567,
"127.0.0.1" "127.0.0.1"
) { ) {
install(ContentNegotiation) { install(ContentNegotiation) {
@ -42,7 +75,7 @@ class KVsTests {
repo, repo,
Int.serializer(), Int.serializer(),
ComplexData.serializer(), ComplexData.serializer(),
Json {} Json
) )
} }
}.start(false) }.start(false)
@ -56,7 +89,7 @@ class KVsTests {
} }
} }
val crudClient = KtorKeyValuesRepoClient( val crudClient = KtorKeyValuesRepoClient(
"http://127.0.0.1:23456", "http://127.0.0.1:34567",
client, client,
ContentType.Application.Json, ContentType.Application.Json,
Int.serializer(), Int.serializer(),

View 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)
}
}
}

View File

@ -1,3 +1,5 @@
package dev.inmo.micro_utils.repos.ktor
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable