From 2737411f3ecfdbf79ae056d55adab22b349eeefe Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Mon, 22 Jul 2024 16:00:23 +0200 Subject: [PATCH] Add test for merging new objects --- Tests/Realm.Tests/Database/RealmValueTests.cs | 62 +++++++-------- .../Database/RealmValueWithCollections.cs | 12 +-- Tests/Realm.Tests/Database/TestObjects.cs | 14 ++-- .../Sync/DataTypeSynchronizationTests.cs | 76 ++++++++++++++++++- 4 files changed, 118 insertions(+), 46 deletions(-) diff --git a/Tests/Realm.Tests/Database/RealmValueTests.cs b/Tests/Realm.Tests/Database/RealmValueTests.cs index e273d0d1ba..4faa83b7d6 100644 --- a/Tests/Realm.Tests/Database/RealmValueTests.cs +++ b/Tests/Realm.Tests/Database/RealmValueTests.cs @@ -16,17 +16,17 @@ // //////////////////////////////////////////////////////////////////////////// +#if TEST_WEAVER +using TestRealmObject = Realms.RealmObject; +#else +using TestRealmObject = Realms.IRealmObject; +#endif using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using MongoDB.Bson; using NUnit.Framework; -#if TEST_WEAVER -using TestRealmObject = Realms.RealmObject; -#else -using TestRealmObject = Realms.IRealmObject; -#endif namespace Realms.Tests.Database { @@ -37,22 +37,22 @@ public class RealmValueTests : RealmInstanceTest private static readonly DateTimeOffset _someDate = new(1234, 5, 6, 7, 8, 9, TimeSpan.Zero); - public static char[] CharValues = new[] { (char)0, 'a', 'b', char.MinValue }; - public static byte[] ByteValues = new[] { (byte)0, (byte)1, byte.MaxValue, byte.MinValue }; - public static int[] IntValues = new[] { 0, 1, -1, int.MaxValue, int.MinValue }; - public static short[] ShortValues = new[] { (short)0, (short)1, (short)-1, short.MaxValue, short.MinValue }; - public static long[] LongValues = new[] { 0, 1, -1, long.MaxValue, long.MinValue }; - public static float[] FloatValues = new[] { 0, 1, -1, float.MaxValue, float.MinValue }; - public static double[] DoubleValues = new[] { 0, 1, -1, double.MaxValue, double.MinValue }; - public static Decimal128[] Decimal128Values = new[] { 0, 1, -1, Decimal128.MaxValue, Decimal128.MinValue }; - public static decimal[] DecimalValues = new[] { 0, 1, -1, decimal.MaxValue, decimal.MinValue }; - public static bool[] BoolValues = new[] { false, true }; - public static DateTimeOffset[] DateValues = new[] { _someDate, DateTimeOffset.MaxValue, DateTimeOffset.MinValue }; - public static Guid[] GuidValues = new[] { Guid.Parse("3809d6d9-7618-4b3d-8044-2aa35fd02f31"), Guid.Empty }; - public static ObjectId[] ObjectIdValues = new[] { new ObjectId("5f63e882536de46d71877979"), ObjectId.Empty }; - public static string[] StringValues = new[] { "a", "abc", string.Empty }; - public static byte[][] DataValues = new[] { new byte[] { 0, 1, 2 }, Array.Empty() }; - public static IRealmObject[] ObjectValues = new[] { new InternalObject { IntProperty = 10, StringProperty = "brown" } }; + public static char[] CharValues = { (char)0, 'a', 'b', char.MinValue }; + public static byte[] ByteValues = { 0, 1, byte.MaxValue, byte.MinValue }; + public static int[] IntValues = { 0, 1, -1, int.MaxValue, int.MinValue }; + public static short[] ShortValues = { 0, 1, -1, short.MaxValue, short.MinValue }; + public static long[] LongValues = { 0, 1, -1, long.MaxValue, long.MinValue }; + public static float[] FloatValues = { 0, 1, -1, float.MaxValue, float.MinValue }; + public static double[] DoubleValues = { 0, 1, -1, double.MaxValue, double.MinValue }; + public static Decimal128[] Decimal128Values = { 0, 1, -1, Decimal128.MaxValue, Decimal128.MinValue }; + public static decimal[] DecimalValues = { 0, 1, -1, decimal.MaxValue, decimal.MinValue }; + public static bool[] BoolValues = { false, true }; + public static DateTimeOffset[] DateValues = { _someDate, DateTimeOffset.MaxValue, DateTimeOffset.MinValue }; + public static Guid[] GuidValues = { Guid.Parse("3809d6d9-7618-4b3d-8044-2aa35fd02f31"), Guid.Empty }; + public static ObjectId[] ObjectIdValues = { new("5f63e882536de46d71877979"), ObjectId.Empty }; + public static string[] StringValues = { "a", "abc", string.Empty }; + public static byte[][] DataValues = { new byte[] { 0, 1, 2 }, Array.Empty() }; + public static IRealmObject[] ObjectValues = { new InternalObject { IntProperty = 10, StringProperty = "brown" } }; [Test] public void CharTests( @@ -654,9 +654,10 @@ public void RealmValue_WhenCastingIsWrong_ThrowsException() [Test] public void RealmValue_Reference_IsChangedCorrectly() { - var rvo = new RealmValueObject(); - - rvo.RealmValueProperty = 10; + var rvo = new RealmValueObject + { + RealmValueProperty = 10 + }; _realm.Write(() => { @@ -677,9 +678,10 @@ public void RealmValue_Reference_IsChangedCorrectly() [Test] public void RealmValue_WhenManaged_CanChangeType() { - var rvo = new RealmValueObject(); - - rvo.RealmValueProperty = 10; + var rvo = new RealmValueObject + { + RealmValueProperty = 10 + }; _realm.Write(() => { @@ -717,7 +719,7 @@ public void RealmValue_WhenManaged_NotificationTests() { var notifiedPropertyNames = new List(); - var handler = new PropertyChangedEventHandler((sender, e) => + var handler = new PropertyChangedEventHandler((_, e) => { notifiedPropertyNames.Add(e.PropertyName); }); @@ -759,7 +761,7 @@ public void RealmValue_WhenManaged_BoolNotificationTests([Values(0, 1)] int intV { var notifiedPropertyNames = new List(); - var handler = new PropertyChangedEventHandler((sender, e) => + var handler = new PropertyChangedEventHandler((_, e) => { notifiedPropertyNames.Add(e.PropertyName); }); @@ -1180,6 +1182,6 @@ public bool Equals(InternalObject? other) => other != null && IntProperty == other.IntProperty && StringProperty == other.StringProperty; - public override string? ToString() => $"{IntProperty} - {StringProperty}"; + public override string ToString() => $"{IntProperty} - {StringProperty}"; } } diff --git a/Tests/Realm.Tests/Database/RealmValueWithCollections.cs b/Tests/Realm.Tests/Database/RealmValueWithCollections.cs index 436746b165..0c7d6621ad 100644 --- a/Tests/Realm.Tests/Database/RealmValueWithCollections.cs +++ b/Tests/Realm.Tests/Database/RealmValueWithCollections.cs @@ -29,10 +29,10 @@ internal class RealmValueWithCollections : RealmInstanceTest { private readonly RealmValueComparer _rvComparer = new(); - public static Func> ListGenerator = i => new List { $"inner{i}", i }; - public static Func> DictGenerator = i => new Dictionary { { "s1", i }, { "s2", $"ah{i}" } }; + private static readonly Func> ListGenerator = i => new List { $"inner{i}", i }; + private static readonly Func> DictGenerator = i => new Dictionary { { "s1", i }, { "s2", $"ah{i}" } }; - public static Func[] CollectionGenerators = new Func[] + public static Func[] CollectionGenerators = { i => (RealmValue)ListGenerator(i), i => (RealmValue)DictGenerator(i), @@ -139,12 +139,13 @@ public void List_InRealmValue_Equality([Values(true, false)] bool isManaged) rv = PersistAndFind(rv).RealmValueProperty; } + // ReSharper disable once EqualExpressionComparison #pragma warning disable CS1718 // Comparison made to same variable Assert.That(rv == rv, Is.True); #pragma warning restore CS1718 // Comparison made to same variable Assert.That(rv.Equals(rv), Is.True); - // They contains the same values, but the collections do not point to the same object reference + // They contain the same values, but the collections do not point to the same object reference Assert.That(rv == rv2, Is.False); Assert.That(rv.Equals(rv2), Is.False); @@ -633,12 +634,13 @@ public void Dictionary_InRealmValue_Equality([Values(true, false)] bool isManage rv = PersistAndFind(rv).RealmValueProperty; } + // ReSharper disable once EqualExpressionComparison #pragma warning disable CS1718 // Comparison made to same variable Assert.That(rv == rv, Is.True); #pragma warning restore CS1718 // Comparison made to same variable Assert.That(rv.Equals(rv), Is.True); - // They contains the same values, but the collections do not point to the same object reference + // They contain the same values, but the collections do not point to the same object reference Assert.That(rv == rv2, Is.False); Assert.That(rv.Equals(rv2), Is.False); diff --git a/Tests/Realm.Tests/Database/TestObjects.cs b/Tests/Realm.Tests/Database/TestObjects.cs index ddfee7695a..173c8ef524 100644 --- a/Tests/Realm.Tests/Database/TestObjects.cs +++ b/Tests/Realm.Tests/Database/TestObjects.cs @@ -16,12 +16,6 @@ // //////////////////////////////////////////////////////////////////////////// -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using MongoDB.Bson; -using Realms.Tests.Database; #if TEST_WEAVER using TestEmbeddedObject = Realms.EmbeddedObject; using TestRealmObject = Realms.RealmObject; @@ -29,6 +23,12 @@ using TestEmbeddedObject = Realms.IEmbeddedObject; using TestRealmObject = Realms.IRealmObject; #endif +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using MongoDB.Bson; +using Realms.Tests.Database; namespace Realms.Tests { @@ -522,7 +522,7 @@ public partial class SyncAllTypesObject : TestRealmObject { [MapTo("_id")] [PrimaryKey] - public ObjectId Id { get; private set; } = ObjectId.GenerateNewId(); + public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); public char CharProperty { get; set; } diff --git a/Tests/Realm.Tests/Sync/DataTypeSynchronizationTests.cs b/Tests/Realm.Tests/Sync/DataTypeSynchronizationTests.cs index 0beb8fd7e0..310af261d8 100644 --- a/Tests/Realm.Tests/Sync/DataTypeSynchronizationTests.cs +++ b/Tests/Realm.Tests/Sync/DataTypeSynchronizationTests.cs @@ -24,7 +24,6 @@ using System.Threading.Tasks; using MongoDB.Bson; using NUnit.Framework; -using NUnit.Framework.Constraints; using Realms.Extensions; using Realms.Helpers; using Realms.Tests.Database; @@ -861,7 +860,7 @@ public void NestedCollections_Merge() o.RealmValueProperty = new Dictionary { { "list", new List { 1, 2, 3 } }, - { "dictionary", new Dictionary() { { "key1", 1 } } }, + { "dictionary", new Dictionary { { "key1", 1 } } }, }; return o; }); @@ -903,20 +902,89 @@ public void NestedCollections_Merge() var list2 = obj1.RealmValueProperty.AsDictionary()["list"].AsList(); var dictionary2 = obj1.RealmValueProperty.AsDictionary()["dictionary"].AsDictionary(); - Assert.That(list1, new NotConstraint(Contains.Item((RealmValue)1))); + Assert.That(list1, Does.Not.Contain((RealmValue)1)); Assert.That(list1, Contains.Item((RealmValue)2)); Assert.That(list1, Contains.Item((RealmValue)3)); Assert.That(list1, Contains.Item((RealmValue)4)); Assert.That(list1, Contains.Item((RealmValue)5)); Assert.That(list1, Is.EqualTo(list2).Using(_rvComparer)); - Assert.That(dictionary1, new NotConstraint(Contains.Key("key1"))); + Assert.That(dictionary1, Does.Not.ContainKey("key1")); Assert.That(dictionary1, Contains.Key("key2")); Assert.That(dictionary1, Contains.Key("key3")); Assert.That(dictionary1, Is.EqualTo(dictionary2).Using(_rvComparer)); }); } + [Test] + public void NestedCollections_MergeNewObjects() + { + SyncTestHelpers.RunBaasTestAsync(async () => + { + var partition = ObjectId.GenerateNewId(); + + var realm1 = await GetFLXIntegrationRealmAsync(); + await realm1.All().Where(o => o.ObjectIdProperty == partition).SubscribeAsync(); + realm1.SyncSession.Stop(); + + var realm2 = await GetFLXIntegrationRealmAsync(); + await realm2.All().Where(o => o.ObjectIdProperty == partition).SubscribeAsync(); + realm2.SyncSession.Stop(); + + var id = ObjectId.GenerateNewId(); + realm1.Write(() => + { + realm1.Add(new SyncAllTypesObject + { + Id = id, + RealmValueProperty = new RealmValue[] { 5, "abc" }, + StringProperty = "client 1", + ObjectIdProperty = partition + }); + }); + + realm2.Write(() => + { + realm2.Add(new SyncAllTypesObject + { + Id = id, + RealmValueProperty = new RealmValue[] { 100, "def" }, + StringProperty = "client 2", + ObjectIdProperty = partition + }); + }); + + realm1.SyncSession.Start(); + realm2.SyncSession.Start(); + + await WaitForUploadAsync(realm1); + await WaitForUploadAsync(realm2); + await WaitForDownloadAsync(realm1); + await WaitForDownloadAsync(realm2); + + var objs1 = realm1.All(); + var objs2 = realm2.All(); + + Assert.That(objs1.Count(), Is.EqualTo(1)); + Assert.That(objs2.Count(), Is.EqualTo(1)); + + var obj1 = objs1.Single(); + var obj2 = objs2.Single(); + + Assert.That(obj1.RealmValueProperty.AsList().Count, Is.EqualTo(2)); + Assert.That(obj2.RealmValueProperty.AsList().Count, Is.EqualTo(2)); + + Assert.That(obj1.StringProperty, Is.EqualTo(obj2.StringProperty)); + + var expected = obj1.StringProperty == "client 1" + ? new RealmValue[] { 5, "abc" } + : new RealmValue[] { 100, "def" }; + + Assert.That(obj1.RealmValueProperty.AsList(), Is.EqualTo(expected)); + Assert.That(obj2.RealmValueProperty.AsList(), Is.EqualTo(expected)); + }); + } + private static RealmValue Clone(RealmValue original) { if (original.Type != RealmValueType.Object)