From 30dcad65b7cb468d3fe6716822fd734fa2046f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus=20R=C3=B8rbech?= Date: Thu, 27 Jun 2024 13:19:02 +0200 Subject: [PATCH 1/2] Make collection in mixed round trip tests repeatable --- .../kotlin/entities/JsonStyleRealmObject.kt | 5 ++- .../common/RealmAnyNestedCollectionTests.kt | 43 ++++++++++--------- ...ealmAnyNestedCollectionNotificationTest.kt | 1 - ...ealmAnyNestedDictionaryNotificationTest.kt | 1 - .../RealmAnyNestedListNotificationTest.kt | 3 +- .../test/mongodb/common/SyncedRealmTests.kt | 27 +++++++----- 6 files changed, 43 insertions(+), 37 deletions(-) diff --git a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt index 41f1afce62..bb8eb8a34a 100644 --- a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt +++ b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt @@ -23,9 +23,10 @@ import io.realm.kotlin.types.annotations.PrimaryKey import org.mongodb.kbson.ObjectId class JsonStyleRealmObject : RealmObject { + // Act as partition key to separate individual test runs + var selector: String = "DEFAULT" @PrimaryKey @PersistedName("_id") - var id: String = ObjectId().toHexString() - var selector: String = "DEFAULT" + var id: ObjectId = ObjectId() var value: RealmAny? = null } diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt index e13edb5c8f..f3a6fb0f7a 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt @@ -30,6 +30,7 @@ import io.realm.kotlin.internal.platform.runBlocking import io.realm.kotlin.test.common.utils.assertFailsWithMessage import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.types.RealmAny +import org.mongodb.kbson.ObjectId import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test @@ -527,26 +528,26 @@ class RealmAnyNestedCollectionTests { @Test fun query() = runBlocking { + var listId: ObjectId? = null + var dictId: ObjectId? = null + var embeddedId: ObjectId? = null realm.write { - copyToRealm( + listId = copyToRealm( JsonStyleRealmObject().apply { - id = "LIST" value = realmAnyListOf(4, 5, 6) } - ) - copyToRealm( + ).id + dictId = copyToRealm( JsonStyleRealmObject().apply { - id = "DICT" value = realmAnyDictionaryOf( "key1" to 7, "key2" to 8, "key3" to 9, ) } - ) - copyToRealm( + ).id + embeddedId = copyToRealm( JsonStyleRealmObject().apply { - id = "EMBEDDED" value = realmAnyListOf( listOf(4, 5, 6), mapOf( @@ -556,35 +557,35 @@ class RealmAnyNestedCollectionTests { ) ) } - ) + ).id } assertEquals(3, realm.query().find().size) // Matching lists realm.query("value[0] == 4").find().single().run { - assertEquals("LIST", id) + assertEquals(listId, id) } realm.query("value[*] == 4").find().single().run { - assertEquals("LIST", id) + assertEquals(listId, id) } realm.query("value[*] == {4, 5, 6}").find().single().run { - assertEquals("LIST", id) + assertEquals(listId, id) } // Matching dictionaries realm.query("value.key1 == 7").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } realm.query("value['key1'] == 7").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } realm.query("value[*] == 7").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } assertEquals(0, realm.query("value.unknown == 3").find().size) realm.query("value.@keys == 'key1'").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } assertEquals(0, realm.query("value.@keys == 'unknown'").find().size) @@ -593,19 +594,19 @@ class RealmAnyNestedCollectionTests { // Matching across all elements and in nested structures realm.query("value[*][*] == 4").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[*][*] == 7").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[*].@keys == 'key1'").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[*].key3[0] == 9").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[0][*] == {4, 5, 6}").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } // FIXME Core issue https://github.com/realm/realm-core/issues/7393 // realm.query("value[*][*] == {4, 5, 6}").find().single().run { diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt index 9d1c69dc25..bc7e30da2d 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt @@ -71,7 +71,6 @@ class RealmAnyNestedCollectionNotificationTest { val o: JsonStyleRealmObject = realm.write { copyToRealm( JsonStyleRealmObject().apply { - id = "SET" value = realmAnyListOf(realmAnyListOf(1, 2, 3)) } ) diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt index 0b25592dd4..baf7aee88a 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt @@ -84,7 +84,6 @@ class RealmAnyNestedDictionaryNotificationTest : FlowableTests, DeletableEntityN val o: JsonStyleRealmObject = realm.write { copyToRealm( JsonStyleRealmObject().apply { - id = "DICTIONARY" value = realmAnyDictionaryOf( "root" to realmAnyDictionaryOf( "key1" to 1, diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt index fab126948e..e3a263419e 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt @@ -84,7 +84,6 @@ class RealmAnyNestedListNotificationTest : FlowableTests, DeletableEntityNotific val o: JsonStyleRealmObject = realm.write { copyToRealm( JsonStyleRealmObject().apply { - id = "LIST" value = realmAnyListOf(realmAnyListOf(1, 2, 3)) } ) @@ -281,7 +280,7 @@ class RealmAnyNestedListNotificationTest : FlowableTests, DeletableEntityNotific val asList = findLatest(parent)!!.value!!.asList() println(asList.size) asList.add( - RealmAny.create(JsonStyleRealmObject().apply { id = "CHILD" }) + RealmAny.create(JsonStyleRealmObject()) ) } channel.receiveOrFail(message = "List add").let { diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt index 47b248ce12..239d1254b5 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt @@ -727,6 +727,8 @@ class SyncedRealmTests { // Create object with all types val selector = ObjectId().toString() + var parentId: ObjectId? = null + var childId: ObjectId? = null createFlexibleSyncConfig( user = user1, @@ -738,14 +740,13 @@ class SyncedRealmTests { realm.write { val child = ( JsonStyleRealmObject().apply { - this.id = "CHILD" this.selector = selector } ) + childId = child.id - copyToRealm( + parentId = copyToRealm( JsonStyleRealmObject().apply { - this.id = "PARENT" this.selector = selector value = realmAnyDictionaryOf( "primitive" to 1, @@ -754,7 +755,7 @@ class SyncedRealmTests { "dictionary" to realmAnyDictionaryOf("dictkey1" to 1, "dictkey2" to "Realm", "dictkey3" to child, "dictkey4" to realmAnyListOf(1, 2, 3)) ) } - ) + ).id } realm.syncSession.uploadAllLocalChangesOrFail() } @@ -766,31 +767,37 @@ class SyncedRealmTests { } ).let { config -> Realm.open(config).use { realm -> + println("download changes") realm.syncSession.downloadAllServerChanges(10.seconds) - val flow = realm.query("_id = $0", "PARENT").asFlow() + println("downloaded changes") + val flow = realm.query("_id = $0", parentId).asFlow() val parent = withTimeout(10.seconds) { - flow.first { + println("get object") + val x = flow.first { it.list.size >= 1 }.list[0] + println("x") + x } + println("get parent") parent.let { val value = it.value!!.asDictionary() assertEquals(RealmAny.Companion.create(1), value["primitive"]) value["list"]!!.asList().let { assertEquals(1, it[0]!!.asInt()) assertEquals("Realm", it[1]!!.asString()) - assertEquals("CHILD", it[2]!!.asRealmObject().id) + assertEquals(childId, it[2]!!.asRealmObject().id) it[3]!!.asDictionary().let { dict -> assertEquals(1, dict["listkey1"]!!.asInt()) assertEquals("Realm", dict["listkey2"]!!.asString()) - assertEquals("CHILD", dict["listkey3"]!!.asRealmObject().id) + assertEquals(childId, dict["listkey3"]!!.asRealmObject().id) } - assertEquals("CHILD", it[2]!!.asRealmObject().id) + assertEquals(childId, it[2]!!.asRealmObject().id) } value["dictionary"]!!.asDictionary().let { assertEquals(1, it["dictkey1"]!!.asInt()) assertEquals("Realm", it["dictkey2"]!!.asString()) - assertEquals("CHILD", it["dictkey3"]!!.asRealmObject().id) + assertEquals(childId, it["dictkey3"]!!.asRealmObject().id) it["dictkey4"]!!.asList().let { assertEquals(realmAnyListOf(1, 2, 3).asList(), it) } From ce040e7fe635c7dc2afd2942ef092579a7695e20 Mon Sep 17 00:00:00 2001 From: Clemente Date: Thu, 27 Jun 2024 15:21:30 +0200 Subject: [PATCH 2/2] Add missing dependency --- buildSrc/src/main/kotlin/Config.kt | 2 +- packages/test-sync/build.gradle.kts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/kotlin/Config.kt b/buildSrc/src/main/kotlin/Config.kt index 7029d7c97e..749866c7c2 100644 --- a/buildSrc/src/main/kotlin/Config.kt +++ b/buildSrc/src/main/kotlin/Config.kt @@ -133,7 +133,7 @@ object Versions { const val nexusPublishPlugin = "1.1.0" // https://github.com/gradle-nexus/publish-plugin const val okio = "3.2.0" // https://square.github.io/okio/#releases const val relinker = "1.4.5" // https://github.com/KeepSafe/ReLinker - const val serialization = "1.7.0" // https://kotlinlang.org/docs/releases.html#release-details + const val serialization = "1.7.1" // https://kotlinlang.org/docs/releases.html#release-details const val shadowJar = "6.1.0" // https://mvnrepository.com/artifact/com.github.johnrengelman.shadow/com.github.johnrengelman.shadow.gradle.plugin?repo=gradle-plugins const val snakeYaml = "1.33" // https://github.com/snakeyaml/snakeyaml val sourceCompatibilityVersion = JavaVersion.VERSION_1_8 // Language level of any Java source code. diff --git a/packages/test-sync/build.gradle.kts b/packages/test-sync/build.gradle.kts index 8bf974864c..a374182607 100644 --- a/packages/test-sync/build.gradle.kts +++ b/packages/test-sync/build.gradle.kts @@ -101,6 +101,7 @@ kotlin { implementation("io.ktor:ktor-client-logging:${Versions.ktor}") implementation("io.ktor:ktor-serialization-kotlinx-json:${Versions.ktor}") implementation("io.ktor:ktor-client-content-negotiation:${Versions.ktor}") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.serialization}") implementation("com.squareup.okio:okio:${Versions.okio}") }