feat(api): migrate away from kotest

This commit is contained in:
Harsh Shandilya 2023-05-04 03:33:33 +05:30
parent 5d2eebf093
commit ae66aced83
No known key found for this signature in database
3 changed files with 72 additions and 48 deletions

View file

@ -24,8 +24,14 @@ dependencies {
implementation(libs.javax.inject) implementation(libs.javax.inject)
testImplementation(testFixtures(libs.eithernet)) testImplementation(testFixtures(libs.eithernet))
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.test)
testImplementation(libs.kotlinx.serialization.json) testImplementation(libs.kotlinx.serialization.json)
testImplementation(libs.retrofit.kotlinxSerializationConverter) testImplementation(libs.retrofit.kotlinxSerializationConverter)
} }

View file

@ -6,65 +6,71 @@
*/ */
package dev.msfjarvis.claw.api package dev.msfjarvis.claw.api
import com.google.common.truth.Truth.assertThat
import com.slack.eithernet.ApiResult.Success import com.slack.eithernet.ApiResult.Success
import com.slack.eithernet.test.newEitherNetController import com.slack.eithernet.test.newEitherNetController
import dev.msfjarvis.claw.model.LobstersPost import dev.msfjarvis.claw.model.LobstersPost
import dev.msfjarvis.claw.model.LobstersPostDetails import dev.msfjarvis.claw.model.LobstersPostDetails
import dev.msfjarvis.claw.model.Tags import dev.msfjarvis.claw.model.Tags
import dev.msfjarvis.claw.model.User import dev.msfjarvis.claw.model.User
import io.kotest.core.spec.style.FunSpec import dev.msfjarvis.claw.util.TestUtils.assertIs
import io.kotest.matchers.collections.shouldHaveSize import kotlinx.coroutines.ExperimentalCoroutinesApi
import io.kotest.matchers.shouldBe import kotlinx.coroutines.test.runTest
import io.kotest.matchers.types.shouldBeTypeOf import org.junit.jupiter.api.Test
class ApiTest : FunSpec() { @OptIn(ExperimentalCoroutinesApi::class)
class ApiTest {
private val wrapper = ApiWrapper(newEitherNetController()) private val wrapper = ApiWrapper(newEitherNetController())
private val api private val api
get() = wrapper.api get() = wrapper.api
init { @Test
test("api gets correct number of items") { fun `api gets correct number of items`() = runTest {
val posts = api.getHottestPosts(1) val posts = api.getHottestPosts(1)
posts.shouldBeTypeOf<Success<List<LobstersPost>>>() assertIs<Success<List<LobstersPost>>>(posts)
posts.value shouldHaveSize 25 assertThat(posts.value).hasSize(25)
} }
test("posts with no urls") { @Test
val posts = api.getHottestPosts(1) fun `posts with no urls`() = runTest {
posts.shouldBeTypeOf<Success<List<LobstersPost>>>() val posts = api.getHottestPosts(1)
val commentsOnlyPosts = posts.value.asSequence().filter { it.url.isEmpty() }.toSet() assertIs<Success<List<LobstersPost>>>(posts)
commentsOnlyPosts shouldHaveSize 2 val commentsOnlyPosts = posts.value.asSequence().filter { it.url.isEmpty() }.toSet()
} assertThat(commentsOnlyPosts).hasSize(2)
}
test("post details with comments") { @Test
val postDetails = api.getPostDetails("tdfoqh") fun `post details with comments`() = runTest {
postDetails.shouldBeTypeOf<Success<LobstersPostDetails>>() val postDetails = api.getPostDetails("tdfoqh")
postDetails.value.comments shouldHaveSize 7 assertIs<Success<LobstersPostDetails>>(postDetails)
} assertThat(postDetails.value.comments).hasSize(7)
}
test("get user details") { @Test
val user = api.getUser("msfjarvis") fun `get user details`() = runTest {
user.shouldBeTypeOf<Success<User>>() val user = api.getUser("msfjarvis")
user.value.username shouldBe "msfjarvis" assertIs<Success<User>>(user)
} assertThat(user.value.username).isEqualTo("msfjarvis")
}
test("get posts by single tag") { @Test
var tags = Tags() fun `get posts by single tag`() = runTest {
tags.addTag("meta") val tags = Tags()
val posts = api.getPostsByTags(tags, 1) tags.addTag("meta")
posts.shouldBeTypeOf<Success<List<LobstersPost>>>() val posts = api.getPostsByTags(tags, 1)
posts.value shouldHaveSize 25 assertIs<Success<List<LobstersPost>>>(posts)
posts.value[0].tags.contains("meta") assertThat(posts.value).hasSize(25)
} assertThat(posts.value[0].tags).contains("meta")
}
test("get posts by multiple tags") { @Test
var tags = Tags() fun `get posts by multiple tags`() = runTest {
tags.addTag("programming") val tags = Tags()
tags.addTag("rust") tags.addTag("programming")
val posts = api.getPostsByTags(tags, 1) tags.addTag("rust")
posts.shouldBeTypeOf<Success<List<LobstersPost>>>() val posts = api.getPostsByTags(tags, 1)
posts.value shouldHaveSize 25 assertIs<Success<List<LobstersPost>>>(posts)
posts.value[0].tags.contains("programming") or posts.value[0].tags.contains("rust") assertThat(posts.value).hasSize(25)
} assertThat(posts.value[0].tags).containsAnyOf("programming", "rust")
} }
} }

View file

@ -6,11 +6,13 @@
*/ */
package dev.msfjarvis.claw.api package dev.msfjarvis.claw.api
import com.slack.eithernet.ApiResult.Companion.httpFailure
import com.slack.eithernet.ApiResult.Companion.success import com.slack.eithernet.ApiResult.Companion.success
import com.slack.eithernet.test.EitherNetController import com.slack.eithernet.test.EitherNetController
import com.slack.eithernet.test.enqueue import com.slack.eithernet.test.enqueue
import dev.msfjarvis.claw.model.LobstersPost import dev.msfjarvis.claw.model.LobstersPost
import dev.msfjarvis.claw.model.LobstersPostDetails import dev.msfjarvis.claw.model.LobstersPostDetails
import dev.msfjarvis.claw.model.Tags
import dev.msfjarvis.claw.model.User import dev.msfjarvis.claw.model.User
import dev.msfjarvis.claw.util.TestUtils.getJson import dev.msfjarvis.claw.util.TestUtils.getJson
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
@ -31,6 +33,16 @@ class ApiWrapper(controller: EitherNetController<LobstersApi>) {
private val metaPosts: List<LobstersPost> = json.decodeFromString(getJson("meta.json")) private val metaPosts: List<LobstersPost> = json.decodeFromString(getJson("meta.json"))
private val programmingRustPosts: List<LobstersPost> = private val programmingRustPosts: List<LobstersPost> =
json.decodeFromString(getJson("programming_rust.json")) json.decodeFromString(getJson("programming_rust.json"))
private val getPostsBody = { args: Array<Any> ->
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 val api = controller.api
@ -39,7 +51,7 @@ class ApiWrapper(controller: EitherNetController<LobstersApi>) {
controller.enqueue(LobstersApi::getHottestPosts) { success(hottest) } controller.enqueue(LobstersApi::getHottestPosts) { success(hottest) }
controller.enqueue(LobstersApi::getPostDetails) { success(postDetails) } controller.enqueue(LobstersApi::getPostDetails) { success(postDetails) }
controller.enqueue(LobstersApi::getUser) { success(user) } controller.enqueue(LobstersApi::getUser) { success(user) }
controller.enqueue(LobstersApi::getPostsByTags) { success(metaPosts) } controller.enqueue(LobstersApi::getPostsByTags, getPostsBody)
controller.enqueue(LobstersApi::getPostsByTags) { success(programmingRustPosts) } controller.enqueue(LobstersApi::getPostsByTags, getPostsBody)
} }
} }