|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +@file:OptIn(ExperimentalFirebaseDataConnect::class) |
| 18 | + |
| 19 | +package com.google.firebase.dataconnect |
| 20 | + |
| 21 | +import com.google.firebase.dataconnect.testutil.DataConnectIntegrationTestBase |
| 22 | +import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect |
| 23 | +import com.google.firebase.dataconnect.testutil.schemas.AllTypesSchema |
| 24 | +import com.google.firebase.dataconnect.testutil.schemas.PersonSchema |
| 25 | +import io.kotest.assertions.withClue |
| 26 | +import io.kotest.matchers.shouldBe |
| 27 | +import io.kotest.property.Arb |
| 28 | +import io.kotest.property.arbitrary.next |
| 29 | +import kotlinx.coroutines.test.runTest |
| 30 | +import kotlinx.serialization.Serializable |
| 31 | +import kotlinx.serialization.serializer |
| 32 | +import org.junit.Test |
| 33 | + |
| 34 | +class UnknownKeysIntegrationTest : DataConnectIntegrationTestBase() { |
| 35 | + |
| 36 | + private val personSchema by lazy { PersonSchema(dataConnectFactory) } |
| 37 | + private val allTypesSchema by lazy { AllTypesSchema(dataConnectFactory) } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun unknownKeysInQueryResponseDataShouldBeIgnored() = runTest { |
| 41 | + val id = Arb.dataConnect.uuid().next() |
| 42 | + allTypesSchema |
| 43 | + .createPrimitive( |
| 44 | + AllTypesSchema.PrimitiveData( |
| 45 | + id = id, |
| 46 | + idFieldNullable = "0d2fcdf1c4a84c64a87f3c7932b31749", |
| 47 | + intField = 42, |
| 48 | + intFieldNullable = 43, |
| 49 | + floatField = 123.45, |
| 50 | + floatFieldNullable = 678.91, |
| 51 | + booleanField = true, |
| 52 | + booleanFieldNullable = false, |
| 53 | + stringField = "TestString", |
| 54 | + stringFieldNullable = "TestNullableString" |
| 55 | + ) |
| 56 | + ) |
| 57 | + .execute() |
| 58 | + |
| 59 | + @Serializable |
| 60 | + data class PrimitiveQueryDataValues( |
| 61 | + val intFieldNullable: Int?, |
| 62 | + val booleanField: Boolean, |
| 63 | + val booleanFieldNullable: Boolean?, |
| 64 | + val stringField: String, |
| 65 | + val stringFieldNullable: String?, |
| 66 | + ) |
| 67 | + |
| 68 | + /** An adaptation of [AllTypesSchema.GetPrimitiveQuery.Data] with some keys missing. */ |
| 69 | + @Serializable |
| 70 | + data class PrimitiveQueryDataMissingSomeKeys(val primitive: PrimitiveQueryDataValues) |
| 71 | + |
| 72 | + val result = |
| 73 | + allTypesSchema |
| 74 | + .getPrimitive(id = id) |
| 75 | + .withDataDeserializer(serializer<PrimitiveQueryDataMissingSomeKeys>()) |
| 76 | + .execute() |
| 77 | + |
| 78 | + result.data.primitive shouldBe |
| 79 | + PrimitiveQueryDataValues( |
| 80 | + intFieldNullable = 43, |
| 81 | + booleanField = true, |
| 82 | + booleanFieldNullable = false, |
| 83 | + stringField = "TestString", |
| 84 | + stringFieldNullable = "TestNullableString" |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun unknownKeysInMutationResponseDataShouldBeIgnored() = runTest { |
| 90 | + @Serializable data class PersonKeyWithId(val id: String) |
| 91 | + val person1 = PersonKeyWithId(id = "8a218894521f457a9be45a0986494058") |
| 92 | + val person4 = PersonKeyWithId(id = "ef8de2a4a6de400e94d555f148b643c0") |
| 93 | + val mutationRef = |
| 94 | + personSchema.dataConnect.mutation("create5People", Unit, serializer<Unit>(), serializer()) |
| 95 | + |
| 96 | + // Precondition check: Verify that the response contains person1..person5 and the expected IDs. |
| 97 | + withClue("precondition check") { |
| 98 | + @Serializable |
| 99 | + data class Create5PeopleData( |
| 100 | + val person1: PersonKeyWithId, |
| 101 | + val person3: PersonKeyWithId, |
| 102 | + val person2: PersonKeyWithId, |
| 103 | + val person4: PersonKeyWithId, |
| 104 | + val person5: PersonKeyWithId, |
| 105 | + ) |
| 106 | + |
| 107 | + val mutationResult = |
| 108 | + mutationRef.withDataDeserializer(serializer<Create5PeopleData>()).execute() |
| 109 | + mutationResult.data shouldBe |
| 110 | + Create5PeopleData( |
| 111 | + person1 = person1, |
| 112 | + person2 = PersonKeyWithId(id = "464443371f284194be4b2e78c3ef000c"), |
| 113 | + person3 = PersonKeyWithId(id = "903d83db81754bd29860458f127ef124"), |
| 114 | + person4 = person4, |
| 115 | + person5 = PersonKeyWithId(id = "8584fd7ca2b6453da18d21d4341f1804"), |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + withClue("actual test") { |
| 120 | + // Create5PeopleDataWithMissingKeys is missing "person2.id", "person3", and "person5" which |
| 121 | + // will be present in the response. |
| 122 | + @Serializable data class PersonKeyWithoutId(val foo: Nothing? = null) |
| 123 | + @Serializable |
| 124 | + data class Create5PeopleDataWithMissingKeys( |
| 125 | + val person1: PersonKeyWithId, |
| 126 | + val person2: PersonKeyWithoutId, |
| 127 | + val person4: PersonKeyWithId, |
| 128 | + ) |
| 129 | + |
| 130 | + val mutationResult = |
| 131 | + mutationRef.withDataDeserializer(serializer<Create5PeopleDataWithMissingKeys>()).execute() |
| 132 | + mutationResult.data shouldBe |
| 133 | + Create5PeopleDataWithMissingKeys( |
| 134 | + person1 = person1, |
| 135 | + person2 = PersonKeyWithoutId(), |
| 136 | + person4 = PersonKeyWithId(id = "ef8de2a4a6de400e94d555f148b643c0"), |
| 137 | + ) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments