Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNET-1156: Add test for merging new objects #3652

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions Tests/Realm.Tests/Database/RealmValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<byte>() };
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<byte>() };
public static IRealmObject[] ObjectValues = { new InternalObject { IntProperty = 10, StringProperty = "brown" } };

[Test]
public void CharTests(
Expand Down Expand Up @@ -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(() =>
{
Expand All @@ -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(() =>
{
Expand Down Expand Up @@ -717,7 +719,7 @@ public void RealmValue_WhenManaged_NotificationTests()
{
var notifiedPropertyNames = new List<string?>();

var handler = new PropertyChangedEventHandler((sender, e) =>
var handler = new PropertyChangedEventHandler((_, e) =>
{
notifiedPropertyNames.Add(e.PropertyName);
});
Expand Down Expand Up @@ -759,7 +761,7 @@ public void RealmValue_WhenManaged_BoolNotificationTests([Values(0, 1)] int intV
{
var notifiedPropertyNames = new List<string?>();

var handler = new PropertyChangedEventHandler((sender, e) =>
var handler = new PropertyChangedEventHandler((_, e) =>
{
notifiedPropertyNames.Add(e.PropertyName);
});
Expand Down Expand Up @@ -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}";
}
}
12 changes: 7 additions & 5 deletions Tests/Realm.Tests/Database/RealmValueWithCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ internal class RealmValueWithCollections : RealmInstanceTest
{
private readonly RealmValueComparer _rvComparer = new();

public static Func<int, List<RealmValue>> ListGenerator = i => new List<RealmValue> { $"inner{i}", i };
public static Func<int, Dictionary<string, RealmValue>> DictGenerator = i => new Dictionary<string, RealmValue> { { "s1", i }, { "s2", $"ah{i}" } };
private static readonly Func<int, List<RealmValue>> ListGenerator = i => new List<RealmValue> { $"inner{i}", i };
private static readonly Func<int, Dictionary<string, RealmValue>> DictGenerator = i => new Dictionary<string, RealmValue> { { "s1", i }, { "s2", $"ah{i}" } };

public static Func<int, RealmValue>[] CollectionGenerators = new Func<int, RealmValue>[]
public static Func<int, RealmValue>[] CollectionGenerators =
{
i => (RealmValue)ListGenerator(i),
i => (RealmValue)DictGenerator(i),
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions Tests/Realm.Tests/Database/TestObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
//
////////////////////////////////////////////////////////////////////////////

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;
#else
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
{
Expand Down Expand Up @@ -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; }

Expand Down
76 changes: 72 additions & 4 deletions Tests/Realm.Tests/Sync/DataTypeSynchronizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -861,7 +860,7 @@
o.RealmValueProperty = new Dictionary<string, RealmValue>
{
{ "list", new List<RealmValue> { 1, 2, 3 } },
{ "dictionary", new Dictionary<string, RealmValue>() { { "key1", 1 } } },
{ "dictionary", new Dictionary<string, RealmValue> { { "key1", 1 } } },
};
return o;
});
Expand Down Expand Up @@ -903,20 +902,89 @@
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));

Check failure on line 905 in Tests/Realm.Tests/Sync/DataTypeSynchronizationTests.cs

View workflow job for this annotation

GitHub Actions / Package Unity

Argument 1: cannot convert from 'Realms.RealmValue' to 'string' [D:\a\realm-dotnet\realm-dotnet\Tests\Realm.Tests\Realm.Tests.csproj::TargetFramework=netstandard2.0]
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"));

Check failure on line 912 in Tests/Realm.Tests/Sync/DataTypeSynchronizationTests.cs

View workflow job for this annotation

GitHub Actions / Package Unity

'ConstraintExpression' does not contain a definition for 'ContainKey' and no accessible extension method 'ContainKey' accepting a first argument of type 'ConstraintExpression' could be found (are you missing a using directive or an assembly reference?) [D:\a\realm-dotnet\realm-dotnet\Tests\Realm.Tests\Realm.Tests.csproj::TargetFramework=netstandard2.0]
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<SyncAllTypesObject>().Where(o => o.ObjectIdProperty == partition).SubscribeAsync();
realm1.SyncSession.Stop();

var realm2 = await GetFLXIntegrationRealmAsync();
await realm2.All<SyncAllTypesObject>().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<SyncAllTypesObject>();
var objs2 = realm2.All<SyncAllTypesObject>();

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)
Expand Down
Loading