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

View File

@ -1,13 +1,17 @@
package dev.inmo.micro_utils.repos.common.tests
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.*
import korlibs.time.seconds
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
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 testData = (0 until testSequencesSize).associate {
("$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)
}
}
open fun unsettingWorksProperly() = runTest {
@Test
fun unsettingWorksProperly() = runTest {
val repo = repoCreator()
val testData = (0 until testSequencesSize).associate {
(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 dev.inmo.micro_utils.repos.*
import korlibs.time.seconds
@ -7,7 +9,8 @@ import kotlinx.coroutines.test.runTest
import kotlin.test.*
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 testData = (0 until testSequencesSize).associate {
("$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> {
protected open val testSequencesSize = 1000

View File

@ -1,15 +1,10 @@
package full
import CommonCRUDRepoTests
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.CRUDRepo
import dev.inmo.micro_utils.repos.create
import dev.inmo.micro_utils.repos.deleteById
import dev.inmo.micro_utils.repos.common.tests.CommonCRUDRepoTests
import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo
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.ISqlExpressionBuilder
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.UpdateBuilder
import java.io.File
import javax.xml.crypto.Data
import kotlin.test.*
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
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 dataColumn = text("data")
@ -29,8 +24,8 @@ class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
override val ResultRow.asId: String
get() = get(idColumn)
override val ResultRow.asObject: CommonCRUDRepoTests.Registered
get() = CommonCRUDRepoTests.Registered(
override val ResultRow.asObject: Registered
get() = Registered(
asId,
get(dataColumn)
)
@ -40,13 +35,13 @@ class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
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[dataColumn] = value.data
}
override fun InsertStatement<Number>.asObject(value: CommonCRUDRepoTests.New): CommonCRUDRepoTests.Registered {
return CommonCRUDRepoTests.Registered(
override fun InsertStatement<Number>.asObject(value: New): Registered {
return Registered(
get(idColumn),
get(dataColumn)
)
@ -64,14 +59,4 @@ class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
database = null
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
import CommonKeyValueRepoTests
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.common.tests.CommonKeyValueRepoTests
import dev.inmo.micro_utils.repos.exposed.initTable
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
import korlibs.time.seconds
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.ISqlExpressionBuilder
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.UpdateBuilder
import java.io.File
import kotlin.test.*
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
class ExposedKeyValueRepoTests : CommonKeyValueRepoTests() {
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!!) }
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
@Test
override fun unsettingWorksProperly(): TestResult {
return super.unsettingWorksProperly()
}
}

View File

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

View File

@ -1,11 +1,9 @@
package full
import CommonCRUDRepoTests
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.CRUDRepo
import dev.inmo.micro_utils.repos.MapCRUDRepo
import kotlinx.coroutines.test.TestResult
import kotlin.test.*
import dev.inmo.micro_utils.repos.common.tests.CommonCRUDRepoTests
class InMemoryCRUDRepoTests : CommonCRUDRepoTests() {
override val repoCreator: suspend () -> CRUDRepo<Registered, String, New> = {
@ -18,14 +16,4 @@ class InMemoryCRUDRepoTests : CommonCRUDRepoTests() {
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
import CommonKeyValueRepoTests
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.test.TestResult
import kotlin.test.*
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.MapKeyValueRepo
import dev.inmo.micro_utils.repos.common.tests.CommonKeyValueRepoTests
class InMemoryKeyValueRepoTests : CommonKeyValueRepoTests() {
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
import CommonKeyValuesRepoTests
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.test.TestResult
import kotlin.test.*
import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.MapKeyValuesRepo
import dev.inmo.micro_utils.repos.common.tests.CommonKeyValuesRepoTests
class InMemoryKeyValuesRepoTests : CommonKeyValuesRepoTests() {
override val repoCreator: suspend () -> KeyValuesRepo<String, String> = { MapKeyValuesRepo() }
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
}

View File

@ -16,6 +16,7 @@ kotlin {
jvmTest {
dependencies {
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.server")
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 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.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
) {

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.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(),

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.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(),

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 kotlinx.serialization.Serializable