62 lines
1.9 KiB
Kotlin
62 lines
1.9 KiB
Kotlin
package dev.inmo.postssystem.core.exposed
|
|
|
|
import dev.inmo.postssystem.core.content.TextContent
|
|
import kotlinx.coroutines.runBlocking
|
|
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()
|
|
}
|
|
ExposedContentRepo(
|
|
Database.Companion.connect("jdbc:sqlite:$it", driver = "org.sqlite.JDBC").also {
|
|
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
|
|
}
|
|
)
|
|
}
|
|
|
|
val results = apis.mapIndexed { i, api ->
|
|
val content = runBlocking { api.registerContent(TextContent(i.toString())) }
|
|
assert(content != null)
|
|
val ids = runBlocking { api.getContentsIds() }
|
|
assertEquals(ids.size, 1)
|
|
content!!
|
|
}
|
|
|
|
results.forEachIndexed { i, content ->
|
|
apis.forEachIndexed { j, api ->
|
|
assert(
|
|
runBlocking {
|
|
api.getContentById(content.id) == (if (i != j) null else content)
|
|
}
|
|
)
|
|
assert(
|
|
runBlocking {
|
|
api.deleteContent(content.id) == (i == j)
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
databaseFiles.forEach {
|
|
File(it).delete()
|
|
}
|
|
}
|
|
} |