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: relax LocalDateSerializer encoding and decoding, and add unit test coverage #6451

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
1 change: 1 addition & 0 deletions firebase-dataconnect/firebase-dataconnect.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ dependencies {
testImplementation(libs.kotlinx.datetime)
testImplementation(libs.kotlinx.serialization.json)
testImplementation(libs.mockk)
testImplementation(libs.testonly.three.ten.abp)
testImplementation(libs.robolectric)

androidTestImplementation(project(":firebase-dataconnect:androidTestutil"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,48 @@ public object LocalDateSerializer : KSerializer<LocalDate> {
PrimitiveSerialDescriptor("com.google.firebase.dataconnect.LocalDate", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: LocalDate) {
value.run {
require(year >= 0) { "invalid value: $value (year must be non-negative)" }
require(month >= 0) { "invalid value: $value (month must be non-negative)" }
require(day >= 0) { "invalid value: $value (day must be non-negative)" }
}
val serializedDate =
"${value.year}".padStart(4, '0') +
'-' +
"${value.month}".padStart(2, '0') +
'-' +
"${value.day}".padStart(2, '0')
val serializedDate: String = serializeToString(value)
encoder.encodeString(serializedDate)
}

override fun deserialize(decoder: Decoder): LocalDate {
val decodedString = decoder.decodeString()
val matcher = Pattern.compile("^(\\d+)-(\\d+)-(\\d+)$").matcher(decodedString)
return deserializeToLocalDate(decodedString)
}

private val decodeRegexPattern = Pattern.compile("^(-?\\d+)-(-?\\d+)-(-?\\d+)$")

private fun deserializeToLocalDate(string: String): LocalDate {
val matcher = decodeRegexPattern.matcher(string)
require(matcher.matches()) {
"date \"$decodedString\" does not match regular expression: ${matcher.pattern()}"
"date \"$string\" does not match regular expression: ${matcher.pattern()}"
}

fun Matcher.groupToIntIgnoringLeadingZeroes(index: Int): Int {
val groupText = group(index)!!.trimStart('0')
return if (groupText.isEmpty()) 0 else groupText.toInt()
val groupText =
group(index)
?: throw IllegalStateException(
"internal error: group(index) should not be null " +
" (index=$index, string=$string, matcher=$this, error code hp48d53pbb)"
)

val isNegative = groupText.firstOrNull() == '-'

val zeroPaddedString =
if (isNegative) {
groupText.substring(1)
} else {
groupText
}

val intAbsString = zeroPaddedString.trimStart('0')
val intStringPrefix = if (isNegative) "-" else ""
val intString = intStringPrefix + intAbsString
if (intString.isEmpty()) {
return 0
}

return intString.toInt()
}

val year = matcher.groupToIntIgnoringLeadingZeroes(1)
Expand All @@ -68,4 +86,33 @@ public object LocalDateSerializer : KSerializer<LocalDate> {

return LocalDate(year = year, month = month, day = day)
}

private fun serializeToString(localDate: LocalDate): String {
val yearStr = localDate.year.toZeroPaddedString(length = 4)
val monthStr = localDate.month.toZeroPaddedString(length = 2)
val dayStr = localDate.day.toZeroPaddedString(length = 2)
return "$yearStr-$monthStr-$dayStr"
}

private fun Int.toZeroPaddedString(length: Int): String = buildString {
append(this@toZeroPaddedString)

val firstChar =
firstOrNull()?.let {
if (it == '-') {
deleteCharAt(0)
it
} else {
null
}
}

while (this.length < length) {
insert(0, '0')
}

if (firstChar != null) {
insert(0, firstChar)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
/*
* 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.
*/

@file:OptIn(ExperimentalKotest::class)

package com.google.firebase.dataconnect

import com.google.firebase.dataconnect.testutil.property.arbitrary.threeTenBp
import com.google.firebase.dataconnect.testutil.shouldContainWithNonAbuttingText
import com.google.firebase.dataconnect.testutil.toDataConnectLocalDate
import com.google.firebase.dataconnect.testutil.toJavaTimeLocalDate
import com.google.firebase.dataconnect.testutil.toKotlinxDatetimeLocalDate
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.withClue
import io.kotest.common.ExperimentalKotest
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldEndWith
import io.kotest.matchers.string.shouldStartWith
import io.kotest.property.Arb
import io.kotest.property.PropTestConfig
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.of
import io.kotest.property.assume
import io.kotest.property.checkAll
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.number
import org.junit.Test

@Suppress("ReplaceCallWithBinaryOperator")
class LocalDateUnitTest {

@Test
fun `constructor() should set properties to corresponding arguments`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate = LocalDate(year = year, month = month, day = day)
assertSoftly {
withClue("year") { localDate.year shouldBe year }
withClue("month") { localDate.month shouldBe month }
withClue("day") { localDate.day shouldBe day }
}
}
}

@Test
fun `equals() should return true when invoked with itself`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate = LocalDate(year = year, month = month, day = day)
localDate.equals(localDate) shouldBe true
}
}

@Test
fun `equals() should return true when invoked with a distinct, but equal, instance`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate1 = LocalDate(year = year, month = month, day = day)
val localDate2 = LocalDate(year = year, month = month, day = day)
localDate1.equals(localDate2) shouldBe true
}
}

@Test
fun `equals() should return false when invoked with null`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate = LocalDate(year = year, month = month, day = day)
localDate.equals(null) shouldBe false
}
}

@Test
fun `equals() should return false when invoked with a different type`() = runTest {
val others = Arb.of("foo", 42, java.time.LocalDate.now())
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), others) { year, month, day, other ->
val localDate = LocalDate(year = year, month = month, day = day)
localDate.equals(other) shouldBe false
}
}

@Test
fun `equals() should return false when only the year differs`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year1, month, day, year2
->
assume(year1 != year2)
val localDate1 = LocalDate(year = year1, month = month, day = day)
val localDate2 = LocalDate(year = year2, month = month, day = day)
localDate1.equals(localDate2) shouldBe false
}
}

@Test
fun `equals() should return false when only the month differs`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month1, day, month2
->
assume(month1 != month2)
val localDate1 = LocalDate(year = year, month = month1, day = day)
val localDate2 = LocalDate(year = year, month = month2, day = day)
localDate1.equals(localDate2) shouldBe false
}
}

@Test
fun `equals() should return false when only the day differs`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month, day1, day2
->
assume(day1 != day2)
val localDate1 = LocalDate(year = year, month = month, day = day1)
val localDate2 = LocalDate(year = year, month = month, day = day2)
localDate1.equals(localDate2) shouldBe false
}
}

@Test
fun `hashCode() should return the same value when invoked repeatedly`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate = LocalDate(year = year, month = month, day = day)
val hashCode = localDate.hashCode()
repeat(5) { withClue("iteration=$it") { localDate.hashCode() shouldBe hashCode } }
}
}

@Test
fun `hashCode() should return the same value when invoked on equal, but distinct, objects`() =
runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate1 = LocalDate(year = year, month = month, day = day)
val localDate2 = LocalDate(year = year, month = month, day = day)
localDate1.hashCode() shouldBe localDate2.hashCode()
}
}

@Test
fun `hashCode() should return different values for different years`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year1, month, day, year2
->
assume(year1.hashCode() != year2.hashCode())
val localDate1 = LocalDate(year = year1, month = month, day = day)
val localDate2 = LocalDate(year = year2, month = month, day = day)
localDate1.hashCode() shouldNotBe localDate2.hashCode()
}
}

@Test
fun `hashCode() should return different values for different months`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month1, day, month2
->
assume(month1.hashCode() != month2.hashCode())
val localDate1 = LocalDate(year = year, month = month1, day = day)
val localDate2 = LocalDate(year = year, month = month2, day = day)
localDate1.hashCode() shouldNotBe localDate2.hashCode()
}
}

@Test
fun `hashCode() should return different values for different days`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month, day1, day2
->
assume(day1.hashCode() != day2.hashCode())
val localDate1 = LocalDate(year = year, month = month, day = day1)
val localDate2 = LocalDate(year = year, month = month, day = day2)
localDate1.hashCode() shouldNotBe localDate2.hashCode()
}
}

@Test
fun `toString() should return a string conforming to what is expected`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate = LocalDate(year = year, month = month, day = day)
val toStringResult = localDate.toString()
assertSoftly {
toStringResult shouldStartWith "LocalDate("
toStringResult shouldEndWith ")"
toStringResult shouldContainWithNonAbuttingText "year=$year"
toStringResult shouldContainWithNonAbuttingText "month=$month"
toStringResult shouldContainWithNonAbuttingText "day=$day"
}
}
}

@Test
fun `copy() with no arguments should return an equal, but distinct, instance`() = runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day ->
val localDate1 = LocalDate(year = year, month = month, day = day)
val localDate2 = localDate1.copy()
localDate1 shouldBe localDate2
}
}

@Test
fun `copy() with all arguments should return a new instance with the given arguments`() =
runTest {
checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int(), Arb.int(), Arb.int()) {
year1,
month1,
day1,
year2,
month2,
day2 ->
val localDate1 = LocalDate(year = year1, month = month1, day = day1)
val localDate2 = localDate1.copy(year = year2, month = month2, day = day2)
localDate2 shouldBe LocalDate(year = year2, month = month2, day = day2)
}
}

@Test
fun `toJavaLocalDate() should return an equivalent java time LocalDate object`() = runTest {
checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData ->
val fdcLocalDate: LocalDate = testData.toDataConnectLocalDate()
val jLocalDate: java.time.LocalDate = fdcLocalDate.toJavaLocalDate()
assertSoftly {
withClue("year") { jLocalDate.year shouldBe testData.year }
withClue("month") { jLocalDate.month.number shouldBe testData.monthValue }
withClue("dayOfMonth") { jLocalDate.dayOfMonth shouldBe testData.dayOfMonth }
}
}
}

@Test
fun `toKotlinxLocalDate() should return an equivalent java time LocalDate object`() = runTest {
checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData ->
val fdcLocalDate: LocalDate = testData.toDataConnectLocalDate()
val kLocalDate: kotlinx.datetime.LocalDate = fdcLocalDate.toKotlinxLocalDate()
assertSoftly {
withClue("year") { kLocalDate.year shouldBe testData.year }
withClue("month") { kLocalDate.month.number shouldBe testData.monthValue }
withClue("dayOfMonth") { kLocalDate.dayOfMonth shouldBe testData.dayOfMonth }
}
}
}

@Test
fun `toDataConnectLocalDate() on java time LocalDate should return an equivalent LocalDate object`() =
runTest {
checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData ->
val jLocalDate: java.time.LocalDate = testData.toJavaTimeLocalDate()
val fdcLocalDate: LocalDate = jLocalDate.toDataConnectLocalDate()
assertSoftly {
withClue("year") { fdcLocalDate.year shouldBe testData.year }
withClue("month") { fdcLocalDate.month shouldBe testData.monthValue }
withClue("day") { fdcLocalDate.day shouldBe testData.dayOfMonth }
}
}
}

@Test
fun `toDataConnectLocalDate() on kotlinx datetime LocalDate should return an equivalent LocalDate object`() =
runTest {
checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData ->
val kLocalDate: kotlinx.datetime.LocalDate = testData.toKotlinxDatetimeLocalDate()
val fdcLocalDate: LocalDate = kLocalDate.toDataConnectLocalDate()
assertSoftly {
withClue("year") { fdcLocalDate.year shouldBe testData.year }
withClue("month") { fdcLocalDate.month shouldBe testData.monthValue }
withClue("day") { fdcLocalDate.day shouldBe testData.dayOfMonth }
}
}
}

private companion object {
val propTestConfig = PropTestConfig(iterations = 50)
}
}
Loading
Loading