Skip to content

dataconnect: add tests for user-defined enum support #7173

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

Merged
merged 7 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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/connectors/connectors.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies {
testImplementation(libs.kotest.assertions)
testImplementation(libs.kotest.property)
testImplementation(libs.kotlin.coroutines.test)
testImplementation(libs.kotlinx.serialization.json)
testImplementation(libs.mockk)
testImplementation(libs.robolectric)

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright 2025 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.connectors.demo

import com.google.firebase.dataconnect.testutil.property.arbitrary.distinctPair
import io.kotest.assertions.withClue
import io.kotest.common.ExperimentalKotest
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.types.shouldBeSameInstanceAs
import io.kotest.matchers.types.shouldNotBeSameInstanceAs
import io.kotest.property.Arb
import io.kotest.property.PropTestConfig
import io.kotest.property.arbitrary.enum
import io.kotest.property.arbitrary.of
import io.kotest.property.assume
import io.kotest.property.checkAll
import kotlinx.coroutines.test.runTest
import org.junit.Test

@Suppress("ReplaceCallWithBinaryOperator")
class EnumValueKnownUnitTest {

@Test
fun `constructor() should set properties to corresponding arguments`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue = EnumValue.Known(enum)
enumValue.value shouldBeSameInstanceAs enum
}
}

@Test
fun `stringValue property should be the name of the enum`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue = EnumValue.Known(enum)
enumValue.stringValue shouldBe enum.name
}
}

@Test
fun `equals() should return true when invoked with itself`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue = EnumValue.Known(enum)
enumValue.equals(enumValue) shouldBe true
}
}

@Test
fun `equals() should return true when invoked with a distinct, but equal, instance`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue1 = EnumValue.Known(enum)
val enumValue2 = EnumValue.Known(enum)
enumValue1.equals(enumValue2) shouldBe true
}
}

@Test
fun `equals() should return false when invoked with null`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue = EnumValue.Known(enum)
enumValue.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.enum<Food>(), others) { enum, other ->
val enumValue = EnumValue.Known(enum)
enumValue.equals(other) shouldBe false
}
}

@Test
fun `equals() should return false when the enum differs`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>().distinctPair()) { (enum1, enum2) ->
val enumValue1 = EnumValue.Known(enum1)
val enumValue2 = EnumValue.Known(enum2)
enumValue1.equals(enumValue2) shouldBe false
}
}

@Test
fun `hashCode() should return the same value when invoked repeatedly`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue = EnumValue.Known(enum)
val hashCode = enumValue.hashCode()
repeat(5) { withClue("iteration=$it") { enumValue.hashCode() shouldBe hashCode } }
}
}

@Test
fun `hashCode() should return the same value when invoked on equal, but distinct, objects`() =
runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue1 = EnumValue.Known(enum)
val enumValue2 = EnumValue.Known(enum)
enumValue1.hashCode() shouldBe enumValue2.hashCode()
}
}

@Test
fun `hashCode() should return different values for different enum values`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>().distinctPair()) { (enum1, enum2) ->
assume(enum1.hashCode() != enum2.hashCode())
val enumValue1 = EnumValue.Known(enum1)
val enumValue2 = EnumValue.Known(enum2)
enumValue1.hashCode() shouldNotBe enumValue2.hashCode()
}
}

@Test
fun `toString() should return a string conforming to what is expected`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue = EnumValue.Known(enum)
enumValue.toString() shouldBe "Known(${enum.name})"
}
}

@Test
fun `copy() with no arguments should return an equal, but distinct, instance`() = runTest {
checkAll(propTestConfig, Arb.enum<Food>()) { enum ->
val enumValue = EnumValue.Known(enum)
val enumValueCopy = enumValue.copy()
enumValue shouldBe enumValueCopy
enumValue shouldNotBeSameInstanceAs enumValueCopy
}
}

@Test
fun `copy() with all arguments should return a new instance with the given arguments`() =
runTest {
checkAll(propTestConfig, Arb.enum<Food>().distinctPair()) { (enum1, enum2) ->
val enumValue1 = EnumValue.Known(enum1)
val enumValue2 = enumValue1.copy(enum2)
enumValue2 shouldBe EnumValue.Known(enum2)
}
}

@Suppress("unused")
private enum class Food {
Burrito,
Cake,
Pizza,
Shawarma,
Sushi,
}

private companion object {
val propTestConfig = PropTestConfig(iterations = 50)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2025 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, ExperimentalSerializationApi::class)

package com.google.firebase.dataconnect.connectors.demo

import com.google.firebase.dataconnect.testutil.property.arbitrary.dataConnect
import io.kotest.common.ExperimentalKotest
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.EdgeConfig
import io.kotest.property.PropTestConfig
import io.kotest.property.arbitrary.enum
import io.kotest.property.arbitrary.map
import io.kotest.property.checkAll
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonPrimitive
import org.junit.Test

class EnumValueSerializerUnitTest {

@Test
fun `serialize() should produce the expected serialized string for _known_ values`() = runTest {
checkAll(propTestConfig, Arb.knownEnumValue()) { knownEnumValue: EnumValue.Known<Dog> ->
val encodedValue = Json.encodeToJsonElement(Dog.serializer, knownEnumValue)
encodedValue.jsonPrimitive.content shouldBe knownEnumValue.value.name
}
}

@Test
fun `serialize() should produce the expected serialized string for _unknown_ values`() = runTest {
checkAll(propTestConfig, Arb.unknownEnumValue()) { unknownEnumValue: EnumValue.Unknown ->
val encodedValue = Json.encodeToJsonElement(Dog.serializer, unknownEnumValue)
encodedValue.jsonPrimitive.content shouldBe unknownEnumValue.stringValue
}
}

@Test
fun `deserialize() should produce the expected EnumValue object for _known_ values`() = runTest {
checkAll(propTestConfig, Arb.knownEnumValue()) { knownEnumValue: EnumValue.Known<Dog> ->
val encodedValue = JsonPrimitive(knownEnumValue.value.name)
val decodedEnumValue = Json.decodeFromJsonElement(Dog.serializer, encodedValue)
decodedEnumValue shouldBe knownEnumValue
}
}

@Test
fun `deserialize() should produce the expected EnumValue object for _unknown_ values`() =
runTest {
checkAll(propTestConfig, Arb.unknownEnumValue()) { unknownEnumValue: EnumValue.Unknown ->
val encodedValue = JsonPrimitive(unknownEnumValue.stringValue)
val decodedEnumValue = Json.decodeFromJsonElement(Dog.serializer, encodedValue)
decodedEnumValue shouldBe unknownEnumValue
}
}

@Suppress("unused")
enum class Dog {
Boxer,
Bulldog,
Dachshund,
Labrador,
Poodle;

companion object {
val serializer: KSerializer<EnumValue<Dog>> = EnumValueSerializer(Dog.entries)
}
}

private companion object {
val propTestConfig =
PropTestConfig(
iterations = 500,
edgeConfig = EdgeConfig(edgecasesGenerationProbability = 0.2)
)

fun Arb.Companion.unknownEnumValue(
stringValue: Arb<String> = Arb.dataConnect.string()
): Arb<EnumValue.Unknown> = stringValue.map { EnumValue.Unknown(it) }

fun Arb.Companion.knownEnumValue(
enumValue: Arb<Dog> = Arb.enum<Dog>()
): Arb<EnumValue.Known<Dog>> = enumValue.map { EnumValue.Known(it) }
}
}
Loading
Loading