mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-14 21:09:24 +00:00
add tests for cruds
This commit is contained in:
69
repos/common/tests/build.gradle
Normal file
69
repos/common/tests/build.gradle
Normal file
@@ -0,0 +1,69 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
project.version = "$version"
|
||||
project.group = "$group"
|
||||
|
||||
kotlin {
|
||||
jvm {
|
||||
compilations.main {
|
||||
kotlinOptions {
|
||||
jvmTarget = "17"
|
||||
}
|
||||
}
|
||||
}
|
||||
js (IR) {
|
||||
browser()
|
||||
nodejs()
|
||||
}
|
||||
androidTarget {
|
||||
compilations.all {
|
||||
kotlinOptions {
|
||||
jvmTarget = "17"
|
||||
}
|
||||
}
|
||||
}
|
||||
linuxX64()
|
||||
mingwX64()
|
||||
linuxArm64()
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
api libs.kt.serialization
|
||||
api kotlin('test')
|
||||
api kotlin('test-annotations-common')
|
||||
api libs.kt.coroutines.test
|
||||
api project(":micro_utils.repos.common")
|
||||
}
|
||||
}
|
||||
|
||||
jvmMain {
|
||||
dependencies {
|
||||
implementation kotlin('test-junit')
|
||||
}
|
||||
}
|
||||
jsMain {
|
||||
dependencies {
|
||||
implementation kotlin('test-js')
|
||||
}
|
||||
}
|
||||
nativeMain.dependsOn commonMain
|
||||
linuxX64Main.dependsOn nativeMain
|
||||
mingwX64Main.dependsOn nativeMain
|
||||
linuxArm64Main.dependsOn nativeMain
|
||||
|
||||
androidMain.dependsOn jvmMain
|
||||
}
|
||||
}
|
||||
|
||||
apply from: "$defaultAndroidSettingsPresetPath"
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
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 kotlin.test.*
|
||||
|
||||
abstract class CommonCRUDRepoTests : CommonRepoTests<CRUDRepo<CommonCRUDRepoTests.Registered, String, CommonCRUDRepoTests.New>>() {
|
||||
data class New(
|
||||
val data: String
|
||||
)
|
||||
data class Registered(
|
||||
val id: String,
|
||||
val data: String
|
||||
)
|
||||
|
||||
open fun creatingWorksProperly() = runTest(timeout = 120.seconds) {
|
||||
val crudRepo = repoCreator()
|
||||
val testData = (0 until testSequencesSize).map {
|
||||
("$it-" + uuid4().toString())
|
||||
}
|
||||
val updatedTestData = (0 until 1000).map {
|
||||
("$it-" + uuid4().toString())
|
||||
}
|
||||
|
||||
val registereds = testData.map {
|
||||
val created = crudRepo.create(New(it)).first()
|
||||
assertEquals(it, created.data)
|
||||
assertEquals(crudRepo.getById(created.id), created)
|
||||
created
|
||||
}
|
||||
|
||||
crudRepo.getAll().forEach { (id, value) ->
|
||||
assertTrue {
|
||||
registereds.first {
|
||||
it.id == id
|
||||
} == value
|
||||
}
|
||||
}
|
||||
val updatedRegistereds = registereds.mapIndexed { i, it ->
|
||||
val updated = crudRepo.update(it.id, New(updatedTestData[i])) ?: error("Unable to update data for $it")
|
||||
assertEquals(updatedTestData[i], updated.data)
|
||||
assertEquals(crudRepo.getById(updated.id), updated)
|
||||
updated
|
||||
}
|
||||
crudRepo.getAll().forEach { (id, value) ->
|
||||
assertTrue {
|
||||
updatedRegistereds.first {
|
||||
it.id == id
|
||||
} == value
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test
|
||||
open fun removingWorksProperly() = runTest {
|
||||
val crudRepo = repoCreator()
|
||||
val testData = (0 until testSequencesSize).map {
|
||||
(it.toString() + uuid4().toString())
|
||||
}
|
||||
val registereds = crudRepo.create(testData.map { New(it) })
|
||||
|
||||
registereds.forEach {
|
||||
val id = it.id
|
||||
assertTrue {
|
||||
crudRepo.getAll()[id] == it
|
||||
}
|
||||
|
||||
crudRepo.deleteById(id)
|
||||
assertFalse {
|
||||
crudRepo.contains(id)
|
||||
}
|
||||
}
|
||||
assertEquals(0, crudRepo.count())
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
import com.benasher44.uuid.uuid4
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import korlibs.time.seconds
|
||||
import kotlinx.coroutines.test.runTest
|
||||
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) {
|
||||
val repo = repoCreator()
|
||||
val testData = (0 until testSequencesSize).associate {
|
||||
("$it-" + uuid4().toString()) to "$it-" + uuid4().toString()
|
||||
}
|
||||
val updatedTestData = testData.keys.associateWith {
|
||||
"$it-" + uuid4().toString()
|
||||
}
|
||||
|
||||
testData.forEach {
|
||||
repo.set(it.key, it.value)
|
||||
assertEquals(it.value, repo.get(it.key))
|
||||
}
|
||||
|
||||
updatedTestData.forEach {
|
||||
repo.set(it.key, it.value)
|
||||
assertEquals(repo.get(it.key), it.value)
|
||||
}
|
||||
}
|
||||
open fun unsettingWorksProperly() = runTest {
|
||||
val repo = repoCreator()
|
||||
val testData = (0 until testSequencesSize).associate {
|
||||
(it.toString() + uuid4().toString()) to uuid4().toString()
|
||||
}
|
||||
|
||||
repo.set(testData)
|
||||
|
||||
testData.forEach {
|
||||
val id = it.key
|
||||
assertTrue {
|
||||
repo.getAll()[id] == it.value
|
||||
}
|
||||
|
||||
repo.unset(id)
|
||||
assertFalse {
|
||||
repo.contains(id)
|
||||
}
|
||||
}
|
||||
assertEquals(0, repo.count())
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
import com.benasher44.uuid.uuid4
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import korlibs.time.seconds
|
||||
import kotlinx.coroutines.joinAll
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlin.test.*
|
||||
|
||||
abstract class CommonKeyValuesRepoTests : CommonRepoTests<KeyValuesRepo<String, String>>() {
|
||||
open fun creatingWorksProperly() = runTest(timeout = 120.seconds) {
|
||||
val repo = repoCreator()
|
||||
val testData = (0 until testSequencesSize).associate {
|
||||
("$it-" + uuid4().toString()) to (0 until 1000).map {
|
||||
"$it-" + uuid4().toString()
|
||||
}.sorted()
|
||||
}
|
||||
val updatedTestData = testData.keys.associateWith {
|
||||
(0 until 1000).map {
|
||||
"$it-" + uuid4().toString()
|
||||
}.sorted()
|
||||
}
|
||||
val addedData = testData.keys.associateWith {
|
||||
"$it-" + uuid4().toString()
|
||||
}
|
||||
|
||||
updatedTestData.map {
|
||||
launch {
|
||||
repo.set(it.key, it.value)
|
||||
assertContentEquals(it.value.sorted(), repo.getAll(it.key).sorted())
|
||||
}
|
||||
}.joinAll()
|
||||
|
||||
updatedTestData.map {
|
||||
launch {
|
||||
repo.set(it.key, it.value)
|
||||
val all = repo.getAll(it.key)
|
||||
assertContentEquals(it.value.sorted(), all.sorted())
|
||||
}
|
||||
}.joinAll()
|
||||
|
||||
addedData.forEach {
|
||||
repo.add(it.key, it.value)
|
||||
assertTrue(repo.contains(it.key, it.value))
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
import dev.inmo.micro_utils.repos.CRUDRepo
|
||||
|
||||
abstract class CommonRepoTests<T> {
|
||||
protected open val testSequencesSize = 1000
|
||||
protected abstract val repoCreator: suspend () -> T
|
||||
}
|
Reference in New Issue
Block a user