add tests for cruds

This commit is contained in:
InsanusMokrassar 2024-08-09 19:22:32 +06:00
parent 8d86f29325
commit b0569f8421
21 changed files with 585 additions and 9 deletions

1
.gitignore vendored
View File

@ -18,5 +18,6 @@ publishing.sh
local.*
local/
**/*.local.*
.kotlin/

View File

@ -10,6 +10,8 @@ jb-compose = "1.7.0-alpha02"
jb-exposed = "0.53.0"
jb-dokka = "1.9.20"
sqlite = "3.46.0.1"
korlibs = "5.4.0"
uuid = "0.8.4"
@ -21,7 +23,7 @@ koin = "3.5.6"
okio = "3.9.0"
ksp = "2.0.0-1.0.23"
ksp = "2.0.10-1.0.24"
kotlin-poet = "1.18.0"
versions = "0.51.0"
@ -52,6 +54,7 @@ kt-serialization-cbor = { module = "org.jetbrains.kotlinx:kotlinx-serialization-
kt-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kt-coroutines" }
kt-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kt-coroutines" }
kt-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kt-coroutines" }
kt-coroutines-debug = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-debug", version.ref = "kt-coroutines" }
ktor-io = { module = "io.ktor:ktor-io", version.ref = "ktor" }
@ -79,6 +82,8 @@ koin = { module = "io.insert-koin:koin-core", version.ref = "koin" }
jb-exposed = { module = "org.jetbrains.exposed:exposed-core", version.ref = "jb-exposed" }
jb-exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "jb-exposed" }
sqlite = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite" }
android-coreKtx = { module = "androidx.core:core-ktx", version.ref = "android-coreKtx" }

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

View File

@ -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())
}
}

View File

@ -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())
}
}

View File

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

View File

@ -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
}

View File

@ -14,5 +14,12 @@ kotlin {
api internalProject("micro_utils.pagination.exposed")
}
}
jvmTest {
dependencies {
api libs.sqlite
api libs.jb.exposed.jdbc
api project(":micro_utils.repos.common.tests")
}
}
}
}

View File

@ -52,9 +52,9 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
*
* @return In case when id for the model has been created new [IdType] should be returned
*/
protected open fun createAndInsertId(value: InputValueType, it: InsertStatement<Number>): IdType? = null
protected open fun createAndInsertId(value: InputValueType, it: UpdateBuilder<Int>): IdType? = null
protected open fun insert(value: InputValueType, it: InsertStatement<Number>) {
protected open fun insert(value: InputValueType, it: UpdateBuilder<Int>) {
val id = createAndInsertId(value, it)
update(id, value, it as UpdateBuilder<Int>)
}

View File

@ -23,11 +23,11 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
override val onValueRemoved: Flow<Key> = _onValueRemoved.asSharedFlow()
protected abstract fun update(k: Key, v: Value, it: UpdateBuilder<Int>)
protected abstract fun insertKey(k: Key, v: Value, it: InsertStatement<Number>)
protected abstract fun insertKey(k: Key, v: Value, it: UpdateBuilder<Int>)
protected open fun insert(k: Key, v: Value, it: InsertStatement<Number>) {
protected open fun insert(k: Key, v: Value, it: UpdateBuilder<Int>) {
insertKey(k, v, it)
update(k, v, it as UpdateBuilder<Int>)
update(k, v, it)
}
override suspend fun set(toSet: Map<Key, Value>) {

View File

@ -5,6 +5,7 @@ import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateBuilder
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedKeyValuesRepo<Key, Value>(
@ -26,7 +27,7 @@ abstract class AbstractExposedKeyValuesRepo<Key, Value>(
override val onDataCleared: Flow<Key>
get() = _onDataCleared.asSharedFlow()
protected abstract fun insert(k: Key, v: Value, it: InsertStatement<Number>)
protected abstract fun insert(k: Key, v: Value, it: UpdateBuilder<Int>)
override suspend fun add(toAdd: Map<Key, List<Value>>) {
transaction(database) {
@ -48,6 +49,28 @@ abstract class AbstractExposedKeyValuesRepo<Key, Value>(
}.forEach { _onNewValue.emit(it) }
}
override suspend fun set(toSet: Map<Key, List<Value>>) {
if (toSet.isEmpty()) return
val prepreparedData = toSet.flatMap { (k, vs) ->
vs.map { v ->
k to v
}
}
transaction(database) {
deleteWhere {
selectByIds(it, toSet.keys.toList())
}
batchInsert(
prepreparedData,
) { (k, v) ->
insert(k, v, this)
}.map {
it.asKey to it.asObject
}
}.forEach { _onNewValue.emit(it) }
}
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
transaction(database) {
toRemove.keys.flatMap { k ->

View File

@ -83,8 +83,8 @@ abstract class AbstractExposedReadKeyValuesRepo<Key, Value>(
}
}
override suspend fun getAll(k: Key, reverseLists: Boolean): List<Value> = transaction(database) {
val query = if (reverseLists) {
override suspend fun getAll(k: Key, reversed: Boolean): List<Value> = transaction(database) {
val query = if (reversed) {
selectAll().where { selectById(k) }.orderBy(keyColumn, SortOrder.DESC)
} else {
selectAll().where { selectById(k) }

View File

@ -0,0 +1,21 @@
package full
import com.benasher44.uuid.uuid4
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.transactions.transactionManager
import org.sqlite.JDBC
import java.io.File
import java.sql.Connection
fun filename() = "${uuid4()}.local.sql".also {
val file = File(it)
file.createNewFile()
file.deleteOnExit()
}
fun createDatabase(filename: String) = Database.connect(
url = "jdbc:sqlite:$filename",
driver = JDBC::class.qualifiedName!!
).also {
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
it.connector().close()
}

View File

@ -0,0 +1,77 @@
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.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
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.*
class ExposedCRUDRepoTests : CommonCRUDRepoTests() {
class Repo(override val database: Database) : AbstractExposedCRUDRepo<CommonCRUDRepoTests.Registered, String, CommonCRUDRepoTests.New>() {
val idColumn = text("_id")
val dataColumn = text("data")
override val primaryKey: PrimaryKey = PrimaryKey(idColumn)
override val ResultRow.asId: String
get() = get(idColumn)
override val ResultRow.asObject: CommonCRUDRepoTests.Registered
get() = CommonCRUDRepoTests.Registered(
asId,
get(dataColumn)
)
override val selectById: ISqlExpressionBuilder.(String) -> Op<Boolean> = { idColumn.eq(it) }
init {
initTable()
}
override fun update(id: String?, value: CommonCRUDRepoTests.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(
get(idColumn),
get(dataColumn)
)
}
}
val filename = filename()
var database: Database? = null
override val repoCreator: suspend () -> CRUDRepo<Registered, String, New> = { Repo(database!!) }
@BeforeTest
fun beforeTest() {
database = createDatabase(filename)
}
@AfterTest
fun afterTest() {
database = null
File(filename).delete()
}
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
@Test
override fun removingWorksProperly(): TestResult {
return super.removingWorksProperly()
}
}

View File

@ -0,0 +1,67 @@
package full
import CommonKeyValueRepoTests
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.*
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.statements.UpdateBuilder
import java.io.File
import kotlin.test.*
class ExposedKeyValueRepoTests : CommonKeyValueRepoTests() {
class Repo(override val database: Database) : AbstractExposedKeyValueRepo<String, String>(database) {
override val keyColumn = text("_id")
val dataColumn = text("data")
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn)
override val ResultRow.asKey: String
get() = get(keyColumn)
override val selectByValue: ISqlExpressionBuilder.(String) -> Op<Boolean> = { dataColumn.eq(it) }
override val ResultRow.asObject: String
get() = get(dataColumn)
override val selectById: ISqlExpressionBuilder.(String) -> Op<Boolean> = { keyColumn.eq(it) }
init {
initTable()
}
override fun update(k: String, v: String, it: UpdateBuilder<Int>) {
it[keyColumn] = k
it[dataColumn] = v
}
override fun insertKey(k: String, v: String, it: UpdateBuilder<Int>) {
it[keyColumn] = k
}
}
val filename = filename()
var database: Database? = null
@BeforeTest
fun beforeTest() {
database = createDatabase(filename)
}
@AfterTest
fun afterTest() {
database = null
File(filename).delete()
}
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

@ -0,0 +1,57 @@
package full
import CommonKeyValuesRepoTests
import dev.inmo.micro_utils.repos.*
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.*
class ExposedKeyValuesRepoTests : CommonKeyValuesRepoTests() {
override val testSequencesSize: Int = 100
class Repo(override val database: Database) : AbstractExposedKeyValuesRepo<String, String>(database) {
override val keyColumn = text("_id")
val dataColumn = text("data")
override val ResultRow.asKey: String
get() = get(keyColumn)
override val selectByValue: ISqlExpressionBuilder.(String) -> Op<Boolean> = { dataColumn.eq(it) }
override val ResultRow.asObject: String
get() = get(dataColumn)
override val selectById: ISqlExpressionBuilder.(String) -> Op<Boolean> = { keyColumn.eq(it) }
init {
initTable()
}
override fun insert(k: String, v: String, it: UpdateBuilder<Int>) {
it[keyColumn] = k
it[dataColumn] = v
}
}
val filename = filename()
var database: Database? = null
@BeforeTest
fun beforeTest() {
database = createDatabase(filename)
}
@AfterTest
fun afterTest() {
database = null
File(filename).delete()
}
override val repoCreator: suspend () -> KeyValuesRepo<String, String> = { Repo(database!!) }
@Test
override fun creatingWorksProperly(): TestResult {
super.creatingWorksProperly()
}
}

View File

@ -14,5 +14,10 @@ kotlin {
api internalProject("micro_utils.coroutines")
}
}
commonTest {
dependencies {
api project(":micro_utils.repos.common.tests")
}
}
}
}

View File

@ -0,0 +1,31 @@
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.*
class InMemoryCRUDRepoTests : CommonCRUDRepoTests() {
override val repoCreator: suspend () -> CRUDRepo<Registered, String, New> = {
MapCRUDRepo(
{ new, id, old ->
Registered(id, new.data)
}
) {
val id = uuid4().toString()
id to Registered(id, it.data)
}
}
@Test
override fun creatingWorksProperly(): TestResult {
return super.creatingWorksProperly()
}
@Test
override fun removingWorksProperly(): TestResult {
return super.removingWorksProperly()
}
}

View File

@ -0,0 +1,20 @@
package full
import CommonKeyValueRepoTests
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.test.TestResult
import kotlin.test.*
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

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

View File

@ -18,6 +18,7 @@ String[] includes = [
":language_codes",
":language_codes:generator",
":repos:common",
":repos:common:tests",
":repos:generator",
":repos:generator:test",
":repos:cache",