mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-16 18:37:02 +05:30
api: add tests for post details
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
e5ea28ea34
commit
f37590f088
2 changed files with 19 additions and 2 deletions
|
@ -5,6 +5,7 @@ import dev.msfjarvis.lobsters.util.TestUtils
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
import kotlin.test.fail
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import mockwebserver3.Dispatcher
|
import mockwebserver3.Dispatcher
|
||||||
import mockwebserver3.MockResponse
|
import mockwebserver3.MockResponse
|
||||||
|
@ -21,7 +22,6 @@ class LobstersApiTest {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val webServer = MockWebServer()
|
private val webServer = MockWebServer()
|
||||||
private val apiData = TestUtils.getJson("hottest.json")
|
|
||||||
private val moshi = Moshi.Builder().build()
|
private val moshi = Moshi.Builder().build()
|
||||||
private val okHttp = OkHttpClient.Builder().build()
|
private val okHttp = OkHttpClient.Builder().build()
|
||||||
private val retrofit =
|
private val retrofit =
|
||||||
|
@ -39,7 +39,16 @@ class LobstersApiTest {
|
||||||
webServer.dispatcher =
|
webServer.dispatcher =
|
||||||
object : Dispatcher() {
|
object : Dispatcher() {
|
||||||
override fun dispatch(request: RecordedRequest): MockResponse {
|
override fun dispatch(request: RecordedRequest): MockResponse {
|
||||||
return MockResponse().setBody(apiData).setResponseCode(200)
|
val path = requireNotNull(request.path)
|
||||||
|
return when {
|
||||||
|
path.startsWith("/hottest") ->
|
||||||
|
MockResponse().setBody(TestUtils.getJson("hottest.json")).setResponseCode(200)
|
||||||
|
path.startsWith("/s/") ->
|
||||||
|
MockResponse()
|
||||||
|
.setBody(TestUtils.getJson("post_details_d9ucpe.json"))
|
||||||
|
.setResponseCode(200)
|
||||||
|
else -> fail("'$path' unexpected")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,4 +79,11 @@ class LobstersApiTest {
|
||||||
val commentsOnlyPosts = posts.asSequence().filter { it.url.isEmpty() }.toSet()
|
val commentsOnlyPosts = posts.asSequence().filter { it.url.isEmpty() }.toSet()
|
||||||
assertEquals(2, commentsOnlyPosts.size)
|
assertEquals(2, commentsOnlyPosts.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `post details with comments`() = runBlocking {
|
||||||
|
val postDetails = apiClient.getPostDetails("d9ucpe")
|
||||||
|
assertEquals(7, postDetails.commentCount)
|
||||||
|
assertEquals(7, postDetails.comments.size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
1
api/src/test/resources/post_details_d9ucpe.json
Normal file
1
api/src/test/resources/post_details_d9ucpe.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue