Skip to content

Commit

Permalink
Added test for list notification for object changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rorbech committed Jan 18, 2024
1 parent 53b90af commit 4afc80d
Showing 1 changed file with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.entities.Sample
import io.realm.kotlin.entities.list.RealmListContainer
import io.realm.kotlin.entities.list.listTestSchema
import io.realm.kotlin.ext.asRealmObject
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.notifications.DeletedList
import io.realm.kotlin.notifications.InitialList
Expand All @@ -35,9 +36,12 @@ import io.realm.kotlin.test.common.utils.assertIsChangeSet
import io.realm.kotlin.test.platform.PlatformUtils
import io.realm.kotlin.test.util.TestChannel
import io.realm.kotlin.test.util.receiveOrFail
import io.realm.kotlin.test.util.trySendOrFail
import io.realm.kotlin.types.RealmAny
import io.realm.kotlin.types.RealmList
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -734,6 +738,79 @@ class RealmListNotificationsTests : RealmEntityNotificationTests {
}
}

@Test
fun eventsOnObjectChangesInList() {
runBlocking {
val channel = Channel<ListChange<RealmListContainer>>(10)
val parent = realm.write { copyToRealm(RealmListContainer()).apply { stringField = "PARENT" }}

val listener = async {
parent.objectListField.asFlow().collect {
channel.trySendOrFail(it)
}
}

channel.receiveOrFail(message = "Initial event").let { assertIs<InitialList<*>>(it) }

realm.write {
findLatest(parent)!!.objectListField.add(
RealmListContainer().apply { stringField = "CHILD" }
)
}
channel.receiveOrFail(message = "List add").let {
assertIs<UpdatedList<*>>(it)
assertEquals(1, it.list.size)
}

realm.write {
findLatest(parent)!!.objectListField[0].stringField = "TEST"
}
channel.receiveOrFail(message = "Object updated").let {
assertIs<UpdatedList<*>>(it)
assertEquals(1, it.list.size)
assertEquals("TEST", it.list[0].stringField)
}

listener.cancel()
}
}
@Test
fun eventsOnObjectChangesInRealmAnyList() {
runBlocking {
val channel = Channel<ListChange<RealmAny?>>(10)
val parent = realm.write { copyToRealm(RealmListContainer()).apply { stringField = "PARENT" }}

val listener = async {
parent.nullableRealmAnyListField.asFlow().collect {
channel.trySendOrFail(it)
}
}

channel.receiveOrFail(message = "Initial event").let { assertIs<InitialList<*>>(it) }

realm.write {
findLatest(parent)!!.nullableRealmAnyListField.add(RealmAny.create(
RealmListContainer().apply { stringField = "CHILD" }))
}
channel.receiveOrFail(message = "List add").let {
assertIs<UpdatedList<*>>(it)
assertEquals(1, it.list.size)
}

realm.write {
findLatest(parent)!!.nullableRealmAnyListField[0]!!.asRealmObject<RealmListContainer>().stringField = "TEST"
}
channel.receiveOrFail(message = "Object updated").let {
assertIs<UpdatedList<*>>(it)
assertEquals(1, it.list.size)
assertEquals("TEST", it.list[0]!!.asRealmObject<RealmListContainer>().stringField)
}

listener.cancel()
}
}


fun RealmList<*>.removeRange(range: IntRange) {
range.reversed().forEach { index -> removeAt(index) }
}
Expand Down

0 comments on commit 4afc80d

Please sign in to comment.