Skip to content

Commit

Permalink
Iniital draft
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Sep 13, 2023
1 parent e0d6541 commit 8bfb325
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,100 +11,195 @@ import io.realm.kotlin.mongodb.syncSession
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import org.mongodb.kbson.ObjectId
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds

// :replace-start: {
// "terms": {
// "TMP_PATH": "\"my-directory-path\"",
// "local2": "local",
// "yourFlexAppId": "YOUR_APP_ID"
// }
// }

class OpenARealmTest: RealmTest() {

class Toad: RealmObject {
// :snippet-start: open-realm-object-schemas
class Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
}

class Person : RealmObject {
var _id: Int? = null
var name: String = ""
}
// :snippet-end:

@AfterTest
fun cleanup() {

}

@Test
fun openAndCloseARealmTest() {
fun openAndCloseDefaultRealmTest() {
runBlocking {
// :snippet-start: open-a-realm
val config = RealmConfiguration.Builder(setOf(Toad::class))
// :remove-start:
.directory("/tmp/") // default location for jvm is... in the project root
// :remove-end:
.build()
// :snippet-start: open-a-default-realm
// Creates a realm with default configuration values
val config = RealmConfiguration.create(
// Pass object classes for the realm schema
schema = setOf(Frog::class, Person::class)
)

// Open the realm with the configuration object
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")
// :snippet-end:
assertTrue(config.path.contains("default.realm"))
assertTrue(config.schema.contains(Frog::class))
// :snippet-start: close-a-realm
realm.close()
// :snippet-end:
assertTrue(realm.isClosed())
Realm.deleteRealm(config)
}
}

@Test
fun openConfiguredRealmTest() {
runBlocking {
// :snippet-start: open-a-realm
// Create a realm configuration with optional arguments
val config = RealmConfiguration.Builder(
schema = setOf(Frog::class, Person::class)
)
.name("my-realm.realm")
.directory(TMP_PATH)
.build()

// Open the realm with the configuration object
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")
// :snippet-end:
// :snippet-start: find-realm-path
val realmPath = realm.configuration.path
Log.v("Realm path: $realmPath")
// :snippet-end:
assertTrue(realmPath.contains("tmp"))
assertEquals("my-realm.realm", config.name)
realm.close()
assertTrue(realm.isClosed())
Realm.deleteRealm(config)
}
}

@Test
fun deleteRealmIfMigrationNeeded() {
val REALM_NAME = getRandom()
runBlocking {
// :snippet-start: delete-realm-if-migration-needed
val config = RealmConfiguration.Builder(setOf(Toad::class))
// :remove-start:
.name(REALM_NAME)
// :remove-end:
val config = RealmConfiguration.Builder(
schema = setOf(Frog::class)
)
.deleteRealmIfMigrationNeeded()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")
// :snippet-end:
assertTrue(config.deleteRealmIfMigrationNeeded)
realm.close()
assertTrue(realm.isClosed())
Realm.deleteRealm(config)
}
}

@Test
fun onAnInMemoryRealm() {
fun openAnInMemoryRealmTest() {
runBlocking {
// :snippet-start: open-an-in-memory-realm
val config = RealmConfiguration.Builder(setOf(Toad::class))
// Create the in-memory realm configuration
val config = RealmConfiguration.Builder(
schema = setOf(Frog::class, Person::class)
)
.inMemory()
.build()

// Open the realm with the configuration object
val realm = Realm.open(config)
Log.v("Successfully opened an in memory realm")
Log.v("Successfully opened an in-memory realm")
// :snippet-end:
assertTrue(config.inMemory)
realm.close()
assertTrue(realm.isClosed())
Realm.deleteRealm(config)
}
}

@Test
fun initialDataCallbackTest() {
runBlocking {
// :snippet-start: initial-data-callback
val config = RealmConfiguration.Builder(
schema = setOf(Frog::class)
)
.initialData {
copyToRealm(Frog().apply {
name = "Kermit"
})
}
.inMemory() // :remove:
.build()

val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")

val initialFrog = realm.query<Frog>().find().first()
Log.v("Realm initialized with: ${initialFrog.name}" )
// :snippet-end:
assertEquals(1, realm.query<Frog>().find().size)
assertEquals("Kermit", initialFrog.name)
realm.close()
assertTrue(realm.isClosed())
Realm.deleteRealm(config)
}
}

@Test
fun syncToLocalRealm() {
val credentials = Credentials.anonymous(reuseExisting = false)
// :snippet-start: sync-to-local-realm
// Instantiate the synced realm with your App ID
val app = App.create(yourFlexAppId)

runBlocking {
val user = app.login(Credentials.anonymous())
val user = app.login(credentials)
// Create the synced realm configuration
val syncConfig = SyncConfiguration.Builder(user, setOf(Toad::class))
val syncConfig = SyncConfiguration.Builder(user, setOf(Frog::class))
.initialSubscriptions { realm ->
add(realm.query<Toad>(),"subscription name")
add(realm.query<Frog>(),"all-frogs")
}
.build()

// Open the synced realm and add data to it
val syncRealm = Realm.open(syncConfig)
Log.v("Successfully opened realm: ${syncRealm.configuration}")
Log.v("Successfully opened realm: ${syncRealm.configuration.name}")

syncRealm.write {
this.copyToRealm(Toad().apply {
this.copyToRealm(Frog().apply {
name = "Kermit"
})
}
// Wait for write to sync
syncRealm.syncSession.uploadAllLocalChanges(30.seconds)
// :remove-start:
val syncFrog = syncRealm.query<Frog>().find().first()
assertEquals("Kermit", syncFrog.name)
// :remove-end:

// Create the local realm
val localConfig = RealmConfiguration.Builder(setOf(Toad::class))
val localConfig = RealmConfiguration.Builder(setOf(Frog::class))
.name("local.realm")
.build()
// Copy data from synced realm to the new realm
Expand All @@ -115,13 +210,19 @@ class OpenARealmTest: RealmTest() {
// Open the new local realm
val localRealm = Realm.open(localConfig)

// Copied Toad object is available in the new realm
val toad: Toad =
localRealm.query<Toad>().find().first()
Log.v("Copied Toad: ${toad.name}")
// Copied Frog object is available in the new realm
val frog = localRealm.query<Frog>().find().first()
Log.v("Copied Frog: ${frog.name}")
assertEquals("Kermit", frog.name) // :remove:

localRealm.close()
Realm.deleteRealm(localConfig) // :remove:
// :remove-start:
assertTrue(syncRealm.isClosed())
assertTrue(localRealm.isClosed())
app.close()
Realm.deleteRealm(localConfig)
//Realm.deleteRealm(syncConfig) // [RLM_ERR_DELETE_OPENED_REALM]: Cannot delete files of an open Realm
// :remove-end:
}
// :snippet-end:
}
Expand All @@ -130,21 +231,27 @@ class OpenARealmTest: RealmTest() {
// :snippet-start: in-memory-to-local-realm
runBlocking {
// Create the in-memory realm
val inMemoryConfig = RealmConfiguration.Builder(setOf(Toad::class))
val inMemoryConfig = RealmConfiguration.Builder(setOf(Frog::class))
.name("inMemory.realm")
.inMemory()
.build()

// Open the realm and add data to it
val inMemoryRealm = Realm.open(inMemoryConfig)
assertTrue(inMemoryRealm.configuration.inMemory) // :remove:

inMemoryRealm.write {
this.copyToRealm(Toad().apply {
this.copyToRealm(Frog().apply {
name = "Kermit"
})
}
// :remove-start:
val inMemoryFrog = inMemoryRealm.query<Frog>().find().first()
assertEquals("Kermit", inMemoryFrog.name)
// :remove-end:

// Create the local realm
val localConfig = RealmConfiguration.Builder(setOf(Toad::class))
val localConfig = RealmConfiguration.Builder(setOf(Frog::class))
.name("local2.realm")
.build()
// Copy data from `inMemoryRealm` to the new realm
Expand All @@ -155,41 +262,46 @@ class OpenARealmTest: RealmTest() {
// Open the new local realm
val localRealm = Realm.open(localConfig)

// Copied Toad object is available in the new realm
val toad: Toad =
localRealm.query<Toad>().find().first()
Log.v("Copied Toad: ${toad.name}")
// Copied Frog object is available in the new realm
val frog = localRealm.query<Frog>().find().first()
Log.v("Copied Frog: ${frog.name}")
assertEquals("Kermit", frog.name) // :remove:

localRealm.close()
Realm.deleteRealm(inMemoryConfig) // :remove:
Realm.deleteRealm(localConfig) // :remove:
// :remove-start:
assertTrue(inMemoryRealm.isClosed())
assertTrue(localRealm.isClosed())
Realm.deleteRealm(inMemoryConfig)
Realm.deleteRealm(localConfig)
// :remove-end:
}
// :snippet-end:
}
@Test
fun unencryptedToEncryptedRealm() {
val encryptionKey = getEncryptionKey()
// :snippet-start: unencrypted-to-encrypted-realm
runBlocking {
// Create the unencrypted realm
val unencryptedConfig = RealmConfiguration.Builder(setOf(Toad::class))
val unencryptedConfig = RealmConfiguration.Builder(setOf(Frog::class))
.name("unencrypted.realm")
.build()

// Open the realm and add data to it
val unencryptedRealm = Realm.open(unencryptedConfig)

unencryptedRealm.write {
this.copyToRealm(Toad().apply {
this.copyToRealm(Frog().apply {
name = "Kermit"
})
}

// ... Generate encryption key ...

// Create the encrypted realm
val encryptedConfig = RealmConfiguration.Builder(setOf(Toad::class))
val encryptedConfig = RealmConfiguration.Builder(setOf(Frog::class))
.name("encrypted.realm")
.encryptionKey(getEncryptionKey())
.encryptionKey(encryptionKey)
.build()

// Copy data from `unencryptedRealm` to the new realm
Expand All @@ -202,16 +314,22 @@ class OpenARealmTest: RealmTest() {
// Open the new encrypted realm
val encryptedRealm = Realm.open(encryptedConfig)

// Copied Toad object is available in the new realm
val toad: Toad =
encryptedRealm.query<Toad>().find().first()
Log.v("Copied Toad: ${toad.name}")

// Copied Frog object is available in the new realm
val frog = encryptedRealm.query<Frog>().find().first()
Log.v("Copied Frog: ${frog.name}")
// :remove-start:
assertEquals("Kermit", frog.name)
assertEquals(encryptionKey, encryptedRealm.configuration.encryptionKey)
// :remove-end:
encryptedRealm.close()
Realm.deleteRealm(unencryptedConfig) // :remove:
Realm.deleteRealm(encryptedConfig) // :remove:
// :remove-start:
assertTrue(encryptedRealm.isClosed())
assertTrue(unencryptedRealm.isClosed())
Realm.deleteRealm(unencryptedConfig)
Realm.deleteRealm(encryptedConfig)
// :remove-end:
}
// :snippet-end:
}
}
// :replace-end:
// :replace-end:
1 change: 0 additions & 1 deletion snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ toc_landing_pages = [
"/sdk/kotlin/users",
"/sdk/kotlin/realm-database",
"/sdk/kotlin/realm-database/schemas",
"/sdk/kotlin/realm-database/realm-files",
"/sdk/kotlin/realm-database/write-transactions",
"/sdk/kotlin/sync",
"/sdk/web",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
val config = RealmConfiguration.Builder(setOf(Toad::class))
val config = RealmConfiguration.Builder(
schema = setOf(Frog::class)
)
.deleteRealmIfMigrationNeeded()
.build()
val realm = Realm.open(config)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
val realmPath = realm.configuration.path
Log.v("Realm path: $realmPath")
Loading

0 comments on commit 8bfb325

Please sign in to comment.