fill EXposedPostsAPICommonTests

This commit is contained in:
InsanusMokrassar 2019-11-11 10:15:45 +06:00
parent bf5da036b6
commit b9f233e3fa

View File

@ -0,0 +1,64 @@
package com.insanusmokrassar.postssystem.core.exposed
import com.insanusmokrassar.postssystem.core.post.SimplePost
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.transactions.transactionManager
import org.junit.jupiter.api.*
import java.io.File
import java.sql.Connection
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ExposedPostsAPICommonTests {
private val tempFolder = System.getProperty("java.io.tmpdir")!!
private val numberOfDatabases = 8
private lateinit var databaseFiles: List<File>
private lateinit var apis: List<ExposedPostsAPI>
@BeforeAll
fun prepare() {
databaseFiles = (0 until numberOfDatabases).map {
File("$tempFolder/ExposedPostsAPICommonTestsDB$it.db")
}
apis = databaseFiles.map {
ExposedPostsAPI(
Database.Companion.connect("jdbc:sqlite:${it.absolutePath}", driver = "org.sqlite.JDBC").also {
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
}
)
}
}
@Test
fun `Test that it is possible to use several different databases at one time`() {
val posts = apis.mapIndexed { i, api ->
val content = runBlocking { api.createPost(SimplePost(listOf(i.toString()))) }
assert(content != null)
assert(runBlocking { api.getPostsIds().size == 1 })
content!!
}
posts.forEachIndexed { i, post ->
apis.forEachIndexed { j, api ->
assert(
runBlocking {
api.getPostById(post.id) == (if (i != j) null else post)
}
)
assert(
runBlocking {
api.deletePost(post.id) == (i == j)
}
)
}
}
}
@AfterAll
fun `Close and delete databases`() {
databaseFiles.forEach {
it.delete()
}
}
}