|
| 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.schemas.PersonSchema |
| 21 | +import com.google.firebase.dataconnect.testutil.schemas.PersonSchema.CreatePersonMutation |
| 22 | +import com.google.firebase.dataconnect.testutil.schemas.PersonSchema.GetPersonQuery |
| 23 | +import com.google.firebase.dataconnect.testutil.shouldSatisfy |
| 24 | +import io.kotest.assertions.throwables.shouldThrow |
| 25 | +import io.kotest.matchers.collections.shouldHaveAtLeastSize |
| 26 | +import io.kotest.property.Arb |
| 27 | +import io.kotest.property.arbitrary.map |
| 28 | +import io.kotest.property.arbitrary.next |
| 29 | +import kotlinx.coroutines.test.runTest |
| 30 | +import kotlinx.serialization.Serializable |
| 31 | +import kotlinx.serialization.SerializationException |
| 32 | +import kotlinx.serialization.serializer |
| 33 | +import org.junit.Test |
| 34 | + |
| 35 | +class OperationExecutionErrorsIntegrationTest : DataConnectIntegrationTestBase() { |
| 36 | + |
| 37 | + private val personSchema: PersonSchema by lazy { PersonSchema(dataConnectFactory) } |
| 38 | + private val dataConnect: FirebaseDataConnect by lazy { personSchema.dataConnect } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun executeQueryFailsWithNullDataNonEmptyErrors() = runTest { |
| 42 | + val queryRef = |
| 43 | + dataConnect.query( |
| 44 | + operationName = GetPersonQuery.operationName, |
| 45 | + variables = Arb.incompatibleVariables().next(rs), |
| 46 | + dataDeserializer = serializer<GetPersonQuery.Data>(), |
| 47 | + variablesSerializer = serializer(), |
| 48 | + optionsBuilder = {}, |
| 49 | + ) |
| 50 | + |
| 51 | + val exception = shouldThrow<DataConnectOperationException> { queryRef.execute() } |
| 52 | + |
| 53 | + exception.shouldSatisfy( |
| 54 | + expectedMessageSubstringCaseInsensitive = "operation encountered errors", |
| 55 | + expectedMessageSubstringCaseSensitive = "jwdbzka4k5", |
| 56 | + expectedCause = null, |
| 57 | + expectedRawData = null, |
| 58 | + expectedData = null, |
| 59 | + errorsValidator = { it.shouldHaveAtLeastSize(1) }, |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun executeMutationFailsWithNullDataNonEmptyErrors() = runTest { |
| 65 | + val mutationRef = |
| 66 | + dataConnect.mutation( |
| 67 | + operationName = CreatePersonMutation.operationName, |
| 68 | + variables = Arb.incompatibleVariables().next(rs), |
| 69 | + dataDeserializer = serializer<CreatePersonMutation.Data>(), |
| 70 | + variablesSerializer = serializer(), |
| 71 | + optionsBuilder = {}, |
| 72 | + ) |
| 73 | + |
| 74 | + val exception = shouldThrow<DataConnectOperationException> { mutationRef.execute() } |
| 75 | + |
| 76 | + exception.shouldSatisfy( |
| 77 | + expectedMessageSubstringCaseInsensitive = "operation encountered errors", |
| 78 | + expectedCause = null, |
| 79 | + expectedRawData = null, |
| 80 | + expectedData = null, |
| 81 | + errorsValidator = { it.shouldHaveAtLeastSize(1) }, |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun executeQueryFailsWithNonNullDataEmptyErrorsButDecodingResponseDataFails() = runTest { |
| 87 | + val id = Arb.alphanumericString().next() |
| 88 | + val queryRef = |
| 89 | + dataConnect.query( |
| 90 | + operationName = GetPersonQuery.operationName, |
| 91 | + variables = GetPersonQuery.Variables(id), |
| 92 | + dataDeserializer = serializer<IncompatibleData>(), |
| 93 | + variablesSerializer = serializer(), |
| 94 | + optionsBuilder = {}, |
| 95 | + ) |
| 96 | + |
| 97 | + val exception = shouldThrow<DataConnectOperationException> { queryRef.execute() } |
| 98 | + |
| 99 | + exception.shouldSatisfy( |
| 100 | + expectedMessageSubstringCaseInsensitive = "decoding data from the server's response failed", |
| 101 | + expectedCause = SerializationException::class, |
| 102 | + expectedRawData = mapOf("person" to null), |
| 103 | + expectedData = null, |
| 104 | + expectedErrors = emptyList(), |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun executeMutationFailsWithNonNullDataEmptyErrorsButDecodingResponseDataFails() = runTest { |
| 110 | + val id = Arb.alphanumericString().next() |
| 111 | + val name = Arb.alphanumericString().next() |
| 112 | + val mutationRef = |
| 113 | + dataConnect.mutation( |
| 114 | + operationName = CreatePersonMutation.operationName, |
| 115 | + variables = CreatePersonMutation.Variables(id, name), |
| 116 | + dataDeserializer = serializer<IncompatibleData>(), |
| 117 | + variablesSerializer = serializer(), |
| 118 | + optionsBuilder = {}, |
| 119 | + ) |
| 120 | + |
| 121 | + val exception = shouldThrow<DataConnectOperationException> { mutationRef.execute() } |
| 122 | + |
| 123 | + exception.shouldSatisfy( |
| 124 | + expectedMessageSubstringCaseInsensitive = "decoding data from the server's response failed", |
| 125 | + expectedCause = SerializationException::class, |
| 126 | + expectedRawData = mapOf("person_insert" to mapOf("id" to id)), |
| 127 | + expectedData = null, |
| 128 | + expectedErrors = emptyList(), |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun executeQueryFailsWithNonNullDataNonEmptyErrorsDecodingSucceeds() = runTest { |
| 134 | + val id = Arb.alphanumericString().next() |
| 135 | + val name = Arb.alphanumericString().next() |
| 136 | + personSchema.createPerson(CreatePersonMutation.Variables(id, name)).execute() |
| 137 | + val queryRef = |
| 138 | + dataConnect.query( |
| 139 | + operationName = "getPersonWithPartialFailure", |
| 140 | + variables = GetPersonWithPartialFailureVariables(id), |
| 141 | + dataDeserializer = serializer<GetPersonWithPartialFailureData>(), |
| 142 | + variablesSerializer = serializer(), |
| 143 | + optionsBuilder = {}, |
| 144 | + ) |
| 145 | + |
| 146 | + val exception = shouldThrow<DataConnectOperationException> { queryRef.execute() } |
| 147 | + |
| 148 | + exception.shouldSatisfy( |
| 149 | + expectedMessageSubstringCaseInsensitive = "operation encountered errors", |
| 150 | + expectedMessageSubstringCaseSensitive = "c8azjdwz2x", |
| 151 | + expectedCause = null, |
| 152 | + expectedRawData = mapOf("person1" to mapOf("name" to name), "person2" to null), |
| 153 | + expectedData = GetPersonWithPartialFailureData(name), |
| 154 | + errorsValidator = { it.shouldHaveAtLeastSize(1) }, |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + fun executeMutationFailsWithNonNullDataNonEmptyErrorsDecodingSucceeds() = runTest { |
| 160 | + val id = Arb.alphanumericString().next() |
| 161 | + val name = Arb.alphanumericString().next() |
| 162 | + val mutationRef = |
| 163 | + dataConnect.mutation( |
| 164 | + operationName = "createPersonWithPartialFailure", |
| 165 | + variables = CreatePersonWithPartialFailureVariables(id = id, name = name), |
| 166 | + dataDeserializer = serializer<CreatePersonWithPartialFailureData>(), |
| 167 | + variablesSerializer = serializer(), |
| 168 | + optionsBuilder = {}, |
| 169 | + ) |
| 170 | + |
| 171 | + val exception = shouldThrow<DataConnectOperationException> { mutationRef.execute() } |
| 172 | + |
| 173 | + exception.shouldSatisfy( |
| 174 | + expectedMessageSubstringCaseInsensitive = "operation encountered errors", |
| 175 | + expectedMessageSubstringCaseSensitive = "ecxpjy4qfy", |
| 176 | + expectedCause = null, |
| 177 | + expectedRawData = mapOf("person1" to mapOf("id" to id), "person2" to null), |
| 178 | + expectedData = CreatePersonWithPartialFailureData(id), |
| 179 | + errorsValidator = { it.shouldHaveAtLeastSize(1) }, |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + fun executeQueryFailsWithNonNullDataNonEmptyErrorsDecodingFails() = runTest { |
| 185 | + val id = Arb.alphanumericString().next() |
| 186 | + val name = Arb.alphanumericString().next() |
| 187 | + personSchema.createPerson(CreatePersonMutation.Variables(id, name)).execute() |
| 188 | + val queryRef = |
| 189 | + dataConnect.query( |
| 190 | + operationName = "getPersonWithPartialFailure", |
| 191 | + variables = GetPersonWithPartialFailureVariables(id), |
| 192 | + dataDeserializer = serializer<IncompatibleData>(), |
| 193 | + variablesSerializer = serializer(), |
| 194 | + optionsBuilder = {}, |
| 195 | + ) |
| 196 | + |
| 197 | + val exception = shouldThrow<DataConnectOperationException> { queryRef.execute() } |
| 198 | + |
| 199 | + exception.shouldSatisfy( |
| 200 | + expectedMessageSubstringCaseInsensitive = "operation encountered errors", |
| 201 | + expectedMessageSubstringCaseSensitive = "c8azjdwz2x", |
| 202 | + expectedCause = null, |
| 203 | + expectedRawData = mapOf("person1" to mapOf("name" to name), "person2" to null), |
| 204 | + expectedData = null, |
| 205 | + errorsValidator = { it.shouldHaveAtLeastSize(1) }, |
| 206 | + ) |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + fun executeMutationFailsWithNonNullDataNonEmptyErrorsDecodingFails() = runTest { |
| 211 | + val id = Arb.alphanumericString().next() |
| 212 | + val name = Arb.alphanumericString().next() |
| 213 | + val mutationRef = |
| 214 | + dataConnect.mutation( |
| 215 | + operationName = "createPersonWithPartialFailure", |
| 216 | + variables = CreatePersonWithPartialFailureVariables(id = id, name = name), |
| 217 | + dataDeserializer = serializer<IncompatibleData>(), |
| 218 | + variablesSerializer = serializer(), |
| 219 | + optionsBuilder = {}, |
| 220 | + ) |
| 221 | + |
| 222 | + val exception = shouldThrow<DataConnectOperationException> { mutationRef.execute() } |
| 223 | + |
| 224 | + exception.shouldSatisfy( |
| 225 | + expectedMessageSubstringCaseInsensitive = "operation encountered errors", |
| 226 | + expectedMessageSubstringCaseSensitive = "ecxpjy4qfy", |
| 227 | + expectedCause = null, |
| 228 | + expectedRawData = mapOf("person1" to mapOf("id" to id), "person2" to null), |
| 229 | + expectedData = null, |
| 230 | + errorsValidator = { it.shouldHaveAtLeastSize(1) }, |
| 231 | + ) |
| 232 | + } |
| 233 | + |
| 234 | + @Serializable private data class IncompatibleVariables(val jwdbzka4k5: String) |
| 235 | + |
| 236 | + @Serializable private data class IncompatibleData(val btzjhbfz7h: String) |
| 237 | + |
| 238 | + private fun Arb.Companion.incompatibleVariables(string: Arb<String> = Arb.alphanumericString()) = |
| 239 | + string.map { IncompatibleVariables(it) } |
| 240 | + |
| 241 | + @Serializable private data class GetPersonWithPartialFailureVariables(val id: String) |
| 242 | + |
| 243 | + @Serializable |
| 244 | + private data class GetPersonWithPartialFailureData(val person1: Person, val person2: Nothing?) { |
| 245 | + constructor(person1Name: String) : this(Person(person1Name), null) |
| 246 | + |
| 247 | + @Serializable private data class Person(val name: String) |
| 248 | + } |
| 249 | + |
| 250 | + @Serializable |
| 251 | + private data class CreatePersonWithPartialFailureVariables(val id: String, val name: String) |
| 252 | + |
| 253 | + @Serializable |
| 254 | + private data class CreatePersonWithPartialFailureData( |
| 255 | + val person1: Person, |
| 256 | + val person2: Nothing? |
| 257 | + ) { |
| 258 | + constructor(person1Id: String) : this(Person(person1Id), null) |
| 259 | + |
| 260 | + @Serializable private data class Person(val id: String) |
| 261 | + } |
| 262 | +} |
0 commit comments