Skip to content

Commit 70dcc7d

Browse files
committed
add json translation test case
1 parent adaa929 commit 70dcc7d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

firebase-vertexai/firebase-vertexai.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ dependencies {
7474
testImplementation("io.kotest:kotest-assertions-core-jvm:5.5.5")
7575
testImplementation("io.ktor:ktor-client-okhttp:$ktorVersion")
7676
testImplementation("io.ktor:ktor-client-mock:$ktorVersion")
77+
testImplementation("org.json:json:20240303")
7778
testImplementation(libs.androidx.test.junit)
7879
testImplementation(libs.androidx.test.runner)
7980
testImplementation(libs.junit)

firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.google.firebase.vertexai.type.ResponseStoppedException
2828
import com.google.firebase.vertexai.type.SerializationException
2929
import com.google.firebase.vertexai.type.ServerException
3030
import com.google.firebase.vertexai.type.ServiceDisabledException
31+
import com.google.firebase.vertexai.type.TextPart
3132
import com.google.firebase.vertexai.type.UnsupportedUserLocationException
3233
import com.google.firebase.vertexai.util.goldenUnaryFile
3334
import com.google.firebase.vertexai.util.shouldNotBeNullOrEmpty
@@ -37,10 +38,12 @@ import io.kotest.matchers.nulls.shouldNotBeNull
3738
import io.kotest.matchers.should
3839
import io.kotest.matchers.shouldBe
3940
import io.kotest.matchers.shouldNotBe
41+
import io.kotest.matchers.string.shouldNotBeEmpty
4042
import io.kotest.matchers.types.shouldBeInstanceOf
4143
import io.ktor.http.HttpStatusCode
4244
import kotlin.time.Duration.Companion.seconds
4345
import kotlinx.coroutines.withTimeout
46+
import org.json.JSONArray
4447
import org.junit.Test
4548

4649
internal class UnarySnapshotTests {
@@ -198,6 +201,26 @@ internal class UnarySnapshotTests {
198201
}
199202
}
200203

204+
@Test
205+
fun `properly translates json text`() =
206+
goldenUnaryFile("success-constraint-decoding-json.json") {
207+
val response = model.generateContent("prompt")
208+
209+
response.candidates.isEmpty() shouldBe false
210+
with(response.candidates.first().content.parts.first().shouldBeInstanceOf<TextPart>()) {
211+
shouldNotBeNull()
212+
val jsonArr = JSONArray(text)
213+
jsonArr.length() shouldBe 3
214+
for (i in 0 until jsonArr.length()) {
215+
with(jsonArr.getJSONObject(i)) {
216+
shouldNotBeNull()
217+
getString("name").shouldNotBeEmpty()
218+
getJSONArray("colors").length() shouldBe 5
219+
}
220+
}
221+
}
222+
}
223+
201224
@Test
202225
fun `invalid response`() =
203226
goldenUnaryFile("failure-invalid-response.json") {

0 commit comments

Comments
 (0)