|
| 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 | +package com.google.firebase.dataconnect |
| 18 | + |
| 19 | +import com.google.firebase.dataconnect.testutil.DataConnectIntegrationTestBase |
| 20 | +import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect |
| 21 | +import com.google.firebase.dataconnect.testutil.schemas.AllTypesSchema |
| 22 | +import io.kotest.matchers.shouldBe |
| 23 | +import io.kotest.property.Arb |
| 24 | +import io.kotest.property.arbitrary.next |
| 25 | +import kotlinx.coroutines.test.runTest |
| 26 | +import kotlinx.serialization.Serializable |
| 27 | +import kotlinx.serialization.serializer |
| 28 | +import org.junit.Test |
| 29 | + |
| 30 | +class UnknownKeysIntegrationTest : DataConnectIntegrationTestBase() { |
| 31 | + |
| 32 | + private val allTypesSchema by lazy { AllTypesSchema(dataConnectFactory) } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun unknownKeysInQueryResponseDataShouldBeIgnored() = runTest { |
| 36 | + val id = Arb.dataConnect.uuid().next() |
| 37 | + allTypesSchema |
| 38 | + .createPrimitive( |
| 39 | + AllTypesSchema.PrimitiveData( |
| 40 | + id = id, |
| 41 | + idFieldNullable = "0d2fcdf1c4a84c64a87f3c7932b31749", |
| 42 | + intField = 42, |
| 43 | + intFieldNullable = 43, |
| 44 | + floatField = 123.45, |
| 45 | + floatFieldNullable = 678.91, |
| 46 | + booleanField = true, |
| 47 | + booleanFieldNullable = false, |
| 48 | + stringField = "TestString", |
| 49 | + stringFieldNullable = "TestNullableString" |
| 50 | + ) |
| 51 | + ) |
| 52 | + .execute() |
| 53 | + |
| 54 | + @Serializable |
| 55 | + data class PrimitiveQueryDataValues( |
| 56 | + val intFieldNullable: Int?, |
| 57 | + val booleanField: Boolean, |
| 58 | + val booleanFieldNullable: Boolean?, |
| 59 | + val stringField: String, |
| 60 | + val stringFieldNullable: String?, |
| 61 | + ) |
| 62 | + |
| 63 | + /** An adaptation of [AllTypesSchema.GetPrimitiveQuery.Data] with some keys missing. */ |
| 64 | + @Serializable |
| 65 | + data class PrimitiveQueryDataMissingSomeKeys(val primitive: PrimitiveQueryDataValues) |
| 66 | + |
| 67 | + @OptIn(ExperimentalFirebaseDataConnect::class) |
| 68 | + val result = |
| 69 | + allTypesSchema |
| 70 | + .getPrimitive(id = id) |
| 71 | + .withDataDeserializer(serializer<PrimitiveQueryDataMissingSomeKeys>()) |
| 72 | + .execute() |
| 73 | + |
| 74 | + result.data.primitive shouldBe |
| 75 | + PrimitiveQueryDataValues( |
| 76 | + intFieldNullable = 43, |
| 77 | + booleanField = true, |
| 78 | + booleanFieldNullable = false, |
| 79 | + stringField = "TestString", |
| 80 | + stringFieldNullable = "TestNullableString" |
| 81 | + ) |
| 82 | + } |
| 83 | +} |
0 commit comments