mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 22:17:03 +05:30
feat(database): migrate away from kotest
This commit is contained in:
parent
7c5a9eb683
commit
4d78a73602
3 changed files with 116 additions and 105 deletions
|
@ -33,8 +33,13 @@ dependencies {
|
|||
implementation(libs.sqldelight.primitiveAdapters)
|
||||
implementation(projects.core)
|
||||
|
||||
testImplementation(libs.kotest.assertions.core)
|
||||
testImplementation(libs.kotest.runner.junit5)
|
||||
testImplementation(libs.junit.jupiter.api)
|
||||
testImplementation(libs.truth) { exclude(group = "junit", module = "junit") }
|
||||
testRuntimeOnly(libs.junit.jupiter.engine)
|
||||
testRuntimeOnly(libs.junit.legacy) {
|
||||
// See https://github.com/google/truth/issues/333
|
||||
because("Truth needs it")
|
||||
}
|
||||
testImplementation(libs.kotlinx.coroutines.core)
|
||||
testImplementation(libs.sqldelight.jvmDriver)
|
||||
}
|
||||
|
|
|
@ -8,41 +8,42 @@ package dev.msfjarvis.claw.database.local
|
|||
|
||||
import app.cash.sqldelight.adapter.primitive.IntColumnAdapter
|
||||
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import dev.msfjarvis.claw.database.LobstersDatabase
|
||||
import dev.msfjarvis.claw.database.model.CSVAdapter
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.collections.shouldHaveSize
|
||||
import io.kotest.matchers.shouldBe
|
||||
import java.util.UUID
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class PostCommentsQueriesTest : FunSpec() {
|
||||
class PostCommentsQueriesTest {
|
||||
private lateinit var postQueries: PostCommentsQueries
|
||||
|
||||
init {
|
||||
beforeEach {
|
||||
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
||||
LobstersDatabase.Schema.create(driver)
|
||||
val database =
|
||||
LobstersDatabase(
|
||||
driver,
|
||||
PostComments.Adapter(CSVAdapter()),
|
||||
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
||||
)
|
||||
postQueries = database.postCommentsQueries
|
||||
}
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
||||
LobstersDatabase.Schema.create(driver)
|
||||
val database =
|
||||
LobstersDatabase(
|
||||
driver,
|
||||
PostComments.Adapter(CSVAdapter()),
|
||||
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
||||
)
|
||||
postQueries = database.postCommentsQueries
|
||||
}
|
||||
|
||||
test("get non-existent post") {
|
||||
val ids = postQueries.getCommentIds(UUID.randomUUID().toString()).executeAsOneOrNull()
|
||||
ids shouldBe null
|
||||
}
|
||||
@Test
|
||||
fun `get non-existent post`() {
|
||||
val ids = postQueries.getCommentIds(UUID.randomUUID().toString()).executeAsOneOrNull()
|
||||
assertThat(ids).isNull()
|
||||
}
|
||||
|
||||
test("put and get post comments") {
|
||||
val postId = UUID.randomUUID().toString()
|
||||
val comments = PostComments(postId, List(10) { UUID.randomUUID().toString() })
|
||||
postQueries.rememberComments(comments)
|
||||
@Test
|
||||
fun `put and get post comments`() {
|
||||
val postId = UUID.randomUUID().toString()
|
||||
val comments = PostComments(postId, List(10) { UUID.randomUUID().toString() })
|
||||
postQueries.rememberComments(comments)
|
||||
|
||||
val ids = postQueries.getCommentIds(postId).executeAsOne().commentIds
|
||||
ids shouldHaveSize 10
|
||||
}
|
||||
val ids = postQueries.getCommentIds(postId).executeAsOne().commentIds
|
||||
assertThat(ids).hasSize(10)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,113 +8,118 @@ package dev.msfjarvis.claw.database.local
|
|||
|
||||
import app.cash.sqldelight.adapter.primitive.IntColumnAdapter
|
||||
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import dev.msfjarvis.claw.database.LobstersDatabase
|
||||
import dev.msfjarvis.claw.database.model.CSVAdapter
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.collections.shouldBeEmpty
|
||||
import io.kotest.matchers.collections.shouldHaveSize
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
class SavedPostQueriesTest : FunSpec() {
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class SavedPostQueriesTest {
|
||||
private lateinit var postQueries: SavedPostQueries
|
||||
|
||||
init {
|
||||
beforeEach {
|
||||
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
||||
LobstersDatabase.Schema.create(driver)
|
||||
val database =
|
||||
LobstersDatabase(
|
||||
driver,
|
||||
PostComments.Adapter(CSVAdapter()),
|
||||
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
||||
)
|
||||
postQueries = database.savedPostQueries
|
||||
}
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
||||
LobstersDatabase.Schema.create(driver)
|
||||
val database =
|
||||
LobstersDatabase(
|
||||
driver,
|
||||
PostComments.Adapter(CSVAdapter()),
|
||||
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
||||
)
|
||||
postQueries = database.savedPostQueries
|
||||
}
|
||||
|
||||
test("add and count posts") {
|
||||
val posts = createTestData(5)
|
||||
@Test
|
||||
fun `add and count posts`() {
|
||||
val posts = createTestData(5)
|
||||
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
|
||||
val postCount = postQueries.selectCount().executeAsOne()
|
||||
val postCount = postQueries.selectCount().executeAsOne()
|
||||
|
||||
postCount shouldBe 5
|
||||
}
|
||||
assertThat(postCount).isEqualTo(5)
|
||||
}
|
||||
|
||||
test("update post in database") {
|
||||
// Get 1 post
|
||||
val post = createTestData(1)[0]
|
||||
@Test
|
||||
fun `update post in database`() {
|
||||
// Get 1 post
|
||||
val post = createTestData(1)[0]
|
||||
|
||||
// Insert post into DB
|
||||
postQueries.insertOrReplacePost(post)
|
||||
// Insert post into DB
|
||||
postQueries.insertOrReplacePost(post)
|
||||
|
||||
// Create a new post and try replacing it
|
||||
val newPost = post.copy(submitterName = "Fake name")
|
||||
postQueries.insertOrReplacePost(newPost)
|
||||
// Create a new post and try replacing it
|
||||
val newPost = post.copy(submitterName = "Fake name")
|
||||
postQueries.insertOrReplacePost(newPost)
|
||||
|
||||
// Check post count
|
||||
val postsCount = postQueries.selectCount().executeAsOne()
|
||||
postsCount shouldBe 1
|
||||
// Check post count
|
||||
val postsCount = postQueries.selectCount().executeAsOne()
|
||||
assertThat(postsCount).isEqualTo(1)
|
||||
|
||||
// Check if post is updated
|
||||
val postFromDb = postQueries.selectPost(post.shortId).executeAsOne()
|
||||
// Check if post is updated
|
||||
val postFromDb = postQueries.selectPost(post.shortId).executeAsOne()
|
||||
|
||||
postFromDb.submitterName shouldBe "Fake name"
|
||||
}
|
||||
assertThat(postFromDb.submitterName).isEqualTo("Fake name")
|
||||
}
|
||||
|
||||
test("get post from db") {
|
||||
// Get 1 post
|
||||
val post = createTestData(1)[0]
|
||||
@Test
|
||||
fun `get post from db`() {
|
||||
// Get 1 post
|
||||
val post = createTestData(1)[0]
|
||||
|
||||
// Insert post into DB
|
||||
postQueries.insertOrReplacePost(post)
|
||||
// Insert post into DB
|
||||
postQueries.insertOrReplacePost(post)
|
||||
|
||||
val postFromDb = postQueries.selectAllPosts().executeAsOne()
|
||||
postFromDb.shortId shouldBe "test_id_1"
|
||||
}
|
||||
val postFromDb = postQueries.selectAllPosts().executeAsOne()
|
||||
assertThat(postFromDb.shortId).isEqualTo("test_id_1")
|
||||
}
|
||||
|
||||
test("get multiple posts from db") {
|
||||
// Get 5 post
|
||||
val posts = createTestData(5)
|
||||
@Test
|
||||
fun `get multiple posts from db`() {
|
||||
// Get 5 post
|
||||
val posts = createTestData(5)
|
||||
|
||||
// Insert posts into DB
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
// Insert posts into DB
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
|
||||
val postsFromDb = postQueries.selectAllPosts().executeAsList()
|
||||
val postsFromDb = postQueries.selectAllPosts().executeAsList()
|
||||
|
||||
// Check if all posts have correct shortId
|
||||
postsFromDb.forEachIndexed { index, post -> post.shortId shouldBe "test_id_${index.inc()}" }
|
||||
// Check if all posts have correct shortId
|
||||
postsFromDb.forEachIndexed { index, post ->
|
||||
assertThat(post.shortId).isEqualTo("test_id_${index.inc()}")
|
||||
}
|
||||
}
|
||||
|
||||
test("delete post") {
|
||||
// Create 3 posts and insert them to DB
|
||||
val posts = createTestData(3)
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
@Test
|
||||
fun `delete post`() {
|
||||
// Create 3 posts and insert them to DB
|
||||
val posts = createTestData(3)
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
|
||||
// Delete 2nd post
|
||||
postQueries.deletePost("test_id_2")
|
||||
// Delete 2nd post
|
||||
postQueries.deletePost("test_id_2")
|
||||
|
||||
val postsFromDB = postQueries.selectAllPosts().executeAsList()
|
||||
val postsFromDB = postQueries.selectAllPosts().executeAsList()
|
||||
|
||||
// Check if size is 2, and only the correct post is deleted
|
||||
postsFromDB shouldHaveSize 2
|
||||
postsFromDB[0].shortId shouldBe "test_id_1"
|
||||
postsFromDB[1].shortId shouldBe "test_id_3"
|
||||
}
|
||||
// Check if size is 2, and only the correct post is deleted
|
||||
assertThat(postsFromDB).hasSize(2)
|
||||
assertThat(postsFromDB[0].shortId).isEqualTo("test_id_1")
|
||||
assertThat(postsFromDB[1].shortId).isEqualTo("test_id_3")
|
||||
}
|
||||
|
||||
test("delete all posts") {
|
||||
// Create 5 posts and insert them to DB
|
||||
val posts = createTestData(5)
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
@Test
|
||||
fun `delete all posts`() {
|
||||
// Create 5 posts and insert them to DB
|
||||
val posts = createTestData(5)
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
|
||||
// Delete all posts
|
||||
postQueries.deleteAllPosts()
|
||||
// Delete all posts
|
||||
postQueries.deleteAllPosts()
|
||||
|
||||
val dbPosts = postQueries.selectAllPosts().executeAsList()
|
||||
val dbPosts = postQueries.selectAllPosts().executeAsList()
|
||||
|
||||
dbPosts.shouldBeEmpty()
|
||||
}
|
||||
assertThat(dbPosts).isEmpty()
|
||||
}
|
||||
|
||||
private fun createTestData(count: Int): ArrayList<SavedPost> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue