Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataconnect: LocalDateIntegrationTest.kt added #6504

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.testutil

import com.google.firebase.dataconnect.FirebaseDataConnect
import com.google.firebase.dataconnect.LocalDate
import com.google.firebase.dataconnect.serializers.UUIDSerializer
import io.kotest.matchers.nulls.shouldNotBeNull
import java.util.UUID
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

suspend fun FirebaseDataConnect.requestTimeAsDate(): LocalDate = requestTime().requestTimeAsDate

private suspend fun FirebaseDataConnect.requestTime(): ExprValuesQueryData.Item {
val insertMutationRef =
mutation("ExprValues_Insert", Unit, serializer<ExprValuesInsertData>(), serializer())
val insertResult = insertMutationRef.execute()

val queryVariables = ExprValuesQueryVariables(insertResult.data.key)
val getByKeyQueryRef =
query("ExprValues_GetByKey", queryVariables, serializer<ExprValuesQueryData>(), serializer())
val queryResults = getByKeyQueryRef.execute()

return queryResults.data.item.shouldNotBeNull()
}

@Serializable
private data class ExprValuesQueryData(val item: Item?) {
@Serializable
data class Item(
val requestTimeAsDate: LocalDate,
)
}

@Serializable private data class ExprValuesQueryVariables(val key: ExprValuesKey)

@Serializable private data class ExprValuesInsertData(val key: ExprValuesKey)

@Serializable
private data class ExprValuesKey(@Serializable(with = UUIDSerializer::class) val id: UUID)
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import com.google.firebase.dataconnect.testutil.dateFromYearMonthDayUTC
import com.google.firebase.dataconnect.testutil.executeWithEmptyVariables
import com.google.firebase.dataconnect.testutil.property.arbitrary.EdgeCases
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
import com.google.firebase.dataconnect.testutil.property.arbitrary.date
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateOffDayBoundary
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateTestData
import com.google.firebase.dataconnect.testutil.property.arbitrary.toJavaUtilDate
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
Expand All @@ -37,7 +37,6 @@ import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.next
import io.kotest.property.checkAll
import java.util.Date
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer
Expand All @@ -47,17 +46,17 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {

@Test
fun nonNullDate_insert_NormalCases() = runTest {
checkAll(20, Arb.dataConnect.date()) {
val key = connector.insertNonNullDate.execute(it.date).data.key
checkAll(20, Arb.dataConnect.dateTestData()) {
val key = connector.insertNonNullDate.execute(it.toJavaUtilDate()).data.key
assertNonNullDateByKeyEquals(key, it.string)
}
}

@Test
fun nonNullDate_insert_EdgeCases() = runTest {
assertSoftly {
EdgeCases.dates.all.forEach {
val key = connector.insertNonNullDate.execute(it.date).data.key
EdgeCases.dates.all().forEach {
val key = connector.insertNonNullDate.execute(it.toJavaUtilDate()).data.key
assertNonNullDateByKeyEquals(key, it.string)
}
}
Expand All @@ -76,10 +75,9 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {

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

@Test
Expand All @@ -96,7 +94,7 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
GetNonNullDatesWithDefaultsByKeyQuery.Data.NonNullDatesWithDefaults(
valueWithVariableDefault = dateFromYearMonthDayUTC(6904, 11, 30),
valueWithSchemaDefault = dateFromYearMonthDayUTC(2112, 1, 31),
epoch = EdgeCases.dates.zero.date,
epoch = EdgeCases.dates.epoch.toJavaUtilDate(),
requestTime1 = expectedRequestTime,
requestTime2 = expectedRequestTime,
)
Expand Down Expand Up @@ -135,24 +133,26 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {

@Test
fun nonNullDate_update_NormalCases() = runTest {
checkAll(20, Arb.dataConnect.date(), Arb.dataConnect.date()) { date1, date2 ->
val key = connector.insertNonNullDate.execute(date1.date).data.key
connector.updateNonNullDate.execute(key) { value = date2.date }
checkAll(20, Arb.dataConnect.dateTestData(), Arb.dataConnect.dateTestData()) { date1, date2 ->
val key = connector.insertNonNullDate.execute(date1.toJavaUtilDate()).data.key
connector.updateNonNullDate.execute(key) { value = date2.toJavaUtilDate() }
assertNonNullDateByKeyEquals(key, date2.string)
}
}

@Test
fun nonNullDate_update_EdgeCases() = runTest {
val edgeCases = EdgeCases.dates.all
val dates1 = edgeCases + List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases
val dates2 = List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases + edgeCases
val edgeCases = EdgeCases.dates.all()
val dates1 =
edgeCases + List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases
val dates2 =
List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases + edgeCases

assertSoftly {
for ((date1, date2) in dates1.zip(dates2)) {
withClue("date1=${date1.string} date2=${date2.string}") {
val key = connector.insertNonNullDate.execute(date1.date).data.key
connector.updateNonNullDate.execute(key) { value = date2.date }
val key = connector.insertNonNullDate.execute(date1.toJavaUtilDate()).data.key
connector.updateNonNullDate.execute(key) { value = date2.toJavaUtilDate() }
assertNonNullDateByKeyEquals(key, date2.string)
}
}
Expand All @@ -161,26 +161,26 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {

@Test
fun nonNullDate_update_DateVariableOmitted() = runTest {
val date = Arb.dataConnect.date().next(rs)
val key = connector.insertNonNullDate.execute(date.date).data.key
val date = Arb.dataConnect.dateTestData().next(rs)
val key = connector.insertNonNullDate.execute(date.toJavaUtilDate()).data.key
connector.updateNonNullDate.execute(key) {}
assertNonNullDateByKeyEquals(key, date.date)
assertNonNullDateByKeyEquals(key, date.toJavaUtilDate())
}

@Test
fun nullableDate_insert_NormalCases() = runTest {
checkAll(20, Arb.dataConnect.date()) {
val key = connector.insertNullableDate.execute { value = it.date }.data.key
checkAll(20, Arb.dataConnect.dateTestData()) {
val key = connector.insertNullableDate.execute { value = it.toJavaUtilDate() }.data.key
assertNullableDateByKeyEquals(key, it.string)
}
}

@Test
fun nullableDate_insert_EdgeCases() = runTest {
val edgeCases = EdgeCases.dates.all + listOf(null)
val edgeCases = EdgeCases.dates.all() + listOf(null)
assertSoftly {
edgeCases.forEach {
val key = connector.insertNullableDate.execute { value = it?.date }.data.key
val key = connector.insertNullableDate.execute { value = it?.toJavaUtilDate() }.data.key
if (it === null) {
assertNullableDateByKeyHasNullInnerValue(key)
} else {
Expand Down Expand Up @@ -209,10 +209,9 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {

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

@Test
Expand Down Expand Up @@ -245,7 +244,7 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
GetNullableDatesWithDefaultsByKeyQuery.Data.NullableDatesWithDefaults(
valueWithVariableDefault = dateFromYearMonthDayUTC(8113, 2, 9),
valueWithSchemaDefault = dateFromYearMonthDayUTC(1921, 12, 2),
epoch = EdgeCases.dates.zero.date,
epoch = EdgeCases.dates.epoch.toJavaUtilDate(),
requestTime1 = expectedRequestTime,
requestTime2 = expectedRequestTime,
)
Expand All @@ -254,24 +253,26 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {

@Test
fun nullableDate_update_NormalCases() = runTest {
checkAll(20, Arb.dataConnect.date(), Arb.dataConnect.date()) { date1, date2 ->
val key = connector.insertNullableDate.execute { value = date1.date }.data.key
connector.updateNullableDate.execute(key) { value = date2.date }
checkAll(20, Arb.dataConnect.dateTestData(), Arb.dataConnect.dateTestData()) { date1, date2 ->
val key = connector.insertNullableDate.execute { value = date1.toJavaUtilDate() }.data.key
connector.updateNullableDate.execute(key) { value = date2.toJavaUtilDate() }
assertNullableDateByKeyEquals(key, date2.string)
}
}

@Test
fun nullableDate_update_EdgeCases() = runTest {
val edgeCases = EdgeCases.dates.all
val dates1 = edgeCases + List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases
val dates2 = List(edgeCases.size) { Arb.dataConnect.date().next(rs) } + edgeCases + edgeCases
val edgeCases = EdgeCases.dates.all()
val dates1 =
edgeCases + List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases
val dates2 =
List(edgeCases.size) { Arb.dataConnect.dateTestData().next(rs) } + edgeCases + edgeCases

assertSoftly {
for ((date1, date2) in dates1.zip(dates2)) {
withClue("date1=${date1.string} date2=${date2.string}") {
val key = connector.insertNullableDate.execute { value = date1.date }.data.key
connector.updateNullableDate.execute(key) { value = date2.date }
val key = connector.insertNullableDate.execute { value = date1.toJavaUtilDate() }.data.key
connector.updateNullableDate.execute(key) { value = date2.toJavaUtilDate() }
assertNullableDateByKeyEquals(key, date2.string)
}
}
Expand All @@ -280,23 +281,23 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {

@Test
fun nullableDate_update_UpdateNonNullValueToNull() = runTest {
val date = Arb.dataConnect.date().next(rs).date
val date = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
val key = connector.insertNullableDate.execute { value = date }.data.key
connector.updateNullableDate.execute(key) { value = null }
assertNullableDateByKeyHasNullInnerValue(key)
}

@Test
fun nullableDate_update_UpdateNullValueToNonNull() = runTest {
val date = Arb.dataConnect.date().next(rs).date
val date = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
val key = connector.insertNullableDate.execute { value = null }.data.key
connector.updateNullableDate.execute(key) { value = date }
assertNullableDateByKeyEquals(key, date)
}

@Test
fun nullableDate_update_DateVariableOmitted() = runTest {
val date = Arb.dataConnect.date().next(rs).date
val date = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
val key = connector.insertNullableDate.execute { value = date }.data.key
connector.updateNullableDate.execute(key) {}
assertNullableDateByKeyEquals(key, date)
Expand All @@ -310,7 +311,7 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
queryResult.data shouldBe GetDateByKeyQueryStringData(expected)
}

private suspend fun assertNonNullDateByKeyEquals(key: NonNullDateKey, expected: Date) {
private suspend fun assertNonNullDateByKeyEquals(key: NonNullDateKey, expected: java.util.Date) {
val queryResult = connector.getNonNullDateByKey.execute(key)
queryResult.data shouldBe
GetNonNullDateByKeyQuery.Data(GetNonNullDateByKeyQuery.Data.Value(expected))
Expand All @@ -333,16 +334,19 @@ class DateScalarIntegrationTest : DemoConnectorIntegrationTestBase() {
queryResult.data shouldBe GetDateByKeyQueryStringData(expected)
}

private suspend fun assertNullableDateByKeyEquals(key: NullableDateKey, expected: Date) {
private suspend fun assertNullableDateByKeyEquals(
key: NullableDateKey,
expected: java.util.Date
) {
val queryResult = connector.getNullableDateByKey.execute(key)
queryResult.data shouldBe
GetNullableDateByKeyQuery.Data(GetNullableDateByKeyQuery.Data.Value(expected))
}

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package com.google.firebase.dataconnect.connectors.demo

import com.google.firebase.dataconnect.connectors.demo.testutil.DemoConnectorIntegrationTestBase
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
import com.google.firebase.dataconnect.testutil.property.arbitrary.date
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateTestData
import com.google.firebase.dataconnect.testutil.property.arbitrary.toJavaUtilDate
import com.google.firebase.dataconnect.testutil.randomTimestamp
import com.google.firebase.dataconnect.testutil.withMicrosecondPrecision
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -85,7 +86,7 @@ class KeyVariablesIntegrationTest : DemoConnectorIntegrationTestBase() {

@Test
fun primaryKeyIsDate() = runTest {
val id = Arb.dataConnect.date().next(rs).date
val id = Arb.dataConnect.dateTestData().next(rs).toJavaUtilDate()
val value = Arb.dataConnect.string().next(rs)

val key = connector.insertPrimaryKeyIsDate.execute(foo = id, value = value).data.key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import com.google.firebase.dataconnect.connectors.demo.testutil.DemoConnectorInt
import com.google.firebase.dataconnect.testutil.property.arbitrary.DataConnectArb
import com.google.firebase.dataconnect.testutil.property.arbitrary.EdgeCases
import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
import com.google.firebase.dataconnect.testutil.property.arbitrary.date
import com.google.firebase.dataconnect.testutil.property.arbitrary.dateTestData
import com.google.firebase.dataconnect.testutil.property.arbitrary.toJavaUtilDate
import com.google.firebase.dataconnect.testutil.withMicrosecondPrecision
import io.kotest.common.ExperimentalKotest
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -523,7 +524,7 @@ class ListVariablesAndDataIntegrationTest : DemoConnectorIntegrationTestBase() {
booleans = EdgeCases.booleans,
uuids = EdgeCases.uuids,
int64s = EdgeCases.int64s,
dates = EdgeCases.dates.all.map { it.date },
dates = EdgeCases.dates.all().map { it.toJavaUtilDate() },
timestamps = EdgeCases.javaTime.instants.all.map { it.timestamp },
)
}
Expand All @@ -545,7 +546,8 @@ class ListVariablesAndDataIntegrationTest : DemoConnectorIntegrationTestBase() {
booleans: Arb<List<Boolean>> = Arb.list(Arb.boolean(), 1..100),
uuids: Arb<List<UUID>> = Arb.list(Arb.uuid(), 1..100),
int64s: Arb<List<Long>> = Arb.list(Arb.long(), 1..100),
dates: Arb<List<Date>> = Arb.list(Arb.dataConnect.date().map { it.date }, 1..100),
dates: Arb<List<Date>> =
Arb.list(Arb.dataConnect.dateTestData().map { it.toJavaUtilDate() }, 1..100),
timestamps: Arb<List<Timestamp>> =
Arb.list(Arb.dataConnect.javaTime.instantTestCase().map { it.timestamp }, 1..100),
): Arb<Lists> = arbitrary {
Expand Down
Loading
Loading