Skip to content
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

Added test for list notification for object changes #1635

Closed
wants to merge 2 commits into from
Closed
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 @@ -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,6 +36,8 @@ 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
Expand Down Expand Up @@ -734,6 +737,81 @@ 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
Loading