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(libs.sqldelight.primitiveAdapters)
|
||||||
implementation(projects.core)
|
implementation(projects.core)
|
||||||
|
|
||||||
testImplementation(libs.kotest.assertions.core)
|
testImplementation(libs.junit.jupiter.api)
|
||||||
testImplementation(libs.kotest.runner.junit5)
|
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.kotlinx.coroutines.core)
|
||||||
testImplementation(libs.sqldelight.jvmDriver)
|
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.adapter.primitive.IntColumnAdapter
|
||||||
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
|
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.LobstersDatabase
|
||||||
import dev.msfjarvis.claw.database.model.CSVAdapter
|
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 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
|
private lateinit var postQueries: PostCommentsQueries
|
||||||
|
|
||||||
init {
|
@BeforeEach
|
||||||
beforeEach {
|
fun setup() {
|
||||||
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
||||||
LobstersDatabase.Schema.create(driver)
|
LobstersDatabase.Schema.create(driver)
|
||||||
val database =
|
val database =
|
||||||
LobstersDatabase(
|
LobstersDatabase(
|
||||||
driver,
|
driver,
|
||||||
PostComments.Adapter(CSVAdapter()),
|
PostComments.Adapter(CSVAdapter()),
|
||||||
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
||||||
)
|
)
|
||||||
postQueries = database.postCommentsQueries
|
postQueries = database.postCommentsQueries
|
||||||
}
|
}
|
||||||
|
|
||||||
test("get non-existent post") {
|
@Test
|
||||||
val ids = postQueries.getCommentIds(UUID.randomUUID().toString()).executeAsOneOrNull()
|
fun `get non-existent post`() {
|
||||||
ids shouldBe null
|
val ids = postQueries.getCommentIds(UUID.randomUUID().toString()).executeAsOneOrNull()
|
||||||
}
|
assertThat(ids).isNull()
|
||||||
|
}
|
||||||
|
|
||||||
test("put and get post comments") {
|
@Test
|
||||||
val postId = UUID.randomUUID().toString()
|
fun `put and get post comments`() {
|
||||||
val comments = PostComments(postId, List(10) { UUID.randomUUID().toString() })
|
val postId = UUID.randomUUID().toString()
|
||||||
postQueries.rememberComments(comments)
|
val comments = PostComments(postId, List(10) { UUID.randomUUID().toString() })
|
||||||
|
postQueries.rememberComments(comments)
|
||||||
|
|
||||||
val ids = postQueries.getCommentIds(postId).executeAsOne().commentIds
|
val ids = postQueries.getCommentIds(postId).executeAsOne().commentIds
|
||||||
ids shouldHaveSize 10
|
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.adapter.primitive.IntColumnAdapter
|
||||||
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
|
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.LobstersDatabase
|
||||||
import dev.msfjarvis.claw.database.model.CSVAdapter
|
import dev.msfjarvis.claw.database.model.CSVAdapter
|
||||||
import io.kotest.core.spec.style.FunSpec
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import io.kotest.matchers.collections.shouldBeEmpty
|
import org.junit.jupiter.api.Test
|
||||||
import io.kotest.matchers.collections.shouldHaveSize
|
|
||||||
import io.kotest.matchers.shouldBe
|
|
||||||
|
|
||||||
class SavedPostQueriesTest : FunSpec() {
|
|
||||||
|
|
||||||
|
class SavedPostQueriesTest {
|
||||||
private lateinit var postQueries: SavedPostQueries
|
private lateinit var postQueries: SavedPostQueries
|
||||||
|
|
||||||
init {
|
@BeforeEach
|
||||||
beforeEach {
|
fun setup() {
|
||||||
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
|
||||||
LobstersDatabase.Schema.create(driver)
|
LobstersDatabase.Schema.create(driver)
|
||||||
val database =
|
val database =
|
||||||
LobstersDatabase(
|
LobstersDatabase(
|
||||||
driver,
|
driver,
|
||||||
PostComments.Adapter(CSVAdapter()),
|
PostComments.Adapter(CSVAdapter()),
|
||||||
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
SavedPost.Adapter(IntColumnAdapter, CSVAdapter()),
|
||||||
)
|
)
|
||||||
postQueries = database.savedPostQueries
|
postQueries = database.savedPostQueries
|
||||||
}
|
}
|
||||||
|
|
||||||
test("add and count posts") {
|
@Test
|
||||||
val posts = createTestData(5)
|
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") {
|
@Test
|
||||||
// Get 1 post
|
fun `update post in database`() {
|
||||||
val post = createTestData(1)[0]
|
// Get 1 post
|
||||||
|
val post = createTestData(1)[0]
|
||||||
|
|
||||||
// Insert post into DB
|
// Insert post into DB
|
||||||
postQueries.insertOrReplacePost(post)
|
postQueries.insertOrReplacePost(post)
|
||||||
|
|
||||||
// Create a new post and try replacing it
|
// Create a new post and try replacing it
|
||||||
val newPost = post.copy(submitterName = "Fake name")
|
val newPost = post.copy(submitterName = "Fake name")
|
||||||
postQueries.insertOrReplacePost(newPost)
|
postQueries.insertOrReplacePost(newPost)
|
||||||
|
|
||||||
// Check post count
|
// Check post count
|
||||||
val postsCount = postQueries.selectCount().executeAsOne()
|
val postsCount = postQueries.selectCount().executeAsOne()
|
||||||
postsCount shouldBe 1
|
assertThat(postsCount).isEqualTo(1)
|
||||||
|
|
||||||
// Check if post is updated
|
// Check if post is updated
|
||||||
val postFromDb = postQueries.selectPost(post.shortId).executeAsOne()
|
val postFromDb = postQueries.selectPost(post.shortId).executeAsOne()
|
||||||
|
|
||||||
postFromDb.submitterName shouldBe "Fake name"
|
assertThat(postFromDb.submitterName).isEqualTo("Fake name")
|
||||||
}
|
}
|
||||||
|
|
||||||
test("get post from db") {
|
@Test
|
||||||
// Get 1 post
|
fun `get post from db`() {
|
||||||
val post = createTestData(1)[0]
|
// Get 1 post
|
||||||
|
val post = createTestData(1)[0]
|
||||||
|
|
||||||
// Insert post into DB
|
// Insert post into DB
|
||||||
postQueries.insertOrReplacePost(post)
|
postQueries.insertOrReplacePost(post)
|
||||||
|
|
||||||
val postFromDb = postQueries.selectAllPosts().executeAsOne()
|
val postFromDb = postQueries.selectAllPosts().executeAsOne()
|
||||||
postFromDb.shortId shouldBe "test_id_1"
|
assertThat(postFromDb.shortId).isEqualTo("test_id_1")
|
||||||
}
|
}
|
||||||
|
|
||||||
test("get multiple posts from db") {
|
@Test
|
||||||
// Get 5 post
|
fun `get multiple posts from db`() {
|
||||||
val posts = createTestData(5)
|
// Get 5 post
|
||||||
|
val posts = createTestData(5)
|
||||||
|
|
||||||
// Insert posts into DB
|
// Insert posts into DB
|
||||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||||
|
|
||||||
val postsFromDb = postQueries.selectAllPosts().executeAsList()
|
val postsFromDb = postQueries.selectAllPosts().executeAsList()
|
||||||
|
|
||||||
// Check if all posts have correct shortId
|
// Check if all posts have correct shortId
|
||||||
postsFromDb.forEachIndexed { index, post -> post.shortId shouldBe "test_id_${index.inc()}" }
|
postsFromDb.forEachIndexed { index, post ->
|
||||||
|
assertThat(post.shortId).isEqualTo("test_id_${index.inc()}")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test("delete post") {
|
@Test
|
||||||
// Create 3 posts and insert them to DB
|
fun `delete post`() {
|
||||||
val posts = createTestData(3)
|
// Create 3 posts and insert them to DB
|
||||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
val posts = createTestData(3)
|
||||||
|
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||||
|
|
||||||
// Delete 2nd post
|
// Delete 2nd post
|
||||||
postQueries.deletePost("test_id_2")
|
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
|
// Check if size is 2, and only the correct post is deleted
|
||||||
postsFromDB shouldHaveSize 2
|
assertThat(postsFromDB).hasSize(2)
|
||||||
postsFromDB[0].shortId shouldBe "test_id_1"
|
assertThat(postsFromDB[0].shortId).isEqualTo("test_id_1")
|
||||||
postsFromDB[1].shortId shouldBe "test_id_3"
|
assertThat(postsFromDB[1].shortId).isEqualTo("test_id_3")
|
||||||
}
|
}
|
||||||
|
|
||||||
test("delete all posts") {
|
@Test
|
||||||
// Create 5 posts and insert them to DB
|
fun `delete all posts`() {
|
||||||
val posts = createTestData(5)
|
// Create 5 posts and insert them to DB
|
||||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
val posts = createTestData(5)
|
||||||
|
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||||
|
|
||||||
// Delete all posts
|
// Delete all posts
|
||||||
postQueries.deleteAllPosts()
|
postQueries.deleteAllPosts()
|
||||||
|
|
||||||
val dbPosts = postQueries.selectAllPosts().executeAsList()
|
val dbPosts = postQueries.selectAllPosts().executeAsList()
|
||||||
|
|
||||||
dbPosts.shouldBeEmpty()
|
assertThat(dbPosts).isEmpty()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createTestData(count: Int): ArrayList<SavedPost> {
|
private fun createTestData(count: Int): ArrayList<SavedPost> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue