86 lines
3.1 KiB
Kotlin
86 lines
3.1 KiB
Kotlin
package dev.inmo.postssystem.core.exposed
|
|
|
|
import dev.inmo.micro_utils.repos.create
|
|
import dev.inmo.micro_utils.repos.deleteById
|
|
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
|
|
import dev.inmo.postssystem.core.content.ContentId
|
|
import dev.inmo.postssystem.core.content.api.business.AdapterType
|
|
import dev.inmo.postssystem.core.content.api.business.asBusinessContentRepo
|
|
import dev.inmo.postssystem.core.content.api.business.content_adapters.text.TextBusinessContentRepoContentAdapter
|
|
import dev.inmo.postssystem.core.content.api.business.content_adapters.text.TextContent
|
|
import kotlinx.coroutines.*
|
|
import kotlinx.coroutines.flow.first
|
|
import org.jetbrains.exposed.sql.Database
|
|
import org.jetbrains.exposed.sql.transactions.transactionManager
|
|
import java.io.File
|
|
import java.sql.Connection
|
|
import kotlin.test.Test
|
|
import kotlin.test.assertEquals
|
|
|
|
class ExposedContentRepoCommonTests {
|
|
private val tempFolder = System.getProperty("java.io.tmpdir")!!
|
|
|
|
@Test
|
|
fun `Test that it is possible to use several different databases at one time`() {
|
|
val numberOfDatabases = 8
|
|
|
|
val databaseFiles = (0 until numberOfDatabases).map {
|
|
"$tempFolder/ExposedContentAPICommonTestsDB$it.db"
|
|
}
|
|
|
|
val apis = databaseFiles.map {
|
|
File(it).also {
|
|
it.delete()
|
|
it.deleteOnExit()
|
|
}
|
|
val database = Database.Companion.connect("jdbc:sqlite:$it", driver = "org.sqlite.JDBC").also {
|
|
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
|
|
}
|
|
ExposedKeyValueRepo<ContentId, AdapterType>(
|
|
database,
|
|
{ text("contentId") },
|
|
{ text("adapterType") },
|
|
"ContentRepo"
|
|
).asBusinessContentRepo(
|
|
TextBusinessContentRepoContentAdapter(
|
|
ExposedKeyValueRepo<ContentId, String>(
|
|
database,
|
|
{ text("contentId") },
|
|
{ text("text") },
|
|
"TextContentRepo"
|
|
)
|
|
)
|
|
)
|
|
}
|
|
|
|
val results = apis.mapIndexed { i, api ->
|
|
val expectedContent = TextContent(i.toString())
|
|
val contents = runBlocking { api.create(TextContent(i.toString())) }
|
|
val idsCount = runBlocking { api.count() }
|
|
assertEquals(idsCount, 1)
|
|
assert(contents.isNotEmpty())
|
|
assertEquals(
|
|
expectedContent,
|
|
contents.first().content
|
|
)
|
|
contents.first()
|
|
}
|
|
|
|
results.forEachIndexed { i, content ->
|
|
apis.forEachIndexed { j, api ->
|
|
assert(
|
|
runBlocking {
|
|
api.getById(content.id) == (if (i != j) null else content)
|
|
}
|
|
)
|
|
runBlocking {
|
|
api.deleteById(content.id)
|
|
}
|
|
}
|
|
}
|
|
|
|
databaseFiles.forEach {
|
|
File(it).delete()
|
|
}
|
|
}
|
|
} |