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)
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)
}

View file

@ -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") {
@Test
fun `api gets correct number of items`() = runTest {
val posts = api.getHottestPosts(1)
posts.shouldBeTypeOf<Success<List<LobstersPost>>>()
posts.value shouldHaveSize 25
assertIs<Success<List<LobstersPost>>>(posts)
assertThat(posts.value).hasSize(25)
}
test("posts with no urls") {
@Test
fun `posts with no urls`() = runTest {
val posts = api.getHottestPosts(1)
posts.shouldBeTypeOf<Success<List<LobstersPost>>>()
assertIs<Success<List<LobstersPost>>>(posts)
val commentsOnlyPosts = posts.value.asSequence().filter { it.url.isEmpty() }.toSet()
commentsOnlyPosts shouldHaveSize 2
assertThat(commentsOnlyPosts).hasSize(2)
}
test("post details with comments") {
@Test
fun `post details with comments`() = runTest {
val postDetails = api.getPostDetails("tdfoqh")
postDetails.shouldBeTypeOf<Success<LobstersPostDetails>>()
postDetails.value.comments shouldHaveSize 7
assertIs<Success<LobstersPostDetails>>(postDetails)
assertThat(postDetails.value.comments).hasSize(7)
}
test("get user details") {
@Test
fun `get user details`() = runTest {
val user = api.getUser("msfjarvis")
user.shouldBeTypeOf<Success<User>>()
user.value.username shouldBe "msfjarvis"
assertIs<Success<User>>(user)
assertThat(user.value.username).isEqualTo("msfjarvis")
}
test("get posts by single tag") {
var tags = Tags()
@Test
fun `get posts by single tag`() = runTest {
val tags = Tags()
tags.addTag("meta")
val posts = api.getPostsByTags(tags, 1)
posts.shouldBeTypeOf<Success<List<LobstersPost>>>()
posts.value shouldHaveSize 25
posts.value[0].tags.contains("meta")
assertIs<Success<List<LobstersPost>>>(posts)
assertThat(posts.value).hasSize(25)
assertThat(posts.value[0].tags).contains("meta")
}
test("get posts by multiple tags") {
var tags = Tags()
@Test
fun `get posts by multiple tags`() = runTest {
val tags = Tags()
tags.addTag("programming")
tags.addTag("rust")
val posts = api.getPostsByTags(tags, 1)
posts.shouldBeTypeOf<Success<List<LobstersPost>>>()
posts.value shouldHaveSize 25
posts.value[0].tags.contains("programming") or posts.value[0].tags.contains("rust")
}
assertIs<Success<List<LobstersPost>>>(posts)
assertThat(posts.value).hasSize(25)
assertThat(posts.value[0].tags).containsAnyOf("programming", "rust")
}
}

View file

@ -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<LobstersApi>) {
private val metaPosts: List<LobstersPost> = json.decodeFromString(getJson("meta.json"))
private val programmingRustPosts: List<LobstersPost> =
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
@ -39,7 +51,7 @@ class ApiWrapper(controller: EitherNetController<LobstersApi>) {
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)
}
}