diff --git a/.gitignore b/.gitignore index 6c97217ff69..42156086770 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,6 @@ publishing.sh local.* local/ +**/*.local.* .kotlin/ diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e405865cb4a..77581e5a842 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" } diff --git a/repos/common/tests/build.gradle b/repos/common/tests/build.gradle new file mode 100644 index 00000000000..ba8a8f0d4ec --- /dev/null +++ b/repos/common/tests/build.gradle @@ -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 +} \ No newline at end of file diff --git a/repos/common/tests/src/commonMain/kotlin/CommonCRUDRepoTests.kt b/repos/common/tests/src/commonMain/kotlin/CommonCRUDRepoTests.kt new file mode 100644 index 00000000000..d8f21192b3d --- /dev/null +++ b/repos/common/tests/src/commonMain/kotlin/CommonCRUDRepoTests.kt @@ -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>() { + 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()) + } +} diff --git a/repos/common/tests/src/commonMain/kotlin/CommonKeyValueRepoTests.kt b/repos/common/tests/src/commonMain/kotlin/CommonKeyValueRepoTests.kt new file mode 100644 index 00000000000..b461f1876f1 --- /dev/null +++ b/repos/common/tests/src/commonMain/kotlin/CommonKeyValueRepoTests.kt @@ -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>() { + 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()) + } +} diff --git a/repos/common/tests/src/commonMain/kotlin/CommonKeyValuesRepoTests.kt b/repos/common/tests/src/commonMain/kotlin/CommonKeyValuesRepoTests.kt new file mode 100644 index 00000000000..a13ad8695de --- /dev/null +++ b/repos/common/tests/src/commonMain/kotlin/CommonKeyValuesRepoTests.kt @@ -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>() { + 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)) + } + } +} diff --git a/repos/common/tests/src/commonMain/kotlin/CommonRepoTests.kt b/repos/common/tests/src/commonMain/kotlin/CommonRepoTests.kt new file mode 100644 index 00000000000..b774bfdbd61 --- /dev/null +++ b/repos/common/tests/src/commonMain/kotlin/CommonRepoTests.kt @@ -0,0 +1,6 @@ +import dev.inmo.micro_utils.repos.CRUDRepo + +abstract class CommonRepoTests { + protected open val testSequencesSize = 1000 + protected abstract val repoCreator: suspend () -> T +} \ No newline at end of file diff --git a/repos/exposed/build.gradle b/repos/exposed/build.gradle index dcb01813d04..c92d0d600f3 100644 --- a/repos/exposed/build.gradle +++ b/repos/exposed/build.gradle @@ -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") + } + } } } diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt index 083ccb075b6..6126720eb93 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt @@ -52,9 +52,9 @@ abstract class AbstractExposedWriteCRUDRepo( * * @return In case when id for the model has been created new [IdType] should be returned */ - protected open fun createAndInsertId(value: InputValueType, it: InsertStatement): IdType? = null + protected open fun createAndInsertId(value: InputValueType, it: UpdateBuilder): IdType? = null - protected open fun insert(value: InputValueType, it: InsertStatement) { + protected open fun insert(value: InputValueType, it: UpdateBuilder) { val id = createAndInsertId(value, it) update(id, value, it as UpdateBuilder) } diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt index a5a939c1b10..74ca058b1ec 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt @@ -23,11 +23,11 @@ abstract class AbstractExposedKeyValueRepo( override val onValueRemoved: Flow = _onValueRemoved.asSharedFlow() protected abstract fun update(k: Key, v: Value, it: UpdateBuilder) - protected abstract fun insertKey(k: Key, v: Value, it: InsertStatement) + protected abstract fun insertKey(k: Key, v: Value, it: UpdateBuilder) - protected open fun insert(k: Key, v: Value, it: InsertStatement) { + protected open fun insert(k: Key, v: Value, it: UpdateBuilder) { insertKey(k, v, it) - update(k, v, it as UpdateBuilder) + update(k, v, it) } override suspend fun set(toSet: Map) { diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValuesRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValuesRepo.kt index f7d0fb5e3c6..7c934de83eb 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValuesRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValuesRepo.kt @@ -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( @@ -26,7 +27,7 @@ abstract class AbstractExposedKeyValuesRepo( override val onDataCleared: Flow get() = _onDataCleared.asSharedFlow() - protected abstract fun insert(k: Key, v: Value, it: InsertStatement) + protected abstract fun insert(k: Key, v: Value, it: UpdateBuilder) override suspend fun add(toAdd: Map>) { transaction(database) { @@ -48,6 +49,28 @@ abstract class AbstractExposedKeyValuesRepo( }.forEach { _onNewValue.emit(it) } } + override suspend fun set(toSet: Map>) { + 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>) { transaction(database) { toRemove.keys.flatMap { k -> diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValuesRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValuesRepo.kt index 1864ee0581d..95563e43724 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValuesRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValuesRepo.kt @@ -83,8 +83,8 @@ abstract class AbstractExposedReadKeyValuesRepo( } } - override suspend fun getAll(k: Key, reverseLists: Boolean): List = transaction(database) { - val query = if (reverseLists) { + override suspend fun getAll(k: Key, reversed: Boolean): List = transaction(database) { + val query = if (reversed) { selectAll().where { selectById(k) }.orderBy(keyColumn, SortOrder.DESC) } else { selectAll().where { selectById(k) } diff --git a/repos/exposed/src/jvmTest/kotlin/Database.kt b/repos/exposed/src/jvmTest/kotlin/Database.kt new file mode 100644 index 00000000000..6a7c2edfb75 --- /dev/null +++ b/repos/exposed/src/jvmTest/kotlin/Database.kt @@ -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() +} diff --git a/repos/exposed/src/jvmTest/kotlin/ExposedCRUDRepoTests.kt b/repos/exposed/src/jvmTest/kotlin/ExposedCRUDRepoTests.kt new file mode 100644 index 00000000000..7821a94e587 --- /dev/null +++ b/repos/exposed/src/jvmTest/kotlin/ExposedCRUDRepoTests.kt @@ -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() { + 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 = { idColumn.eq(it) } + + init { + initTable() + } + + override fun update(id: String?, value: CommonCRUDRepoTests.New, it: UpdateBuilder) { + it[idColumn] = id ?: uuid4().toString() + it[dataColumn] = value.data + } + + override fun InsertStatement.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 = { 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() + } +} diff --git a/repos/exposed/src/jvmTest/kotlin/ExposedKeyValueRepoTests.kt b/repos/exposed/src/jvmTest/kotlin/ExposedKeyValueRepoTests.kt new file mode 100644 index 00000000000..e142511804d --- /dev/null +++ b/repos/exposed/src/jvmTest/kotlin/ExposedKeyValueRepoTests.kt @@ -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(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 = { dataColumn.eq(it) } + override val ResultRow.asObject: String + get() = get(dataColumn) + override val selectById: ISqlExpressionBuilder.(String) -> Op = { keyColumn.eq(it) } + + init { + initTable() + } + + override fun update(k: String, v: String, it: UpdateBuilder) { + it[keyColumn] = k + it[dataColumn] = v + } + + override fun insertKey(k: String, v: String, it: UpdateBuilder) { + 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 = { Repo(database!!) } + + @Test + override fun creatingWorksProperly(): TestResult { + return super.creatingWorksProperly() + } + + @Test + override fun unsettingWorksProperly(): TestResult { + return super.unsettingWorksProperly() + } +} diff --git a/repos/exposed/src/jvmTest/kotlin/ExposedKeyValuesRepoTests.kt b/repos/exposed/src/jvmTest/kotlin/ExposedKeyValuesRepoTests.kt new file mode 100644 index 00000000000..43ec7718941 --- /dev/null +++ b/repos/exposed/src/jvmTest/kotlin/ExposedKeyValuesRepoTests.kt @@ -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(database) { + + override val keyColumn = text("_id") + val dataColumn = text("data") + + override val ResultRow.asKey: String + get() = get(keyColumn) + override val selectByValue: ISqlExpressionBuilder.(String) -> Op = { dataColumn.eq(it) } + override val ResultRow.asObject: String + get() = get(dataColumn) + override val selectById: ISqlExpressionBuilder.(String) -> Op = { keyColumn.eq(it) } + + init { + initTable() + } + + override fun insert(k: String, v: String, it: UpdateBuilder) { + 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 = { Repo(database!!) } + + @Test + override fun creatingWorksProperly(): TestResult { + super.creatingWorksProperly() + } +} diff --git a/repos/inmemory/build.gradle b/repos/inmemory/build.gradle index 7de5521c228..ec31c5fa1ea 100644 --- a/repos/inmemory/build.gradle +++ b/repos/inmemory/build.gradle @@ -14,5 +14,10 @@ kotlin { api internalProject("micro_utils.coroutines") } } + commonTest { + dependencies { + api project(":micro_utils.repos.common.tests") + } + } } } \ No newline at end of file diff --git a/repos/inmemory/src/commonTest/kotlin/InMemoryCRUDRepoTests.kt b/repos/inmemory/src/commonTest/kotlin/InMemoryCRUDRepoTests.kt new file mode 100644 index 00000000000..c14d499212d --- /dev/null +++ b/repos/inmemory/src/commonTest/kotlin/InMemoryCRUDRepoTests.kt @@ -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 = { + 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() + } +} diff --git a/repos/inmemory/src/commonTest/kotlin/InMemoryKeyValueRepoTests.kt b/repos/inmemory/src/commonTest/kotlin/InMemoryKeyValueRepoTests.kt new file mode 100644 index 00000000000..3da9bf755b8 --- /dev/null +++ b/repos/inmemory/src/commonTest/kotlin/InMemoryKeyValueRepoTests.kt @@ -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 = { MapKeyValueRepo() } + + @Test + override fun creatingWorksProperly(): TestResult { + return super.creatingWorksProperly() + } + + @Test + override fun unsettingWorksProperly(): TestResult { + return super.unsettingWorksProperly() + } +} diff --git a/repos/inmemory/src/commonTest/kotlin/InMemoryKeyValuesRepoTests.kt b/repos/inmemory/src/commonTest/kotlin/InMemoryKeyValuesRepoTests.kt new file mode 100644 index 00000000000..0d943bb713d --- /dev/null +++ b/repos/inmemory/src/commonTest/kotlin/InMemoryKeyValuesRepoTests.kt @@ -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 = { MapKeyValuesRepo() } + @Test + override fun creatingWorksProperly(): TestResult { + return super.creatingWorksProperly() + } +} diff --git a/settings.gradle b/settings.gradle index 039ae66714a..3c8ea281c61 100644 --- a/settings.gradle +++ b/settings.gradle @@ -18,6 +18,7 @@ String[] includes = [ ":language_codes", ":language_codes:generator", ":repos:common", + ":repos:common:tests", ":repos:generator", ":repos:generator:test", ":repos:cache",