Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Sep 19, 2023
1 parent 975c063 commit 10d853c
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class CreateTest: RealmTest() {
val config = RealmConfiguration.Builder(
schema = setOf(RealmDictionary_Frog::class) // Pass the defined class as the object schema
)
.directory("/tmp/") // default location for jvm is... in the project root
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")
Expand Down Expand Up @@ -114,7 +114,7 @@ class CreateTest: RealmTest() {
val config = RealmConfiguration.Builder(
schema = setOf(RealmDictionary_Frog::class) // Pass the defined class as the object schema
)
.directory("/tmp/") // default location for jvm is... in the project root
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import io.realm.kotlin.ext.*
import io.realm.kotlin.internal.platform.runBlocking
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.types.*
import kotlinx.datetime.Instant
import org.mongodb.kbson.Decimal128
import org.mongodb.kbson.ObjectId
import kotlin.test.*

class CustomObjectType : RealmObject {
var uuid: RealmUUID = RealmUUID.random()
}
Expand Down Expand Up @@ -172,51 +172,6 @@ class RealmSupportedTypes : RealmObject {
// :snippet-end:
}

// :snippet-start: timestamp-workaround
// model class that stores an Instant (kotlinx-datetime) field as a RealmInstant via a conversion
class RealmInstantConversion : RealmObject {
private var _timestamp: RealmInstant = RealmInstant.from(0, 0)
public var timestamp: Instant
get() {
return _timestamp.toInstant()
}
set(value) {
_timestamp = value.toRealmInstant()
}
}

fun RealmInstant.toInstant(): Instant {
val sec: Long = this.epochSeconds
// The value always lies in the range `-999_999_999..999_999_999`.
// minus for timestamps before epoch, positive for after
val nano: Int = this.nanosecondsOfSecond

return if (sec >= 0) { // For positive timestamps, conversion can happen directly
Instant.fromEpochSeconds(sec, nano.toLong())
} else {
// For negative timestamps, RealmInstant starts from the higher value with negative
// nanoseconds, while Instant starts from the lower value with positive nanoseconds
// TODO This probably breaks at edge cases like MIN/MAX
Instant.fromEpochSeconds(sec - 1, 1_000_000 + nano.toLong())
}
}

fun Instant.toRealmInstant(): RealmInstant {
val sec: Long = this.epochSeconds
// The value is always positive and lies in the range `0..999_999_999`.
val nano: Int = this.nanosecondsOfSecond

return if (sec >= 0) { // For positive timestamps, conversion can happen directly
RealmInstant.from(sec, nano)
} else {
// For negative timestamps, RealmInstant starts from the higher value with negative
// nanoseconds, while Instant starts from the lower value with positive nanoseconds
// TODO This probably breaks at edge cases like MIN/MAX
RealmInstant.from(sec + 1, -1_000_000 + nano)
}
}
// :snippet-end:

class SupportedDataTypesTest : RealmTest() {
@Test
fun populateEnumPropertiesTest() {
Expand Down Expand Up @@ -362,6 +317,7 @@ class SupportedDataTypesTest : RealmTest() {
realm.close()
}
}

@Test
fun populateBSONPropertiesWithValuesTest() {
runBlocking {
Expand Down Expand Up @@ -393,6 +349,7 @@ class SupportedDataTypesTest : RealmTest() {
realm.close()
}
}

@Test
fun populateRealmDefaultPropertiesTest() {
runBlocking {
Expand Down Expand Up @@ -421,10 +378,12 @@ class SupportedDataTypesTest : RealmTest() {
realm.close()
}
}

@Test
fun populateRealmPropertiesWithValuesTest() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(RealmSupportedTypes::class, CustomObjectType::class))
.name("populatePropertiesWithValues.realm")
.inMemory()
.build()
val realm = Realm.open(config)
Expand Down Expand Up @@ -477,6 +436,7 @@ class SupportedDataTypesTest : RealmTest() {
realm.close()
}
}

@Test
fun populateRealmObjectDefaultPropertiesTest() {
runBlocking {
Expand Down Expand Up @@ -509,10 +469,12 @@ class SupportedDataTypesTest : RealmTest() {
realm.close()
}
}

@Test
fun populateRealmObjectPropertiesWithValuesTest() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(EmbeddedObjectType::class, AnotherCustomObjectType::class, CustomObjectType::class))
.name("populateObjectPropertiesWithValues.realm")
.inMemory()
.build()
val realm = Realm.open(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import org.mongodb.kbson.ObjectId
// "terms": {
// "ExampleRealmObject_": "",
// "RealmList_": "",
// "ExampleRealmDictionary_": "",
// "RealmDictionary_": "",
// "ExampleRealmSet_": "",
// "RealmSet_": ""
// }
// }

/*
** Define Realm Object Model page examples **
* Tested in SchemaTest.kt file
*/

// :snippet-start: define-realm-object
Expand Down Expand Up @@ -74,43 +77,62 @@ class RealmList_Pond : RealmObject {
}
// :snippet-end:

/*
Tested in Create, Read, Update, and Delete.kt files
*/

// :snippet-start: define-a-realm-set
// RealmSet<E> can be any supported primitive or
// BSON type or a RealmObject
class RealmSet_Frog : RealmObject {
class ExampleRealmSet_Frog : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
// Set of RealmObject type (CANNOT be nullable)
var favoriteSnacks: RealmSet<RealmSet_Snack> = realmSetOf()
var favoriteSnacks: RealmSet<ExampleRealmSet_Snack> = realmSetOf()
// Set of primitive type (can be nullable)
var favoriteWeather: RealmSet<String?> = realmSetOf()
}

class RealmSet_Snack : RealmObject {
class ExampleRealmSet_Snack : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
}
// :snippet-end:

/*
Tested in Create, Read, Update, and Delete.kt files
*/
class RealmSet_Frog : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""

var favoriteSnacks: RealmSet<RealmSet_Snack> = realmSetOf()
}

class RealmSet_Snack : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
}

// :snippet-start: define-realm-dictionary-property
// RealmDictionary<K, V> can be any supported
// primitive or BSON types, a RealmObject, or
// an EmbeddedRealmObject
class RealmDictionary_Frog : RealmObject {
class ExampleRealmDictionary_Frog : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
// Dictionary of RealmObject type (value MUST be nullable)
var favoriteFriendsByForest: RealmDictionary<RealmDictionary_Frog?> = realmDictionaryOf()
var favoriteFriendsByPond: RealmDictionary<ExampleRealmDictionary_Frog?> = realmDictionaryOf()
// Dictionary of EmbeddedRealmObject type (value MUST be nullable)
var favoriteForestsByForest: RealmDictionary<ExampleRealmObject_Forest?> = realmDictionaryOf()
var favoriteTreesInForest: RealmDictionary<ExampleRealmObject_Forest?> = realmDictionaryOf()
// Dictionary of primitive type (value can be nullable)
var favoritePondsByForest: RealmDictionary<String?> = realmDictionaryOf()
}
// :snippet-end:
/*
Tested in Create, Read, Update, and Delete.kt files
*/
class RealmDictionary_Frog : RealmObject {
var _id: ObjectId = ObjectId()
var name: String = ""
var favoriteFriendsByForest: RealmDictionary<RealmDictionary_Frog?> = realmDictionaryOf()
var favoritePondsByForest: RealmDictionary<String?> = realmDictionaryOf()
}

// :replace-end:
Loading

0 comments on commit 10d853c

Please sign in to comment.