mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 04:07:03 +05:30
refactor(api): update tests for EitherNet
This commit is contained in:
parent
8652d4ceaa
commit
4607a68de0
4 changed files with 46 additions and 56 deletions
|
@ -10,6 +10,7 @@ dependencies {
|
||||||
api(libs.retrofit.lib)
|
api(libs.retrofit.lib)
|
||||||
api(libs.eithernet)
|
api(libs.eithernet)
|
||||||
implementation(libs.kotlinx.serialization.core)
|
implementation(libs.kotlinx.serialization.core)
|
||||||
|
testImplementation(testFixtures(libs.eithernet))
|
||||||
testImplementation(libs.kotlinx.coroutines.core)
|
testImplementation(libs.kotlinx.coroutines.core)
|
||||||
testImplementation(kotlin("test-junit"))
|
testImplementation(kotlin("test-junit"))
|
||||||
testImplementation(libs.kotlinx.serialization.json)
|
testImplementation(libs.kotlinx.serialization.json)
|
||||||
|
|
|
@ -1,52 +1,46 @@
|
||||||
package dev.msfjarvis.claw.api
|
package dev.msfjarvis.claw.api
|
||||||
|
|
||||||
|
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.User
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertIs
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import retrofit2.Retrofit
|
|
||||||
import retrofit2.mock.MockRetrofit
|
|
||||||
import retrofit2.mock.NetworkBehavior
|
|
||||||
import retrofit2.mock.create
|
|
||||||
|
|
||||||
class ApiTest {
|
class ApiTest {
|
||||||
private val retrofit = Retrofit.Builder().baseUrl(LobstersApi.BASE_URL).build()
|
private val wrapper = ApiWrapper(newEitherNetController())
|
||||||
private val networkBehaviour = createNetworkBehaviour()
|
private val api
|
||||||
private val api =
|
get() = wrapper.api
|
||||||
MockRetrofit.Builder(retrofit)
|
|
||||||
.networkBehavior(networkBehaviour)
|
|
||||||
.build()
|
|
||||||
.create<LobstersApi>()
|
|
||||||
.let(::FakeApi)
|
|
||||||
|
|
||||||
private fun createNetworkBehaviour(): NetworkBehavior {
|
|
||||||
val behaviour = NetworkBehavior.create()
|
|
||||||
behaviour.setFailurePercent(0)
|
|
||||||
behaviour.setErrorPercent(0)
|
|
||||||
return behaviour
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `api gets correct number of items`() = runBlocking {
|
fun `api gets correct number of items`() = runBlocking {
|
||||||
val posts = api.getHottestPosts(1)
|
val posts = api.getHottestPosts(1)
|
||||||
assertEquals(25, posts.size)
|
assertIs<Success<List<LobstersPost>>>(posts)
|
||||||
|
assertEquals(25, posts.value.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `posts with no urls`() = runBlocking {
|
fun `posts with no urls`() = runBlocking {
|
||||||
val posts = api.getHottestPosts(1)
|
val posts = api.getHottestPosts(1)
|
||||||
val commentsOnlyPosts = posts.asSequence().filter { it.url.isEmpty() }.toSet()
|
assertIs<Success<List<LobstersPost>>>(posts)
|
||||||
|
val commentsOnlyPosts = posts.value.asSequence().filter { it.url.isEmpty() }.toSet()
|
||||||
assertEquals(2, commentsOnlyPosts.size)
|
assertEquals(2, commentsOnlyPosts.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `post details with comments`() = runBlocking {
|
fun `post details with comments`() = runBlocking {
|
||||||
val postDetails = api.getPostDetails("tdfoqh")
|
val postDetails = api.getPostDetails("tdfoqh")
|
||||||
assertEquals(7, postDetails.comments.size)
|
assertIs<Success<LobstersPostDetails>>(postDetails)
|
||||||
|
assertEquals(7, postDetails.value.comments.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `get user details`() = runBlocking {
|
fun `get user details`() = runBlocking {
|
||||||
val user = api.getUser("msfjarvis")
|
val user = api.getUser("msfjarvis")
|
||||||
assertEquals("msfjarvis", user.username)
|
assertIs<Success<User>>(user)
|
||||||
|
assertEquals("msfjarvis", user.value.username)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
28
api/src/test/kotlin/dev/msfjarvis/claw/api/ApiWrapper.kt
Normal file
28
api/src/test/kotlin/dev/msfjarvis/claw/api/ApiWrapper.kt
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package dev.msfjarvis.claw.api
|
||||||
|
|
||||||
|
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.User
|
||||||
|
import dev.msfjarvis.claw.util.TestUtils.getJson
|
||||||
|
import kotlinx.serialization.decodeFromString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
|
class ApiWrapper(controller: EitherNetController<LobstersApi>) {
|
||||||
|
private val json = Json { ignoreUnknownKeys = true }
|
||||||
|
private val hottest: List<LobstersPost> = json.decodeFromString(getJson("hottest.json"))
|
||||||
|
private val postDetails: LobstersPostDetails =
|
||||||
|
json.decodeFromString(getJson("post_details_tdfoqh.json"))
|
||||||
|
private val user: User = json.decodeFromString(getJson("msfjarvis.json"))
|
||||||
|
|
||||||
|
val api = controller.api
|
||||||
|
|
||||||
|
init {
|
||||||
|
controller.enqueue(LobstersApi::getHottestPosts) { success(hottest) }
|
||||||
|
controller.enqueue(LobstersApi::getNewestPosts) { success(hottest) }
|
||||||
|
controller.enqueue(LobstersApi::getPostDetails) { success(postDetails) }
|
||||||
|
controller.enqueue(LobstersApi::getUser) { success(user) }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,33 +0,0 @@
|
||||||
package dev.msfjarvis.claw.api
|
|
||||||
|
|
||||||
import dev.msfjarvis.claw.model.LobstersPost
|
|
||||||
import dev.msfjarvis.claw.model.LobstersPostDetails
|
|
||||||
import dev.msfjarvis.claw.model.User
|
|
||||||
import dev.msfjarvis.claw.util.TestUtils.getJson
|
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import retrofit2.mock.BehaviorDelegate
|
|
||||||
|
|
||||||
class FakeApi(private val delegate: BehaviorDelegate<LobstersApi>) : LobstersApi {
|
|
||||||
private val json = Json { ignoreUnknownKeys = true }
|
|
||||||
private val hottest: List<LobstersPost> = json.decodeFromString(getJson("hottest.json"))
|
|
||||||
private val postDetails: LobstersPostDetails =
|
|
||||||
json.decodeFromString(getJson("post_details_tdfoqh.json"))
|
|
||||||
private val user: User = json.decodeFromString(getJson("msfjarvis.json"))
|
|
||||||
|
|
||||||
override suspend fun getHottestPosts(page: Int): List<LobstersPost> {
|
|
||||||
return delegate.returningResponse(hottest).getHottestPosts(page)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getNewestPosts(page: Int): List<LobstersPost> {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getPostDetails(postId: String): LobstersPostDetails {
|
|
||||||
return delegate.returningResponse(postDetails).getPostDetails(postId)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getUser(username: String): User {
|
|
||||||
return delegate.returningResponse(user).getUser(username)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue