Skip to content

Commit c737e21

Browse files
authored
dataconnect: LocalDateIntegrationTest.kt added (#6504)
1 parent 28a227a commit c737e21

File tree

13 files changed

+1730
-156
lines changed

13 files changed

+1730
-156
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 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.FirebaseDataConnect
20+
import com.google.firebase.dataconnect.LocalDate
21+
import com.google.firebase.dataconnect.serializers.UUIDSerializer
22+
import io.kotest.matchers.nulls.shouldNotBeNull
23+
import java.util.UUID
24+
import kotlinx.serialization.Serializable
25+
import kotlinx.serialization.serializer
26+
27+
suspend fun FirebaseDataConnect.requestTimeAsDate(): LocalDate = requestTime().requestTimeAsDate
28+
29+
private suspend fun FirebaseDataConnect.requestTime(): ExprValuesQueryData.Item {
30+
val insertMutationRef =
31+
mutation("ExprValues_Insert", Unit, serializer<ExprValuesInsertData>(), serializer())
32+
val insertResult = insertMutationRef.execute()
33+
34+
val queryVariables = ExprValuesQueryVariables(insertResult.data.key)
35+
val getByKeyQueryRef =
36+
query("ExprValues_GetByKey", queryVariables, serializer<ExprValuesQueryData>(), serializer())
37+
val queryResults = getByKeyQueryRef.execute()
38+
39+
return queryResults.data.item.shouldNotBeNull()
40+
}
41+
42+
@Serializable
43+
private data class ExprValuesQueryData(val item: Item?) {
44+
@Serializable
45+
data class Item(
46+
val requestTimeAsDate: LocalDate,
47+
)
48+
}
49+
50+
@Serializable private data class ExprValuesQueryVariables(val key: ExprValuesKey)
51+
52+
@Serializable private data class ExprValuesInsertData(val key: ExprValuesKey)
53+
54+
@Serializable
55+
private data class ExprValuesKey(@Serializable(with = UUIDSerializer::class) val id: UUID)

firebase-dataconnect/connectors/src/androidTest/kotlin/com/google/firebase/dataconnect/connectors/demo/DateScalarIntegrationTest.kt

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import com.google.firebase.dataconnect.testutil.dateFromYearMonthDayUTC
2727
import com.google.firebase.dataconnect.testutil.executeWithEmptyVariables
2828
import com.google.firebase.dataconnect.testutil.property.arbitrary.EdgeCases
2929
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
30-
import com.google.firebase.dataconnect.testutil.property.arbitrary.date
31-
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateOffDayBoundary
30+
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateTestData
31+
import com.google.firebase.dataconnect.testutil.property.arbitrary.toJavaUtilDate
3232
import io.kotest.assertions.assertSoftly
3333
import io.kotest.assertions.throwables.shouldThrow
3434
import io.kotest.assertions.withClue
@@ -37,7 +37,6 @@ import io.kotest.property.Arb
3737
import io.kotest.property.arbitrary.int
3838
import io.kotest.property.arbitrary.next
3939
import io.kotest.property.checkAll
40-
import java.util.Date
4140
import kotlinx.coroutines.test.runTest
4241
import kotlinx.serialization.Serializable
4342
import kotlinx.serialization.serializer
@@ -47,17 +46,17 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
4746

4847
@Test
4948
fun nonNullDate_insert_NormalCases() = runTest {
50-
checkAll(20, Arb.dataConnect.date()) {
51-
val key = connector.insertNonNullDate.execute(it.date).data.key
49+
checkAll(20, Arb.dataConnect.dateTestData()) {
50+
val key = connector.insertNonNullDate.execute(it.toJavaUtilDate()).data.key
5251
assertNonNullDateByKeyEquals(key, it.string)
5352
}
5453
}
5554

5655
@Test
5756
fun nonNullDate_insert_EdgeCases() = runTest {
5857
assertSoftly {
59-
EdgeCases.dates.all.forEach {
60-
val key = connector.insertNonNullDate.execute(it.date).data.key
58+
EdgeCases.dates.all().forEach {
59+
val key = connector.insertNonNullDate.execute(it.toJavaUtilDate()).data.key
6160
assertNonNullDateByKeyEquals(key, it.string)
6261
}
6362
}
@@ -76,10 +75,9 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
7675

7776
@Test
7877
fun nonNullDate_insert_ShouldIgnoreTime() = runTest {
79-
checkAll(20, Arb.dataConnect.dateOffDayBoundary()) {
80-
val key = connector.insertNonNullDate.execute(it.date).data.key
81-
assertNonNullDateByKeyEquals(key, it.string)
82-
}
78+
val date = "2024-03-26T19:48:00.144Z"
79+
val key = connector.insertNonNullDate.executeWithStringVariables(date).data.key
80+
assertNonNullDateByKeyEquals(key, dateFromYearMonthDayUTC(2024, 3, 26))
8381
}
8482

8583
@Test
@@ -96,7 +94,7 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
9694
GetNonNullDatesWithDefaultsByKeyQuery.Data.NonNullDatesWithDefaults(
9795
valueWithVariableDefault = dateFromYearMonthDayUTC(6904, 11, 30),
9896
valueWithSchemaDefault = dateFromYearMonthDayUTC(2112, 1, 31),
99-
epoch = EdgeCases.dates.zero.date,
97+
epoch = EdgeCases.dates.epoch.toJavaUtilDate(),
10098
requestTime1 = expectedRequestTime,
10199
requestTime2 = expectedRequestTime,
102100
)
@@ -135,24 +133,26 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
135133

136134
@Test
137135
fun nonNullDate_update_NormalCases() = runTest {
138-
checkAll(20, Arb.dataConnect.date(), Arb.dataConnect.date()) { date1, date2 ->
139-
val key = connector.insertNonNullDate.execute(date1.date).data.key
140-
connector.updateNonNullDate.execute(key) { value = date2.date }
136+
checkAll(20, Arb.dataConnect.dateTestData(), Arb.dataConnect.dateTestData()) { date1, date2 ->
137+
val key = connector.insertNonNullDate.execute(date1.toJavaUtilDate()).data.key
138+
connector.updateNonNullDate.execute(key) { value = date2.toJavaUtilDate() }
141139
assertNonNullDateByKeyEquals(key, date2.string)
142140
}
143141
}
144142

145143
@Test
146144
fun nonNullDate_update_EdgeCases() = runTest {
147-
val edgeCases = EdgeCases.dates.all
148-
val dates1 = edgeCases + List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases
149-
val dates2 = List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases + edgeCases
145+
val edgeCases = EdgeCases.dates.all()
146+
val dates1 =
147+
edgeCases + List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases
148+
val dates2 =
149+
List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases + edgeCases
150150

151151
assertSoftly {
152152
for ((date1, date2) in dates1.zip(dates2)) {
153153
withClue("date1=${date1.string} date2=${date2.string}") {
154-
val key = connector.insertNonNullDate.execute(date1.date).data.key
155-
connector.updateNonNullDate.execute(key) { value = date2.date }
154+
val key = connector.insertNonNullDate.execute(date1.toJavaUtilDate()).data.key
155+
connector.updateNonNullDate.execute(key) { value = date2.toJavaUtilDate() }
156156
assertNonNullDateByKeyEquals(key, date2.string)
157157
}
158158
}
@@ -161,26 +161,26 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
161161

162162
@Test
163163
fun nonNullDate_update_DateVariableOmitted() = runTest {
164-
val date = Arb.dataConnect.date().next(rs)
165-
val key = connector.insertNonNullDate.execute(date.date).data.key
164+
val date = Arb.dataConnect.dateTestData().next(rs)
165+
val key = connector.insertNonNullDate.execute(date.toJavaUtilDate()).data.key
166166
connector.updateNonNullDate.execute(key) {}
167-
assertNonNullDateByKeyEquals(key, date.date)
167+
assertNonNullDateByKeyEquals(key, date.toJavaUtilDate())
168168
}
169169

170170
@Test
171171
fun nullableDate_insert_NormalCases() = runTest {
172-
checkAll(20, Arb.dataConnect.date()) {
173-
val key = connector.insertNullableDate.execute { value = it.date }.data.key
172+
checkAll(20, Arb.dataConnect.dateTestData()) {
173+
val key = connector.insertNullableDate.execute { value = it.toJavaUtilDate() }.data.key
174174
assertNullableDateByKeyEquals(key, it.string)
175175
}
176176
}
177177

178178
@Test
179179
fun nullableDate_insert_EdgeCases() = runTest {
180-
val edgeCases = EdgeCases.dates.all + listOf(null)
180+
val edgeCases = EdgeCases.dates.all() + listOf(null)
181181
assertSoftly {
182182
edgeCases.forEach {
183-
val key = connector.insertNullableDate.execute { value = it?.date }.data.key
183+
val key = connector.insertNullableDate.execute { value = it?.toJavaUtilDate() }.data.key
184184
if (it === null) {
185185
assertNullableDateByKeyHasNullInnerValue(key)
186186
} else {
@@ -209,10 +209,9 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
209209

210210
@Test
211211
fun nullableDate_insert_ShouldIgnoreTime() = runTest {
212-
checkAll(20, Arb.dataConnect.dateOffDayBoundary()) {
213-
val key = connector.insertNullableDate.execute { value = it.date }.data.key
214-
assertNullableDateByKeyEquals(key, it.string)
215-
}
212+
val date = "2024-03-26T19:48:00.144Z"
213+
val key = connector.insertNullableDate.executeWithStringVariables(date).data.key
214+
assertNullableDateByKeyEquals(key, dateFromYearMonthDayUTC(2024, 3, 26))
216215
}
217216

218217
@Test
@@ -245,7 +244,7 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
245244
GetNullableDatesWithDefaultsByKeyQuery.Data.NullableDatesWithDefaults(
246245
valueWithVariableDefault = dateFromYearMonthDayUTC(8113, 2, 9),
247246
valueWithSchemaDefault = dateFromYearMonthDayUTC(1921, 12, 2),
248-
epoch = EdgeCases.dates.zero.date,
247+
epoch = EdgeCases.dates.epoch.toJavaUtilDate(),
249248
requestTime1 = expectedRequestTime,
250249
requestTime2 = expectedRequestTime,
251250
)
@@ -254,24 +253,26 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
254253

255254
@Test
256255
fun nullableDate_update_NormalCases() = runTest {
257-
checkAll(20, Arb.dataConnect.date(), Arb.dataConnect.date()) { date1, date2 ->
258-
val key = connector.insertNullableDate.execute { value = date1.date }.data.key
259-
connector.updateNullableDate.execute(key) { value = date2.date }
256+
checkAll(20, Arb.dataConnect.dateTestData(), Arb.dataConnect.dateTestData()) { date1, date2 ->
257+
val key = connector.insertNullableDate.execute { value = date1.toJavaUtilDate() }.data.key
258+
connector.updateNullableDate.execute(key) { value = date2.toJavaUtilDate() }
260259
assertNullableDateByKeyEquals(key, date2.string)
261260
}
262261
}
263262

264263
@Test
265264
fun nullableDate_update_EdgeCases() = runTest {
266-
val edgeCases = EdgeCases.dates.all
267-
val dates1 = edgeCases + List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases
268-
val dates2 = List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases + edgeCases
265+
val edgeCases = EdgeCases.dates.all()
266+
val dates1 =
267+
edgeCases + List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases
268+
val dates2 =
269+
List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases + edgeCases
269270

270271
assertSoftly {
271272
for ((date1, date2) in dates1.zip(dates2)) {
272273
withClue("date1=${date1.string} date2=${date2.string}") {
273-
val key = connector.insertNullableDate.execute { value = date1.date }.data.key
274-
connector.updateNullableDate.execute(key) { value = date2.date }
274+
val key = connector.insertNullableDate.execute { value = date1.toJavaUtilDate() }.data.key
275+
connector.updateNullableDate.execute(key) { value = date2.toJavaUtilDate() }
275276
assertNullableDateByKeyEquals(key, date2.string)
276277
}
277278
}
@@ -280,23 +281,23 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
280281

281282
@Test
282283
fun nullableDate_update_UpdateNonNullValueToNull() = runTest {
283-
val date = Arb.dataConnect.date().next(rs).date
284+
val date = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
284285
val key = connector.insertNullableDate.execute { value = date }.data.key
285286
connector.updateNullableDate.execute(key) { value = null }
286287
assertNullableDateByKeyHasNullInnerValue(key)
287288
}
288289

289290
@Test
290291
fun nullableDate_update_UpdateNullValueToNonNull() = runTest {
291-
val date = Arb.dataConnect.date().next(rs).date
292+
val date = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
292293
val key = connector.insertNullableDate.execute { value = null }.data.key
293294
connector.updateNullableDate.execute(key) { value = date }
294295
assertNullableDateByKeyEquals(key, date)
295296
}
296297

297298
@Test
298299
fun nullableDate_update_DateVariableOmitted() = runTest {
299-
val date = Arb.dataConnect.date().next(rs).date
300+
val date = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
300301
val key = connector.insertNullableDate.execute { value = date }.data.key
301302
connector.updateNullableDate.execute(key) {}
302303
assertNullableDateByKeyEquals(key, date)
@@ -310,7 +311,7 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
310311
queryResult.data shouldBe GetDateByKeyQueryStringData(expected)
311312
}
312313

313-
private suspend fun assertNonNullDateByKeyEquals(key: NonNullDateKey, expected: Date) {
314+
private suspend fun assertNonNullDateByKeyEquals(key: NonNullDateKey, expected: java.util.Date) {
314315
val queryResult = connector.getNonNullDateByKey.execute(key)
315316
queryResult.data shouldBe
316317
GetNonNullDateByKeyQuery.Data(GetNonNullDateByKeyQuery.Data.Value(expected))
@@ -333,16 +334,19 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
333334
queryResult.data shouldBe GetDateByKeyQueryStringData(expected)
334335
}
335336

336-
private suspend fun assertNullableDateByKeyEquals(key: NullableDateKey, expected: Date) {
337+
private suspend fun assertNullableDateByKeyEquals(
338+
key: NullableDateKey,
339+
expected: java.util.Date
340+
) {
337341
val queryResult = connector.getNullableDateByKey.execute(key)
338342
queryResult.data shouldBe
339343
GetNullableDateByKeyQuery.Data(GetNullableDateByKeyQuery.Data.Value(expected))
340344
}
341345

342346
/**
343347
* A `Data` type that can be used in place of [GetNonNullDateByKeyQuery.Data] that types the value
344-
* as a [String] instead of a [Date], allowing verification of the data sent over the wire without
345-
* possible confounding from date deserialization.
348+
* as a [String] instead of a [java.util.Date], allowing verification of the data sent over the
349+
* wire without possible confounding from date deserialization.
346350
*/
347351
@Serializable
348352
private data class GetDateByKeyQueryStringData(val value: DateStringValue?) {
@@ -353,15 +357,15 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
353357

354358
/**
355359
* A `Variables` type that can be used in place of [InsertNonNullDateMutation.Variables] that
356-
* types the value as a [String] instead of a [Date], allowing verification of the data sent over
357-
* the wire without possible confounding from date serialization.
360+
* types the value as a [String] instead of a [java.util.Date], allowing verification of the data
361+
* sent over the wire without possible confounding from date serialization.
358362
*/
359363
@Serializable private data class InsertDateStringVariables(val value: String?)
360364

361365
/**
362366
* A `Variables` type that can be used in place of [InsertNonNullDateMutation.Variables] that
363-
* types the value as a [Int] instead of a [Date], allowing verification that the server fails
364-
* with an expected error (rather than crashing, for example).
367+
* types the value as a [Int] instead of a [java.util.Date], allowing verification that the server
368+
* fails with an expected error (rather than crashing, for example).
365369
*/
366370
@Serializable private data class InsertDateIntVariables(val value: Int)
367371

firebase-dataconnect/connectors/src/androidTest/kotlin/com/google/firebase/dataconnect/connectors/demo/KeyVariablesIntegrationTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ package com.google.firebase.dataconnect.connectors.demo
1818

1919
import com.google.firebase.dataconnect.connectors.demo.testutil.DemoConnectorIntegrationTestBase
2020
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
21-
import com.google.firebase.dataconnect.testutil.property.arbitrary.date
21+
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateTestData
22+
import com.google.firebase.dataconnect.testutil.property.arbitrary.toJavaUtilDate
2223
import com.google.firebase.dataconnect.testutil.randomTimestamp
2324
import com.google.firebase.dataconnect.testutil.withMicrosecondPrecision
2425
import io.kotest.matchers.shouldBe
@@ -85,7 +86,7 @@ class KeyVariablesIntegrationTest : DemoConnectorIntegrationTestBase() {
8586

8687
@Test
8788
fun primaryKeyIsDate() = runTest {
88-
val id = Arb.dataConnect.date().next(rs).date
89+
val id = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
8990
val value = Arb.dataConnect.string().next(rs)
9091

9192
val key = connector.insertPrimaryKeyIsDate.execute(foo = id, value = value).data.key

firebase-dataconnect/connectors/src/androidTest/kotlin/com/google/firebase/dataconnect/connectors/demo/ListVariablesAndDataIntegrationTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import com.google.firebase.dataconnect.connectors.demo.testutil.DemoConnectorInt
2121
import com.google.firebase.dataconnect.testutil.property.arbitrary.DataConnectArb
2222
import com.google.firebase.dataconnect.testutil.property.arbitrary.EdgeCases
2323
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
24-
import com.google.firebase.dataconnect.testutil.property.arbitrary.date
24+
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateTestData
25+
import com.google.firebase.dataconnect.testutil.property.arbitrary.toJavaUtilDate
2526
import com.google.firebase.dataconnect.testutil.withMicrosecondPrecision
2627
import io.kotest.common.ExperimentalKotest
2728
import io.kotest.matchers.shouldBe
@@ -523,7 +524,7 @@ class ListVariablesAndDataIntegrationTest : DemoConnectorIntegrationTestBase() {
523524
booleans = EdgeCases.booleans,
524525
uuids = EdgeCases.uuids,
525526
int64s = EdgeCases.int64s,
526-
dates = EdgeCases.dates.all.map { it.date },
527+
dates = EdgeCases.dates.all().map { it.toJavaUtilDate() },
527528
timestamps = EdgeCases.javaTime.instants.all.map { it.timestamp },
528529
)
529530
}
@@ -545,7 +546,8 @@ class ListVariablesAndDataIntegrationTest : DemoConnectorIntegrationTestBase() {
545546
booleans: Arb<List<Boolean>> = Arb.list(Arb.boolean(), 1..100),
546547
uuids: Arb<List<UUID>> = Arb.list(Arb.uuid(), 1..100),
547548
int64s: Arb<List<Long>> = Arb.list(Arb.long(), 1..100),
548-
dates: Arb<List<Date>> = Arb.list(Arb.dataConnect.date().map { it.date }, 1..100),
549+
dates: Arb<List<Date>> =
550+
Arb.list(Arb.dataConnect.dateTestData().map { it.toJavaUtilDate() }, 1..100),
549551
timestamps: Arb<List<Timestamp>> =
550552
Arb.list(Arb.dataConnect.javaTime.instantTestCase().map { it.timestamp }, 1..100),
551553
): Arb<Lists> = arbitrary {

0 commit comments

Comments
 (0)