Skip to content

Commit

Permalink
Merge pull request #290 from GitLiveApp/add-tests-for-firestore-array…
Browse files Browse the repository at this point in the history
…-queries

add tests for arrayUnion and arrayRemove in Firestore
  • Loading branch information
nbransby authored Apr 19, 2022
2 parents c7bebbd + ed641f9 commit a135597
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ expect fun runTest(test: suspend CoroutineScope.() -> Unit)
class FirebaseFirestoreTest {

@Serializable
data class FirestoreTest(val prop1: String, val time: Double = 0.0, val count: Int = 0)
data class FirestoreTest(
val prop1: String,
val time: Double = 0.0,
val count: Int = 0,
val list: List<String> = emptyList(),
)

@BeforeTest
fun initializeFirebase() {
Expand Down Expand Up @@ -254,6 +259,36 @@ class FirebaseFirestoreTest {
assertEquals(5, dataAfter.count)
}

@Test
fun testArrayUnion() = runTest {
val doc = Firebase.firestore
.collection("testFirestoreArrayUnion")
.document("test1")

doc.set(FirestoreTest.serializer(), FirestoreTest("increment1", list = listOf("first")))
val dataBefore = doc.get().data(FirestoreTest.serializer())
assertEquals(listOf("first"), dataBefore.list)

doc.update("list" to FieldValue.arrayUnion("second"))
val dataAfter = doc.get().data(FirestoreTest.serializer())
assertEquals(listOf("first", "second"), dataAfter.list)
}

@Test
fun testArrayRemove() = runTest {
val doc = Firebase.firestore
.collection("testFirestoreArrayRemove")
.document("test1")

doc.set(FirestoreTest.serializer(), FirestoreTest("increment1", list = listOf("first", "second")))
val dataBefore = doc.get().data(FirestoreTest.serializer())
assertEquals(listOf("first", "second"), dataBefore.list)

doc.update("list" to FieldValue.arrayRemove("second"))
val dataAfter = doc.get().data(FirestoreTest.serializer())
assertEquals(listOf("first"), dataAfter.list)
}

private suspend fun setupFirestoreData() {
Firebase.firestore.collection("testFirestoreQuerying")
.document("one")
Expand Down

0 comments on commit a135597

Please sign in to comment.