diff --git a/firebase-dataconnect/connectors/src/androidTest/kotlin/com/google/firebase/dataconnect/connectors/demo/OrderDirectionIntegrationTest.kt b/firebase-dataconnect/connectors/src/androidTest/kotlin/com/google/firebase/dataconnect/connectors/demo/OrderDirectionIntegrationTest.kt new file mode 100644 index 00000000000..910b98bbdf6 --- /dev/null +++ b/firebase-dataconnect/connectors/src/androidTest/kotlin/com/google/firebase/dataconnect/connectors/demo/OrderDirectionIntegrationTest.kt @@ -0,0 +1,94 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.dataconnect.connectors.demo + +import com.google.firebase.dataconnect.connectors.demo.testutil.DemoConnectorIntegrationTestBase +import com.google.firebase.dataconnect.testutil.sortedParallelTo +import com.google.firebase.dataconnect.testutil.tag +import io.kotest.common.DelicateKotest +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder +import io.kotest.property.Arb +import io.kotest.property.arbitrary.distinct +import io.kotest.property.arbitrary.int +import io.kotest.property.arbitrary.next +import java.util.UUID +import kotlinx.coroutines.test.runTest +import org.junit.Test + +class OrderDirectionIntegrationTest : DemoConnectorIntegrationTestBase() { + + @OptIn(DelicateKotest::class) private val uniqueInts = Arb.int().distinct() + + @Test + fun orderDirectionQueryVariableOmittedShouldUseUnspecifiedOrder() = runTest { + val tag = Arb.tag().next(rs) + val values = List(5) { uniqueInts.next(rs) } + val insertedIds = insertRow(tag, values) + + val queryResult = connector.orderDirectionTestGetAllByTag.execute(tag) {} + + val queryIds = queryResult.data.items.map { it.id } + queryIds shouldContainExactlyInAnyOrder insertedIds + } + + @Test + fun orderDirectionQueryVariableAscendingOrder() = runTest { + val tag = Arb.tag().next(rs) + val values = List(5) { uniqueInts.next(rs) } + val insertedIds = insertRow(tag, values) + + val queryResult = + connector.orderDirectionTestGetAllByTag.execute(tag) { orderDirection = OrderDirection.ASC } + + val queryIds = queryResult.data.items.map { it.id } + val insertedIdsSorted = insertedIds.sortedParallelTo(values) + queryIds shouldContainExactly insertedIdsSorted + } + + @Test + fun orderDirectionQueryVariableDescendingOrder() = runTest { + val tag = Arb.tag().next(rs) + val values = List(5) { uniqueInts.next(rs) } + val insertedIds = insertRow(tag, values) + + val queryResult = + connector.orderDirectionTestGetAllByTag.execute(tag) { orderDirection = OrderDirection.DESC } + + val queryIds = queryResult.data.items.map { it.id } + val insertedIdsSorted = insertedIds.sortedParallelTo(values).reversed() + queryIds shouldContainExactly insertedIdsSorted + } + + private suspend fun insertRow(tag: String, values: List): List { + require(values.size == 5) { "values.size must be 5, but got ${values.size}" } + return insertRow(tag, values[0], values[1], values[2], values[3], values[4]) + } + + private suspend fun insertRow( + tag: String, + value1: Int, + value2: Int, + value3: Int, + value4: Int, + value5: Int, + ): List { + val result = + connector.orderDirectionTestInsert5.execute(tag, value1, value2, value3, value4, value5) + return result.data.run { listOf(key1.id, key2.id, key3.id, key4.id, key5.id) } + } +} diff --git a/firebase-dataconnect/emulator/dataconnect/connector/alltypes/alltypes_ops.gql b/firebase-dataconnect/emulator/dataconnect/connector/alltypes/alltypes_ops.gql index 9ed5737e8c9..575c2d64dfa 100644 --- a/firebase-dataconnect/emulator/dataconnect/connector/alltypes/alltypes_ops.gql +++ b/firebase-dataconnect/emulator/dataconnect/connector/alltypes/alltypes_ops.gql @@ -190,33 +190,3 @@ query getFarm($id: String!) @auth(level: PUBLIC) { } } } - -############################################################################### -# Operations for table: OrderDirectionTest -############################################################################### - -mutation OrderDirectionTestInsert5( - $tag: String!, - $value1: Int!, - $value2: Int!, - $value3: Int!, - $value4: Int!, - $value5: Int!, -) @auth(level: PUBLIC) { - key1: orderDirectionTest_insert(data: { tag: $tag, value: $value1 }) - key2: orderDirectionTest_insert(data: { tag: $tag, value: $value2 }) - key3: orderDirectionTest_insert(data: { tag: $tag, value: $value3 }) - key4: orderDirectionTest_insert(data: { tag: $tag, value: $value4 }) - key5: orderDirectionTest_insert(data: { tag: $tag, value: $value5 }) -} - -query OrderDirectionTestGetAllByTag( - $tag: String!, - $orderDirection: OrderDirection, -) @auth(level: PUBLIC) { - items: orderDirectionTests( - limit: 10, - orderBy: { value: $orderDirection }, - where: { tag: { eq: $tag } }, - ) { id } -} diff --git a/firebase-dataconnect/emulator/dataconnect/connector/demo/demo_ops.gql b/firebase-dataconnect/emulator/dataconnect/connector/demo/demo_ops.gql index 4f63cad042f..99122b6b14b 100644 --- a/firebase-dataconnect/emulator/dataconnect/connector/demo/demo_ops.gql +++ b/firebase-dataconnect/emulator/dataconnect/connector/demo/demo_ops.gql @@ -1413,3 +1413,33 @@ query AnyScalarNonNullableListOfNonNullableGetAllByTagAndValue($tag: String, $va where: { value: { includesAll: $value }, tag: { eq: $tag } }, ) { id } } + +############################################################################### +# Operations for table: OrderDirectionTest +############################################################################### + +mutation OrderDirectionTestInsert5( + $tag: String!, + $value1: Int!, + $value2: Int!, + $value3: Int!, + $value4: Int!, + $value5: Int!, +) @auth(level: PUBLIC) { + key1: orderDirectionTest_insert(data: { tag: $tag, value: $value1 }) + key2: orderDirectionTest_insert(data: { tag: $tag, value: $value2 }) + key3: orderDirectionTest_insert(data: { tag: $tag, value: $value3 }) + key4: orderDirectionTest_insert(data: { tag: $tag, value: $value4 }) + key5: orderDirectionTest_insert(data: { tag: $tag, value: $value5 }) +} + +query OrderDirectionTestGetAllByTag( + $tag: String!, + $orderDirection: OrderDirection, +) @auth(level: PUBLIC) { + items: orderDirectionTests( + limit: 10, + orderBy: { value: $orderDirection }, + where: { tag: { eq: $tag } }, + ) { id } +} diff --git a/firebase-dataconnect/emulator/dataconnect/schema/alltypes_schema.gql b/firebase-dataconnect/emulator/dataconnect/schema/alltypes_schema.gql index 279340d42d1..47465c76f3a 100644 --- a/firebase-dataconnect/emulator/dataconnect/schema/alltypes_schema.gql +++ b/firebase-dataconnect/emulator/dataconnect/schema/alltypes_schema.gql @@ -62,8 +62,3 @@ type Farmer @table { name: String! parent: Farmer } - -type OrderDirectionTest @table @index(fields: ["tag"]) { - value: Int - tag: String -} diff --git a/firebase-dataconnect/emulator/dataconnect/schema/demo_schema.gql b/firebase-dataconnect/emulator/dataconnect/schema/demo_schema.gql index d497f7726e3..a9326c0a0c3 100644 --- a/firebase-dataconnect/emulator/dataconnect/schema/demo_schema.gql +++ b/firebase-dataconnect/emulator/dataconnect/schema/demo_schema.gql @@ -337,3 +337,8 @@ type AnyScalarNonNullableListOfNonNullable @table @index(fields: ["tag"]) { tag: String position: Int } + +type OrderDirectionTest @table @index(fields: ["tag"]) { + value: Int + tag: String +} diff --git a/firebase-dataconnect/src/androidTest/kotlin/com/google/firebase/dataconnect/OrderDirectionIntegrationTest.kt b/firebase-dataconnect/src/androidTest/kotlin/com/google/firebase/dataconnect/OrderDirectionIntegrationTest.kt index 6dea7fd97af..88e9fd8767d 100644 --- a/firebase-dataconnect/src/androidTest/kotlin/com/google/firebase/dataconnect/OrderDirectionIntegrationTest.kt +++ b/firebase-dataconnect/src/androidTest/kotlin/com/google/firebase/dataconnect/OrderDirectionIntegrationTest.kt @@ -34,14 +34,14 @@ import org.junit.Test class OrderDirectionIntegrationTest : DataConnectIntegrationTestBase() { private val dataConnect: FirebaseDataConnect by lazy { - val connectorConfig = testConnectorConfig.copy(connector = "alltypes") + val connectorConfig = testConnectorConfig.copy(connector = "demo") dataConnectFactory.newInstance(connectorConfig) } @OptIn(DelicateKotest::class) private val uniqueInts = Arb.int().distinct() @Test - fun orderDirectionQueryVariableOmittedShouldUseAscendingOrder() = runTest { + fun orderDirectionQueryVariableOmittedShouldUseUnspecifiedOrder() = runTest { val tag = Arb.tag().next(rs) val values = List(5) { uniqueInts.next(rs) } val insertedIds = insertRow(tag, values)