forked from mongodb/docs-realm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30e51bd
commit c729876
Showing
9 changed files
with
1,042 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
258 changes: 258 additions & 0 deletions
258
examples/kotlin/shared/src/commonTest/kotlin/com/mongodb/realm/realmkmmapp/SchemaSyncTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
package com.mongodb.realm.realmkmmapp | ||
|
||
import io.realm.kotlin.Realm | ||
import io.realm.kotlin.ext.query | ||
import io.realm.kotlin.ext.realmListOf | ||
import io.realm.kotlin.mongodb.App | ||
import io.realm.kotlin.mongodb.Credentials | ||
import io.realm.kotlin.mongodb.sync.SyncConfiguration | ||
import io.realm.kotlin.mongodb.syncSession | ||
import kotlinx.coroutines.runBlocking | ||
import org.mongodb.kbson.ObjectId | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
|
||
// :replace-start: { | ||
// "terms": { | ||
// "ExampleSyncObject_": "", | ||
// "ExampleSyncRelationship_": "" | ||
// } | ||
// } | ||
class SchemaSyncTest : RealmTest() { | ||
|
||
@Test | ||
fun syncRealmObjectTest() { | ||
val credentials = Credentials.anonymous(reuseExisting = false) | ||
runBlocking { | ||
val app = App.create(FLEXIBLE_APP_ID) | ||
val user = app.login(credentials) | ||
val flexSyncConfig = SyncConfiguration.Builder(user, setOf(ExampleSyncObject_Frog::class, ExampleSyncObject_Pond::class)) | ||
.initialSubscriptions { realm -> | ||
add(realm.query<ExampleSyncObject_Frog>(), "all-frogs") | ||
add(realm.query<ExampleSyncObject_Pond>(), "all-ponds") | ||
} | ||
.build() | ||
val syncRealm = Realm.open(flexSyncConfig) | ||
Log.v("Successfully opened realm: ${syncRealm.configuration}") | ||
|
||
val frogId = ObjectId() | ||
val pondId = ObjectId() | ||
syncRealm.write { | ||
val existingFrogs = query<ExampleSyncObject_Frog>().find() | ||
delete(existingFrogs) | ||
val existingPonds = query<ExampleSyncObject_Pond>().find() | ||
delete(existingPonds) | ||
copyToRealm(ExampleSyncObject_Frog().apply { | ||
_id = frogId | ||
name = "Kermit" | ||
age = 42 | ||
}) | ||
copyToRealm(ExampleSyncObject_Pond().apply { | ||
_id = pondId | ||
name = "Frog Pond" | ||
|
||
}) | ||
} | ||
syncRealm.syncSession.uploadAllLocalChanges(30.seconds) | ||
syncRealm.write { | ||
val frog = query<ExampleSyncObject_Frog>("_id == $0", frogId).find().first() | ||
val pond = query<ExampleSyncObject_Pond>("_id == $0", pondId).find().first() | ||
delete(frog) | ||
delete(pond) | ||
} | ||
user.delete() | ||
syncRealm.close() | ||
} | ||
} | ||
|
||
@Test | ||
fun syncToOneRelationshipTest() { | ||
val credentials = Credentials.anonymous(reuseExisting = false) | ||
runBlocking { | ||
val app = App.create(FLEXIBLE_APP_ID) | ||
val user = app.login(credentials) | ||
val flexSyncConfig = SyncConfiguration.Builder(user, setOf(ExampleSyncRelationship_Frog::class, ExampleSyncRelationship_Pond::class)) | ||
.initialSubscriptions { realm -> | ||
add(realm.query<ExampleSyncRelationship_Frog>()) | ||
add(realm.query<ExampleSyncRelationship_Pond>()) | ||
} | ||
.build() | ||
val syncRealm = Realm.open(flexSyncConfig) | ||
Log.v("Successfully opened realm: ${syncRealm.configuration}") | ||
|
||
val frogId = ObjectId() | ||
val pondId = ObjectId() | ||
syncRealm.write { | ||
val existingFrogs = query<ExampleSyncRelationship_Frog>().find() | ||
delete(existingFrogs) | ||
val existingPonds = query<ExampleSyncRelationship_Pond>().find() | ||
delete(existingPonds) | ||
copyToRealm(ExampleSyncRelationship_Frog().apply { | ||
_id = frogId | ||
name = "Kermit" | ||
age = 42 | ||
favoritePond = copyToRealm(ExampleSyncRelationship_Pond().apply { | ||
_id = pondId | ||
name = "Frog Pond" | ||
}) | ||
}) | ||
} | ||
syncRealm.syncSession.uploadAllLocalChanges(30.seconds) | ||
syncRealm.write { | ||
val frog = query<ExampleSyncRelationship_Frog>("_id == $0", frogId).find().first() | ||
val pond = query<ExampleSyncRelationship_Pond>("_id == $0", pondId).find().first() | ||
delete(frog) | ||
delete(pond) | ||
} | ||
user.delete() | ||
syncRealm.close() | ||
} | ||
} | ||
|
||
@Test | ||
fun syncToManyRelationshipTest() { | ||
val credentials = Credentials.anonymous(reuseExisting = false) | ||
runBlocking { | ||
val app = App.create(FLEXIBLE_APP_ID) | ||
val user = app.login(credentials) | ||
val flexSyncConfig = SyncConfiguration.Builder(user, setOf(ExampleSyncRelationship_Frog_1::class, ExampleSyncRelationship_Pond_1::class)) | ||
.initialSubscriptions { realm -> | ||
add(realm.query<ExampleSyncRelationship_Frog_1>()) | ||
add(realm.query<ExampleSyncRelationship_Pond_1>()) | ||
} | ||
.build() | ||
val syncRealm = Realm.open(flexSyncConfig) | ||
Log.v("Successfully opened realm: ${syncRealm.configuration}") | ||
|
||
val frogId = ObjectId() | ||
val pondId = ObjectId() | ||
syncRealm.write { | ||
val existingFrogs = query<ExampleSyncRelationship_Frog_1>().find() | ||
delete(existingFrogs) | ||
val existingPonds = query<ExampleSyncRelationship_Pond_1>().find() | ||
delete(existingPonds) | ||
copyToRealm(ExampleSyncRelationship_Frog_1().apply { | ||
_id = frogId | ||
name = "Kermit" | ||
age = 42 | ||
favoritePonds = realmListOf(copyToRealm(ExampleSyncRelationship_Pond_1().apply { | ||
_id = pondId | ||
name = "Frog Pond" | ||
})) | ||
}) | ||
} | ||
syncRealm.syncSession.uploadAllLocalChanges(30.seconds) | ||
syncRealm.write { | ||
val frog = query<ExampleSyncRelationship_Frog_1>("_id == $0", frogId).find().first() | ||
val pond = query<ExampleSyncRelationship_Pond_1>("_id == $0", pondId).find().first() | ||
delete(frog) | ||
delete(pond) | ||
} | ||
user.delete() | ||
syncRealm.close() | ||
} | ||
} | ||
|
||
@Test | ||
fun syncInverseRelationshipTest() { | ||
val credentials = Credentials.anonymous(reuseExisting = false) | ||
runBlocking { | ||
val app = App.create(FLEXIBLE_APP_ID) | ||
val user = app.login(credentials) | ||
val flexSyncConfig = SyncConfiguration.Builder(user, setOf(ExampleSyncRelationship_Frog_2::class, ExampleSyncRelationship_Pond_2::class)) | ||
.initialSubscriptions { realm -> | ||
add(realm.query<ExampleSyncRelationship_Frog_2>()) | ||
add(realm.query<ExampleSyncRelationship_Pond_2>()) | ||
} | ||
.build() | ||
val syncRealm = Realm.open(flexSyncConfig) | ||
Log.v("Successfully opened realm: ${syncRealm.configuration}") | ||
|
||
val frogId = ObjectId() | ||
val pondId = ObjectId() | ||
syncRealm.write { | ||
val existingFrogs = query<ExampleSyncRelationship_Frog_2>().find() | ||
delete(existingFrogs) | ||
val existingPonds = query<ExampleSyncRelationship_Pond_2>().find() | ||
delete(existingPonds) | ||
copyToRealm(ExampleSyncRelationship_Frog_2().apply { | ||
_id = frogId | ||
name = "Kermit" | ||
age = 42 | ||
favoritePonds = realmListOf(copyToRealm(ExampleSyncRelationship_Pond_2().apply { | ||
_id = pondId | ||
name = "Frog Pond" | ||
})) | ||
}) | ||
} | ||
syncRealm.syncSession.uploadAllLocalChanges(30.seconds) | ||
syncRealm.write { | ||
val frog = query<ExampleSyncRelationship_Frog_2>("_id == $0", frogId).find().first() | ||
val pond = query<ExampleSyncRelationship_Pond_2>("_id == $0", pondId).find().first() | ||
assertEquals(pond.favoritePond.first(), frog) | ||
delete(frog) | ||
delete(pond) | ||
} | ||
user.delete() | ||
syncRealm.close() | ||
} | ||
} | ||
|
||
@Test | ||
fun syncEmbeddedRelationshipTest() { | ||
val credentials = Credentials.anonymous(reuseExisting = false) | ||
runBlocking { | ||
val app = App.create(FLEXIBLE_APP_ID) | ||
val user = app.login(credentials) | ||
val flexSyncConfig = SyncConfiguration.Builder(user, setOf(ExampleSyncRelationship_Frog_3::class, ExampleSyncRelationship_Forest::class, ExampleSyncRelationship_EmbeddedPond::class)) | ||
.initialSubscriptions { realm -> | ||
add(realm.query<ExampleSyncRelationship_Frog_3>()) | ||
add(realm.query<ExampleSyncRelationship_Forest>()) | ||
} | ||
.build() | ||
val syncRealm = Realm.open(flexSyncConfig) | ||
Log.v("Successfully opened realm: ${syncRealm.configuration}") | ||
|
||
val frogId = ObjectId() | ||
val forestId = ObjectId() | ||
syncRealm.write { | ||
val existingFrogs = query<ExampleSyncRelationship_Frog_3>().find() | ||
delete(existingFrogs) | ||
val existingForests = query<ExampleSyncRelationship_Forest>().find() | ||
delete(existingForests) | ||
val existingPonds = query<ExampleSyncRelationship_EmbeddedPond>().find() | ||
delete(existingPonds) | ||
copyToRealm(ExampleSyncRelationship_Frog_3().apply { | ||
_id = frogId | ||
name = "Kermit" | ||
age = 42 | ||
favoritePond = ExampleSyncRelationship_EmbeddedPond().apply { | ||
name = "Frog Pond" | ||
} | ||
}) | ||
copyToRealm(ExampleSyncRelationship_Forest().apply { | ||
_id = forestId | ||
name = "Frog Forest" | ||
forestPonds = realmListOf(ExampleSyncRelationship_EmbeddedPond().apply { | ||
name = "Frog Pond" | ||
}) | ||
}) | ||
} | ||
syncRealm.syncSession.uploadAllLocalChanges(30.seconds) | ||
syncRealm.write { | ||
val frog = query<ExampleSyncRelationship_Frog_3>("_id == $0", frogId).find().first() | ||
val forest = query<ExampleSyncRelationship_Forest>("_id == $0", forestId).find().first() | ||
assertEquals(frog.favoritePond?.name, "Frog Pond") | ||
assertEquals(forest.forestPonds.first().name, "Frog Pond") | ||
delete(frog) | ||
delete(forest) | ||
} | ||
user.delete() | ||
syncRealm.close() | ||
} | ||
} | ||
} | ||
|
||
// :replace-end: |
Oops, something went wrong.