Skip to content

Commit 73db13a

Browse files
authored
Firestore: add a Kotlin unit test for PropertyName (#4850)
1 parent 77b2f26 commit 73db13a

File tree

1 file changed

+17
-0
lines changed
  • firebase-firestore/ktx/src/test/kotlin/com/google/firebase/firestore/ktx

1 file changed

+17
-0
lines changed

firebase-firestore/ktx/src/test/kotlin/com/google/firebase/firestore/ktx/FirestoreTests.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.google.firebase.firestore.DocumentSnapshot
2222
import com.google.firebase.firestore.FieldPath
2323
import com.google.firebase.firestore.FirebaseFirestore
2424
import com.google.firebase.firestore.FirebaseFirestoreSettings
25+
import com.google.firebase.firestore.PropertyName
2526
import com.google.firebase.firestore.TestUtil
2627
import com.google.firebase.firestore.model.ObjectValue
2728
import com.google.firebase.firestore.testutil.TestUtil.wrap
@@ -113,6 +114,13 @@ class LibraryVersionTest : BaseTestCase() {
113114

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

117+
// NOTE: When Kotlin properties are annotated with `PropertyName` they need to use @get and @set.
118+
// Also, the properties need to be mutable; that is, declared with `var` and not `val`.
119+
// See https://github.com/firebase/firebase-android-sdk/issues/4822
120+
data class DataClassWithPropertyName(
121+
@get:PropertyName("dbName") @set:PropertyName("dbName") var objName: String = "DefaultObjName"
122+
)
123+
116124
@RunWith(RobolectricTestRunner::class)
117125
class DocumentSnapshotTests {
118126
@Before
@@ -155,6 +163,15 @@ class DocumentSnapshotTests {
155163
assertThat(room)
156164
.isEqualTo(ds.toObject(Room::class.java, DocumentSnapshot.ServerTimestampBehavior.ESTIMATE))
157165
}
166+
167+
@Test
168+
fun `PropertyName annotation works on data classes`() {
169+
val ds = TestUtil.documentSnapshot("foo/bar", mapOf("dbName" to "CustomValue"), false)
170+
171+
val toObjectReturnValue = ds.toObject<DataClassWithPropertyName>()
172+
173+
assertThat(toObjectReturnValue).isEqualTo(DataClassWithPropertyName("CustomValue"))
174+
}
158175
}
159176

160177
@RunWith(RobolectricTestRunner::class)

0 commit comments

Comments
 (0)