Skip to content

Commit d7e01d1

Browse files
committed
EnumIntegrationTest.kt: fix withNullAppended bug
1 parent 932f01f commit d7e01d1

File tree

3 files changed

+104
-43
lines changed

3 files changed

+104
-43
lines changed

firebase-dataconnect/src/androidTest/kotlin/com/google/firebase/dataconnect/EnumIntegrationTest.kt

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
@file:OptIn(ExperimentalFirebaseDataConnect::class)
18-
1917
package com.google.firebase.dataconnect
2018

2119
import com.google.firebase.dataconnect.testutil.DataConnectIntegrationTestBase
2220
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
21+
import com.google.firebase.dataconnect.testutil.withNullAppended
2322
import io.kotest.assertions.assertSoftly
2423
import io.kotest.assertions.throwables.shouldThrow
2524
import io.kotest.assertions.withClue
@@ -185,11 +184,6 @@ class EnumIntegrationTest : DataConnectIntegrationTestBase() {
185184
@Serializable data class Item(val value: N5ekmae3jn)
186185
}
187186

188-
@Serializable
189-
private data class GetNonNullableByKeySubsetData(val item: Item?) {
190-
@Serializable data class Item(val value: N5ekmae3jnSubset)
191-
}
192-
193187
//////////////////////////////////////////////////////////////////////////////////////////////////
194188
// Tests for EnumNullable table.
195189
//////////////////////////////////////////////////////////////////////////////////////////////////
@@ -234,12 +228,9 @@ class EnumIntegrationTest : DataConnectIntegrationTestBase() {
234228
val insertVariables = InsertNullableVariables(enumValue)
235229
val key = dataConnect.mutation(insertVariables).execute().data.key
236230
val queryVariables = GetNullableByKeyVariables(key)
237-
val queryRef =
238-
dataConnect
239-
.query(queryVariables)
240-
.withDataDeserializer(serializer<GetNullableByKeySubsetData>())
231+
val queryRef = dataConnect.query(queryVariables)
241232
val queryResult = queryRef.execute().data
242-
withClue(queryResult) { queryResult.item?.value shouldBe enumValue }
233+
withClue(queryResult) { queryResult?.item?.value shouldBe enumValue }
243234
}
244235
}
245236

@@ -319,11 +310,6 @@ class EnumIntegrationTest : DataConnectIntegrationTestBase() {
319310
@Serializable data class Item(val value: N5ekmae3jn?)
320311
}
321312

322-
@Serializable
323-
private data class GetNullableByKeySubsetData(val item: Item?) {
324-
@Serializable data class Item(val value: N5ekmae3jnSubset?)
325-
}
326-
327313
//////////////////////////////////////////////////////////////////////////////////////////////////
328314
// Tests for EnumNonNullableTableDefault table.
329315
//////////////////////////////////////////////////////////////////////////////////////////////////
@@ -420,11 +406,6 @@ class EnumIntegrationTest : DataConnectIntegrationTestBase() {
420406
@Serializable data class Item(val value: List<N5ekmae3jn?>)
421407
}
422408

423-
@Serializable
424-
private data class GetNonNullableListOfNonNullableByKeySubsetData(val item: Item?) {
425-
@Serializable data class Item(val value: List<N5ekmae3jnSubset?>?)
426-
}
427-
428409
//////////////////////////////////////////////////////////////////////////////////////////////////
429410
// Tests for EnumNonNullableListOfNullable table.
430411
//////////////////////////////////////////////////////////////////////////////////////////////////
@@ -739,13 +720,6 @@ class EnumIntegrationTest : DataConnectIntegrationTestBase() {
739720
N3HWNCRWBP,
740721
}
741722

742-
@Suppress("SpellCheckingInspection")
743-
private enum class N5ekmae3jnSubset {
744-
DPSKD6HR3A,
745-
XGWGVMYTHJ,
746-
QJX7C7RD5T
747-
}
748-
749723
@Suppress("SpellCheckingInspection", "unused")
750724
enum class S7yayynb25 {
751725
XJ27ZAXKD3,
@@ -759,20 +733,6 @@ class EnumIntegrationTest : DataConnectIntegrationTestBase() {
759733
/** The default number of iterations to use in property-based tests. */
760734
const val NUM_ITERATIONS = 10
761735

762-
fun <T> List<T>.withNullAppended(): List<T?> =
763-
buildList(size + 1) {
764-
addAll(this)
765-
add(null)
766-
}
767-
768-
fun N5ekmae3jn.toN5ekmae3jnSubsetOrNull(): N5ekmae3jnSubset? =
769-
when (this) {
770-
N5ekmae3jn.DPSKD6HR3A -> N5ekmae3jnSubset.DPSKD6HR3A
771-
N5ekmae3jn.XGWGVMYTHJ -> N5ekmae3jnSubset.XGWGVMYTHJ
772-
N5ekmae3jn.QJX7C7RD5T -> N5ekmae3jnSubset.QJX7C7RD5T
773-
else -> null
774-
}
775-
776736
fun FirebaseDataConnect.mutation(
777737
variables: InsertNonNullableVariables
778738
): MutationRef<InsertData, InsertNonNullableVariables> =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.testutil
18+
19+
/**
20+
* Creates and returns a new [List] that contains all of the elements in the receiving [List] but
21+
* with a `null` element added to the end.
22+
*/
23+
@JvmName("withNullAppendedCollection")
24+
fun <T> Collection<T>.withNullAppended(): List<T?> =
25+
buildList(size + 1) {
26+
addAll(this@withNullAppended)
27+
add(null)
28+
}
29+
30+
/**
31+
* Creates and returns a new [List] that contains all of the elements in the receiving [Iterable]
32+
* but with a `null` element added to the end.
33+
*/
34+
@JvmName("withNullAppendedIterable")
35+
fun <T> Iterable<T>.withNullAppended(): List<T?> = buildList {
36+
addAll(this@withNullAppended)
37+
add(null)
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.testutil
18+
19+
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
20+
import io.kotest.matchers.shouldBe
21+
import io.kotest.property.Arb
22+
import io.kotest.property.arbitrary.list
23+
import io.kotest.property.checkAll
24+
import kotlinx.coroutines.test.runTest
25+
import org.junit.Test
26+
27+
class WithNullAppendedUnitTest {
28+
29+
@Test
30+
fun `Iterable withNullAppended on an empty iterable`() {
31+
val iterable: Iterable<Nothing> = emptyList()
32+
iterable.withNullAppended() shouldBe listOf(null)
33+
}
34+
35+
@Test
36+
fun `Iterable withNullAppended on a non-empty iterable`() = runTest {
37+
checkAll(NUM_ITERATIONS, Arb.list(Arb.dataConnect.string(), 1..100)) { list ->
38+
val iterable: Iterable<String> = list
39+
val expected = List(list.size + 1) { if (it < list.size) list[it] else null }
40+
iterable.withNullAppended() shouldBe expected
41+
}
42+
}
43+
44+
@Test
45+
fun `Collection withNullAppended on an empty collection`() {
46+
val collection: Collection<Nothing> = emptyList()
47+
collection.withNullAppended() shouldBe listOf(null)
48+
}
49+
50+
@Test
51+
fun `Collection withNullAppended on a non-empty collection`() = runTest {
52+
checkAll(NUM_ITERATIONS, Arb.list(Arb.dataConnect.string(), 1..100)) { list ->
53+
val collection: Collection<String> = list
54+
val expected = List(list.size + 1) { if (it < list.size) list[it] else null }
55+
collection.withNullAppended() shouldBe expected
56+
}
57+
}
58+
59+
private companion object {
60+
61+
const val NUM_ITERATIONS = 100
62+
}
63+
}

0 commit comments

Comments
 (0)