Skip to content

Firestore: add a Kotlin unit test for PropertyName #4850

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 3 commits into from
Apr 6, 2023
Merged
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
Expand Up @@ -22,6 +22,7 @@ import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FieldPath
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings
import com.google.firebase.firestore.PropertyName
import com.google.firebase.firestore.TestUtil
import com.google.firebase.firestore.model.ObjectValue
import com.google.firebase.firestore.testutil.TestUtil.wrap
Expand Down Expand Up @@ -113,6 +114,13 @@ class LibraryVersionTest : BaseTestCase() {

data class Room(var a: Int = 0, var b: Int = 0)

// NOTE: When Kotlin properties are annotated with `PropertyName` they need to use @get and @set.
// Also, the properties need to be mutable; that is, declared with `var` and not `val`.
// See https://github.com/firebase/firebase-android-sdk/issues/4822
data class DataClassWithPropertyName(
@get:PropertyName("dbName") @set:PropertyName("dbName") var objName: String = "DefaultObjName"
)

@RunWith(RobolectricTestRunner::class)
class DocumentSnapshotTests {
@Before
Expand Down Expand Up @@ -155,6 +163,15 @@ class DocumentSnapshotTests {
assertThat(room)
.isEqualTo(ds.toObject(Room::class.java, DocumentSnapshot.ServerTimestampBehavior.ESTIMATE))
}

@Test
fun `PropertyName annotation works on data classes`() {
val ds = TestUtil.documentSnapshot("foo/bar", mapOf("dbName" to "CustomValue"), false)

val toObjectReturnValue = ds.toObject<DataClassWithPropertyName>()

assertThat(toObjectReturnValue).isEqualTo(DataClassWithPropertyName("CustomValue"))
}
}

@RunWith(RobolectricTestRunner::class)
Expand Down