Skip to content

Commit 607382b

Browse files
authored
dataconnect: testing connector added for future realtime testing (#8095)
1 parent d2950a5 commit 607382b

6 files changed

Lines changed: 230 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
connectorId: realtime
2+
authMode: PUBLIC
3+
generate:
4+
kotlinSdk:
5+
outputDir: ../../.generated/realtime
6+
package: com.google.firebase.dataconnect.connectors.realtime
7+
clientCache:
8+
maxAge: 1h
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
mutation RealtimeString_Insert($name: String!) @auth(level: PUBLIC) {
16+
key: realtimeString_insert(data: {name: $name})
17+
}
18+
19+
mutation RealtimeString_Update($key: RealtimeString_Key!, $name: String!) @auth(level: PUBLIC) {
20+
realtimeString_update(key: $key, data: {name: $name})
21+
}
22+
23+
mutation RealtimeString_Delete($key: RealtimeString_Key!) @auth(level: PUBLIC) {
24+
realtimeString_delete(key: $key)
25+
}
26+
27+
query RealtimeString_GetByKey($key: RealtimeString_Key!) @auth(level: PUBLIC) {
28+
item: realtimeString(key: $key) { name }
29+
}

firebase-dataconnect/emulator/dataconnect/dataconnect.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ connectorDirs: [
1313
"./connector/demo",
1414
"./connector/alltypes",
1515
"./connector/keywords",
16+
"./connector/realtime",
1617
"./connector/person",
1718
"./connector/posts",
1819
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
type RealtimeString @table {
16+
name: String!
17+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2026 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.schemas
18+
19+
import com.google.firebase.dataconnect.ConnectorConfig
20+
import com.google.firebase.dataconnect.FirebaseDataConnect
21+
import com.google.firebase.dataconnect.serializers.UUIDSerializer
22+
import com.google.firebase.dataconnect.testutil.TestDataConnectFactory
23+
import java.util.UUID
24+
import kotlinx.serialization.Serializable
25+
import kotlinx.serialization.serializer
26+
27+
class RealtimeConnector private constructor(val dataConnect: FirebaseDataConnect) {
28+
29+
val getStringByKey = GetStringByKeyQuery(this)
30+
31+
val insertString = InsertStringMutation(this)
32+
33+
val updateString = UpdateStringMutation(this)
34+
35+
val deleteString = DeleteStringMutation(this)
36+
37+
suspend fun getString(key: Key) = getStringByKey.execute(key)
38+
39+
suspend fun insertString(name: String) = insertString.execute(name)
40+
41+
suspend fun updateString(key: Key, name: String) = updateString.execute(key, name)
42+
43+
suspend fun deleteString(key: Key) = deleteString.execute(key)
44+
45+
class GetStringByKeyQuery(val connector: RealtimeConnector) {
46+
47+
suspend fun execute(key: Key) = execute(Variables(key))
48+
49+
suspend fun execute(variables: Variables) = queryRef(variables).execute().data.item
50+
51+
fun queryRef(variables: Variables) =
52+
connector.dataConnect.query(OPERATION_NAME, variables, serializer<Data>(), serializer())
53+
54+
@Serializable data class Variables(val key: Key)
55+
56+
@Serializable
57+
data class Data(val item: Item?) {
58+
@Serializable data class Item(val name: String)
59+
}
60+
61+
companion object {
62+
const val OPERATION_NAME = "RealtimeString_GetByKey"
63+
}
64+
}
65+
66+
class InsertStringMutation(val connector: RealtimeConnector) {
67+
@Serializable data class Variables(val name: String)
68+
@Serializable data class Data(val key: Key)
69+
70+
suspend fun execute(name: String) = execute(Variables(name = name))
71+
72+
suspend fun execute(variables: Variables) = mutationRef(variables).execute().data.key
73+
74+
fun mutationRef(variables: Variables) =
75+
connector.dataConnect.mutation(OPERATION_NAME, variables, serializer<Data>(), serializer())
76+
77+
companion object {
78+
const val OPERATION_NAME = "RealtimeString_Insert"
79+
}
80+
}
81+
82+
class UpdateStringMutation(val connector: RealtimeConnector) {
83+
@Serializable data class Variables(val key: Key, val name: String)
84+
85+
suspend fun execute(key: Key, name: String) = execute(Variables(key = key, name = name))
86+
87+
suspend fun execute(variables: Variables) {
88+
mutationRef(variables).execute()
89+
}
90+
91+
fun mutationRef(variables: Variables) =
92+
connector.dataConnect.mutation(OPERATION_NAME, variables, serializer<Unit>(), serializer())
93+
94+
companion object {
95+
const val OPERATION_NAME = "RealtimeString_Update"
96+
}
97+
}
98+
99+
class DeleteStringMutation(val connector: RealtimeConnector) {
100+
@Serializable data class Variables(val key: Key)
101+
102+
suspend fun execute(key: Key) = execute(Variables(key = key))
103+
104+
suspend fun execute(variables: Variables) {
105+
mutationRef(variables).execute()
106+
}
107+
108+
fun mutationRef(variables: Variables) =
109+
connector.dataConnect.mutation(OPERATION_NAME, variables, serializer<Unit>(), serializer())
110+
111+
companion object {
112+
const val OPERATION_NAME = "RealtimeString_Delete"
113+
}
114+
}
115+
116+
@Serializable data class Key(val id: @Serializable(with = UUIDSerializer::class) UUID)
117+
118+
companion object {
119+
val config =
120+
ConnectorConfig(
121+
connector = "realtime",
122+
location = "us-central1",
123+
serviceId = "sid2ehn9ct8te",
124+
)
125+
126+
fun getInstance(dataConnectFactory: TestDataConnectFactory): RealtimeConnector {
127+
val dataConnect = dataConnectFactory.newInstance(config)
128+
return RealtimeConnector(dataConnect)
129+
}
130+
}
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2026 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.schemas
18+
19+
import com.google.firebase.dataconnect.testutil.DataConnectIntegrationTestBase
20+
import com.google.firebase.dataconnect.testutil.property.arbitrary.pair
21+
import io.kotest.matchers.nulls.shouldBeNull
22+
import io.kotest.matchers.nulls.shouldNotBeNull
23+
import io.kotest.matchers.shouldBe
24+
import io.kotest.property.Arb
25+
import io.kotest.property.arbitrary.map
26+
import io.kotest.property.arbs.firstName
27+
import kotlinx.coroutines.test.runTest
28+
import org.junit.Test
29+
30+
class RealtimeConnectorIntegrationTest : DataConnectIntegrationTestBase() {
31+
32+
@Test
33+
fun realtimeConnectorBasicFunctionalityTest() = runTest {
34+
val connector = RealtimeConnector.getInstance(dataConnectFactory)
35+
val (name1, name2) = Arb.firstName().map { it.name }.pair().sample(rs).value
36+
37+
val key = connector.insertString(name1)
38+
connector.getString(key).shouldNotBeNull().name shouldBe name1
39+
connector.updateString(key, name2)
40+
connector.getString(key).shouldNotBeNull().name shouldBe name2
41+
connector.deleteString(key)
42+
connector.getString(key).shouldBeNull()
43+
}
44+
}

0 commit comments

Comments
 (0)