From 7ffa225487b1632c450c34da89787050250647dd Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 5 Jun 2023 02:51:11 +0530 Subject: [PATCH] feat(database): add test for `SavedPostSerializer` --- database/build.gradle.kts | 1 + .../claw/database/SavedPostSerializerTest.kt | 61 +++++++++++++++++++ database/src/test/resources/saved_post.json | 1 + 3 files changed, 63 insertions(+) create mode 100644 database/src/test/kotlin/dev/msfjarvis/claw/database/SavedPostSerializerTest.kt create mode 100644 database/src/test/resources/saved_post.json diff --git a/database/build.gradle.kts b/database/build.gradle.kts index e99d1f91..38c23b86 100644 --- a/database/build.gradle.kts +++ b/database/build.gradle.kts @@ -37,5 +37,6 @@ dependencies { implementation(libs.kotlinx.serialization.core) testImplementation(libs.sqldelight.jvmDriver) + testImplementation(libs.kotlinx.serialization.json) addTestDependencies(project) } diff --git a/database/src/test/kotlin/dev/msfjarvis/claw/database/SavedPostSerializerTest.kt b/database/src/test/kotlin/dev/msfjarvis/claw/database/SavedPostSerializerTest.kt new file mode 100644 index 00000000..b163fd05 --- /dev/null +++ b/database/src/test/kotlin/dev/msfjarvis/claw/database/SavedPostSerializerTest.kt @@ -0,0 +1,61 @@ +/* + * Copyright © 2023 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package dev.msfjarvis.claw.database + +import com.google.common.truth.Truth.assertThat +import dev.msfjarvis.claw.database.local.SavedPost +import java.io.File +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy +import org.junit.jupiter.api.Test + +@OptIn(ExperimentalSerializationApi::class) +class SavedPostSerializerTest { + private val json = Json { + ignoreUnknownKeys = true + namingStrategy = JsonNamingStrategy.SnakeCase + } + private val text = getJson() + + @Test + fun serialize() { + val encoded = json.encodeToString(SavedPostSerializer(), SAVED_POST) + assertThat(encoded).isNotEmpty() + assertThat(encoded).isEqualTo(text) + } + + @Test + fun deserialize() { + val decoded = json.decodeFromString(SavedPostSerializer(), text) + assertThat(decoded).isEqualTo(SAVED_POST) + } + + private fun getJson(): String { + // Load the JSON response + val uri = javaClass.classLoader!!.getResource("saved_post.json") + val file = File(uri.path) + return String(file.readBytes()) + } + + private companion object { + private val SAVED_POST = + SavedPost( + title = "Fun Format Friday: You now have a super computer. What next?", + shortId = "nbigsf", + url = "", + createdAt = "2023-05-04T23:43:50.000-05:00", + commentCount = 13, + commentsUrl = "https://lobste.rs/s/nbigsf/fun_format_friday_you_now_have_super", + submitterName = "LenFalken", + submitterAvatarUrl = "/avatars/LenFalken-100.png", + tags = listOf("ask", "programming"), + description = + "

You suddenly have in your possession a super computer. What comes next? What projects are suddenly possible for you? What performance tests can you now explore?

\n", + ) + } +} diff --git a/database/src/test/resources/saved_post.json b/database/src/test/resources/saved_post.json new file mode 100644 index 00000000..be0a5e7a --- /dev/null +++ b/database/src/test/resources/saved_post.json @@ -0,0 +1 @@ +{"short_id":"nbigsf","title":"Fun Format Friday: You now have a super computer. What next?","url":"","created_at":"2023-05-04T23:43:50.000-05:00","comment_count":13,"comments_url":"https://lobste.rs/s/nbigsf/fun_format_friday_you_now_have_super","submitter_name":"LenFalken","submitter_avatar_url":"/avatars/LenFalken-100.png","tags":["ask","programming"],"description":"

You suddenly have in your possession a super computer. What comes next? What projects are suddenly possible for you? What performance tests can you now explore?

\n"} \ No newline at end of file