Skip to content

Commit

Permalink
Fix RealmSet test and update snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Aug 9, 2023
1 parent 09a957e commit 4239901
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,53 +106,6 @@ class CreateTest: RealmTest() {
realm.close()
}
}

@Test
fun createToOneRelationship() {
class Owner : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
}
// :snippet-start: define-to-one-relationship
class Frog : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
var owner: Owner? = null
}
// :snippet-end:

runBlocking {
val config = RealmConfiguration.Builder(
schema = setOf(Frog::class, Owner::class) // Pass the defined class as the object schema
)
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")

realm.write {
val frogs = query<Frog>().find()
delete(frogs)
assertEquals(0, frogs.size)
}

// :snippet-start: create-to-one-relationship
realm.write {
copyToRealm(Frog().apply {
name = "Kermit"
owner = copyToRealm(Owner().apply {
name = "Jim Henson"
})
})
}
// :snippet-end:
val frogs = realm.query<Frog>().find()
assertEquals(1, frogs.size)
val thisFrog = frogs.first()
thisFrog.owner?.let { assertEquals("Jim Henson", it.name) }
realm.close()
}
}
}

// :replace-end:
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.internal.platform.runBlocking
import io.realm.kotlin.notifications.SetChange
import io.realm.kotlin.notifications.SetChangeSet
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.types.*
import io.realm.kotlin.types.annotations.Ignore
import io.realm.kotlin.types.annotations.Index
import io.realm.kotlin.types.annotations.PersistedName
import io.realm.kotlin.types.annotations.PrimaryKey
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.datetime.Instant
import org.mongodb.kbson.ObjectId
Expand Down Expand Up @@ -222,6 +221,7 @@ class SchemaTest: RealmTest() {
realm.close()
}
}

@Test
fun createRealmSetTypes() {
runBlocking {
Expand All @@ -230,27 +230,7 @@ class SchemaTest: RealmTest() {
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")
val kermitFrog = realm.query<Frog2>("name = 'Kermit'").first().find()
val kermitSnacks = kermitFrog?.favoriteSnacks
val job = CoroutineScope(Dispatchers.Default).launch {
kermitSnacks
?.asFlow()
?.collect() { changes: SetChange<Snack> ->
when (changes) {
is SetChangeSet -> {
changes.insertions
Log.v("Set of snacks changed: $changes")
}
else -> {
Log.v("Set of snacks unchanged")
}

}
val set = kermitFrog.favoriteSnacks

assertTrue(set.last().name.equals("new snack"))
}
}

// :snippet-start: add-item-to-realm-set
realm.write {
// Create a Frog object named 'Kermit' that will have a RealmSet of favorite snacks
Expand All @@ -274,7 +254,7 @@ class SchemaTest: RealmTest() {
// :snippet-start: add-all-to-realm-set
realm.write {
val myFrog = query<Frog2>("name = 'Kermit'").find().first()
val set = myFrog.favoriteSnacks
val set = findLatest(myFrog)!!.favoriteSnacks

val cricketsSnack = copyToRealm(Snack().apply {
name = "crickets"
Expand All @@ -287,7 +267,6 @@ class SchemaTest: RealmTest() {
})

set.addAll(setOf(cricketsSnack, earthWormsSnack, waxWormsSnack))
println("$set") // :remove:
assertEquals(4, set.size) // :remove:
// :uncomment-start:
//}
Expand All @@ -301,36 +280,83 @@ class SchemaTest: RealmTest() {

// :snippet-start: set-contains-multiple-items
val containsAllSnacks = set.containsAll(set)
Log.v("Does Kermit eat crickets, earthworms, and wax worms?: $containsAllSnacks") // true
Log.v("Does Kermit eat crickets, earthworms, and waxworms?: $containsAllSnacks") // true
// :snippet-end:
assertTrue(containsAllSnacks)

// :snippet-start: remove-item-from-set
val fliesSnack = query<Snack>("name = 'flies'").first().find()
val fliesSnack = query<Snack>("name == $0", "flies").first().find()

set.remove(fliesSnack)
// :snippet-end:
assertFalse(set.contains(fliesSnack))

val deleteSnacks = findLatest(myFrog)!!.favoriteSnacks
// :snippet-start: remove-multiple-items-from-set
set.removeAll(deleteSnacks)
val removeSnacks = findLatest(myFrog)!!.favoriteSnacks

val remove = set.removeAll(myFrog.favoriteSnacks)
// :snippet-end:
// TODO update test once https://github.com/realm/realm-kotlin/issues/1097 is fixed
// assertTrue(set.isEmpty())
println(remove) // prints true
println(removeSnacks.size) // prints 1
println(removeSnacks.query().find()[0].name) // prints earthworms
set.removeAll(myFrog.favoriteSnacks) // second remove works
println(removeSnacks.size) // prints 0
assertTrue(set.isEmpty()) // passes
}
realm.close()
Realm.deleteRealm(config)
}
}

// :snippet-start: react-to-changes-from-the-set
val newSnack = Snack().apply {
name = "new snack"
}
set.add(newSnack)
}
@Test
fun listenForSetChanges() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(Frog2::class, Snack::class))
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")
// capture changes to the set
val detectedChanges = mutableListOf<SetChange<Snack>>()

job.cancel()
// :snippet-end:
realm.writeBlocking {
copyToRealm(Frog2().apply {
name = "Kermit"
})
}

realm.close()
Realm.deleteRealm(config)
// :snippet-start: react-to-changes-from-the-set
val kermitFrog = realm.query<Frog2>("name = 'Kermit'").find().first()

val job = launch(Dispatchers.Default) {
kermitFrog.favoriteSnacks
.asFlow()
.collect {
// Listen for changes to the RealmSet
// :remove-start:
change ->
Log.v("Change detected: $change")
detectedChanges.add(change)
// :remove-end:
}
}
// :snippet-end:
realm.writeBlocking {
val set = findLatest(kermitFrog)!!.favoriteSnacks
val newSnack = copyToRealm(Snack().apply {
name = "chocolate"
})
set.add(newSnack)
}
delay(1000)
assertEquals(1, detectedChanges.size)
job.cancel()
realm.close()
Realm.deleteRealm(config)
}
}
}

// :replace-end:
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
realm.write {
val myFrog: Frog = realm.query<Frog>("name = 'Kermit'").first().find()!!
val myFrog = query<Frog>("name = 'Kermit'").find().first()
val set = findLatest(myFrog)!!.favoriteSnacks

val cricketsSnack = this.copyToRealm(Snack().apply {
val cricketsSnack = copyToRealm(Snack().apply {
name = "crickets"
})
val earthWormsSnack = this.copyToRealm(Snack().apply {
val earthWormsSnack = copyToRealm(Snack().apply {
name = "earthworms"
})
val waxWormsSnack = this.copyToRealm(Snack().apply {
val waxWormsSnack = copyToRealm(Snack().apply {
name = "waxworms"
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
realm.write {
// Create a Frog object named 'Kermit' that will have a RealmSet of favorite snacks
val frog = this.copyToRealm(Frog().apply {
val frog = copyToRealm(Frog().apply {
name = "Kermit"
})
// Get the RealmSet of favorite snacks from the Frog object we just created
val set = frog.favoriteSnacks

// Create a Snack object for the Frog to add to Kermit's favorite snacks
val fliesSnack = this.copyToRealm(Snack().apply {
val fliesSnack = copyToRealm(Snack().apply {
name = "flies"
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
val kermitFrog = realm.query<Frog>("name = 'Kermit'").first().find()
val job = CoroutineScope(Dispatchers.Default).launch {
kermitFrog?.favoriteSnacks
?.asFlow()
?.collect() {
val kermitFrog = realm.query<Frog>("name = 'Kermit'").find().first()

val job = launch(Dispatchers.Default) {
kermitFrog.favoriteSnacks
.asFlow()
.collect {
// Listen for changes to the RealmSet
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
val fliesSnack = realm.query<Snack>("name = 'flies'").first().find()
val fliesSnack = query<Snack>("name == $0", "flies").first().find()

set.remove(fliesSnack)
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
set.removeAll(set)
val removeSnacks = findLatest(myFrog)!!.favoriteSnacks

val remove = set.removeAll(myFrog.favoriteSnacks)
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val containsAllSnacks = set.containsAll(set)
Log.v("Does Kermit eat crickets, earthworms, and wax worms?: $containsAllSnacks") // true
Log.v("Does Kermit eat crickets, earthworms, and waxworms?: $containsAllSnacks") // true

0 comments on commit 4239901

Please sign in to comment.