Skip to content

Commit

Permalink
Update unit test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ashdavies committed Jul 14, 2024
1 parent dfe1433 commit 9172af3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions after-party/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ kotlin {
implementation(kotlin("test"))

implementation(libs.app.cash.turbine)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.kotlinx.coroutines.test)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
package io.ashdavies.gallery

import app.cash.turbine.test
import io.ashdavies.http.defaultHttpClient
import io.ashdavies.http.json
import io.ashdavies.util.randomUuid
import io.ktor.client.HttpClient
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals

private val RandomImage = "${randomUuid()}.jpg"

private fun inMemoryHttpClient(images: List<String> = emptyList()): HttpClient {
return HttpClient(inMemoryHttpClientEngine(images)) {
install(ContentNegotiation) {
json {
ignoreUnknownKeys = true
encodeDefaults = true
}
}
}
}

internal class SyncManagerTest {

@Test
fun `should request initial value`() = runTest {
val manager = SyncManager(
client = defaultHttpClient(inMemoryHttpClientEngine(listOf(RandomImage))),
client = inMemoryHttpClient(listOf(RandomImage)),
reader = { ByteReadChannel.Empty },
)

Expand All @@ -27,7 +40,7 @@ internal class SyncManagerTest {
@Test
fun `should sync image on invocation`() = runTest {
val manager = SyncManager(
client = defaultHttpClient(inMemoryHttpClientEngine(emptyList())),
client = inMemoryHttpClient(),
reader = { ByteReadChannel.Empty },
)

Expand All @@ -44,7 +57,7 @@ internal class SyncManagerTest {
@Test
fun `should put synced image without content`() = runTest {
val manager = SyncManager(
client = defaultHttpClient(inMemoryHttpClientEngine(listOf(RandomImage))),
client = inMemoryHttpClient(listOf(RandomImage)),
reader = { ByteReadChannel.Empty },
)

Expand All @@ -61,7 +74,7 @@ internal class SyncManagerTest {
@Test
fun `should include content length header`() = runTest {
val manager = SyncManager(
client = defaultHttpClient(inMemoryHttpClientEngine(emptyList())),
client = inMemoryHttpClient(),
reader = { ByteReadChannel.Empty },
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public fun httpClient(
block()
}

private fun Configuration.json(
public fun Configuration.json(
from: Json = Json.Default,
builderAction: JsonBuilder.() -> Unit,
) = json(Json(from, builderAction))
) {
json(Json(from, builderAction))
}

0 comments on commit 9172af3

Please sign in to comment.