From ae66aced830f83277482a5893866c0816241228d Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 4 May 2023 03:33:33 +0530 Subject: [PATCH] feat(api): migrate away from kotest --- api/build.gradle.kts | 10 +- .../kotlin/dev/msfjarvis/claw/api/ApiTest.kt | 94 ++++++++++--------- .../dev/msfjarvis/claw/api/ApiWrapper.kt | 16 +++- 3 files changed, 72 insertions(+), 48 deletions(-) diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 0930a5c3..d598a4c2 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -24,8 +24,14 @@ dependencies { implementation(libs.javax.inject) testImplementation(testFixtures(libs.eithernet)) - 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.test) testImplementation(libs.kotlinx.serialization.json) testImplementation(libs.retrofit.kotlinxSerializationConverter) } diff --git a/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiTest.kt b/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiTest.kt index 8d6c8bd4..0444986b 100644 --- a/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiTest.kt +++ b/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiTest.kt @@ -6,65 +6,71 @@ */ package dev.msfjarvis.claw.api +import com.google.common.truth.Truth.assertThat import com.slack.eithernet.ApiResult.Success import com.slack.eithernet.test.newEitherNetController import dev.msfjarvis.claw.model.LobstersPost import dev.msfjarvis.claw.model.LobstersPostDetails import dev.msfjarvis.claw.model.Tags import dev.msfjarvis.claw.model.User -import io.kotest.core.spec.style.FunSpec -import io.kotest.matchers.collections.shouldHaveSize -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeTypeOf +import dev.msfjarvis.claw.util.TestUtils.assertIs +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test -class ApiTest : FunSpec() { +@OptIn(ExperimentalCoroutinesApi::class) +class ApiTest { private val wrapper = ApiWrapper(newEitherNetController()) private val api get() = wrapper.api - init { - test("api gets correct number of items") { - val posts = api.getHottestPosts(1) - posts.shouldBeTypeOf>>() - posts.value shouldHaveSize 25 - } + @Test + fun `api gets correct number of items`() = runTest { + val posts = api.getHottestPosts(1) + assertIs>>(posts) + assertThat(posts.value).hasSize(25) + } - test("posts with no urls") { - val posts = api.getHottestPosts(1) - posts.shouldBeTypeOf>>() - val commentsOnlyPosts = posts.value.asSequence().filter { it.url.isEmpty() }.toSet() - commentsOnlyPosts shouldHaveSize 2 - } + @Test + fun `posts with no urls`() = runTest { + val posts = api.getHottestPosts(1) + assertIs>>(posts) + val commentsOnlyPosts = posts.value.asSequence().filter { it.url.isEmpty() }.toSet() + assertThat(commentsOnlyPosts).hasSize(2) + } - test("post details with comments") { - val postDetails = api.getPostDetails("tdfoqh") - postDetails.shouldBeTypeOf>() - postDetails.value.comments shouldHaveSize 7 - } + @Test + fun `post details with comments`() = runTest { + val postDetails = api.getPostDetails("tdfoqh") + assertIs>(postDetails) + assertThat(postDetails.value.comments).hasSize(7) + } - test("get user details") { - val user = api.getUser("msfjarvis") - user.shouldBeTypeOf>() - user.value.username shouldBe "msfjarvis" - } + @Test + fun `get user details`() = runTest { + val user = api.getUser("msfjarvis") + assertIs>(user) + assertThat(user.value.username).isEqualTo("msfjarvis") + } - test("get posts by single tag") { - var tags = Tags() - tags.addTag("meta") - val posts = api.getPostsByTags(tags, 1) - posts.shouldBeTypeOf>>() - posts.value shouldHaveSize 25 - posts.value[0].tags.contains("meta") - } + @Test + fun `get posts by single tag`() = runTest { + val tags = Tags() + tags.addTag("meta") + val posts = api.getPostsByTags(tags, 1) + assertIs>>(posts) + assertThat(posts.value).hasSize(25) + assertThat(posts.value[0].tags).contains("meta") + } - test("get posts by multiple tags") { - var tags = Tags() - tags.addTag("programming") - tags.addTag("rust") - val posts = api.getPostsByTags(tags, 1) - posts.shouldBeTypeOf>>() - posts.value shouldHaveSize 25 - posts.value[0].tags.contains("programming") or posts.value[0].tags.contains("rust") - } + @Test + fun `get posts by multiple tags`() = runTest { + val tags = Tags() + tags.addTag("programming") + tags.addTag("rust") + val posts = api.getPostsByTags(tags, 1) + assertIs>>(posts) + assertThat(posts.value).hasSize(25) + assertThat(posts.value[0].tags).containsAnyOf("programming", "rust") } } diff --git a/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiWrapper.kt b/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiWrapper.kt index bef9288a..4880ea8a 100644 --- a/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiWrapper.kt +++ b/api/src/test/kotlin/dev/msfjarvis/claw/api/ApiWrapper.kt @@ -6,11 +6,13 @@ */ package dev.msfjarvis.claw.api +import com.slack.eithernet.ApiResult.Companion.httpFailure import com.slack.eithernet.ApiResult.Companion.success import com.slack.eithernet.test.EitherNetController import com.slack.eithernet.test.enqueue import dev.msfjarvis.claw.model.LobstersPost import dev.msfjarvis.claw.model.LobstersPostDetails +import dev.msfjarvis.claw.model.Tags import dev.msfjarvis.claw.model.User import dev.msfjarvis.claw.util.TestUtils.getJson import kotlinx.serialization.ExperimentalSerializationApi @@ -31,6 +33,16 @@ class ApiWrapper(controller: EitherNetController) { private val metaPosts: List = json.decodeFromString(getJson("meta.json")) private val programmingRustPosts: List = json.decodeFromString(getJson("programming_rust.json")) + private val getPostsBody = { args: Array -> + val tags = args[0] as Tags + if ("meta" in tags) { + success(metaPosts) + } else if ("programming" in tags && "rust" in tags) { + success(programmingRustPosts) + } else { + httpFailure(400) + } + } val api = controller.api @@ -39,7 +51,7 @@ class ApiWrapper(controller: EitherNetController) { controller.enqueue(LobstersApi::getHottestPosts) { success(hottest) } controller.enqueue(LobstersApi::getPostDetails) { success(postDetails) } controller.enqueue(LobstersApi::getUser) { success(user) } - controller.enqueue(LobstersApi::getPostsByTags) { success(metaPosts) } - controller.enqueue(LobstersApi::getPostsByTags) { success(programmingRustPosts) } + controller.enqueue(LobstersApi::getPostsByTags, getPostsBody) + controller.enqueue(LobstersApi::getPostsByTags, getPostsBody) } }