Skip to content

Commit

Permalink
[RNET-992] Add sync support for collections in mixed (#3556)
Browse files Browse the repository at this point in the history
  • Loading branch information
rorbech authored Apr 9, 2024
1 parent 46fa89b commit 9eaca6c
Show file tree
Hide file tree
Showing 6 changed files with 636 additions and 33 deletions.
171 changes: 169 additions & 2 deletions Tests/Realm.Tests/Database/RealmValueWithCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,87 @@ public void List_AfterCreation_CanBeAssigned([Values(true, false)] bool isManage
Assert.That(rvo.RealmValueProperty == newStringVal);
}

[Test]
public void List_AfterCreation_CanBeReassigned([Values(true, false)] bool isManaged)
{
var initialList = (RealmValue)new List<RealmValue> { 1, 2, 3 };
var rvo = new RealmValueObject { RealmValueProperty = initialList };

if (isManaged)
{
_realm.Write(() =>
{
_realm.Add(rvo);
});
}

var actualList = rvo.RealmValueProperty;
Assert.That(initialList, Is.EqualTo(actualList).Using(_rvComparer));

var updatedList = (RealmValue)new List<RealmValue> { 4, 5 };
_realm.Write(() =>
{
rvo.RealmValueProperty = updatedList;
});

actualList = rvo.RealmValueProperty;
Assert.That(updatedList, Is.EqualTo(actualList).Using(_rvComparer));
}

[Test]
public void List_AfterCreation_EmbeddedListCanBeReassigned([Values(true, false)] bool isManaged)
{
var initialList = (RealmValue)new List<RealmValue> { new List<RealmValue> { 1, 2, 3 } };
var rvo = new RealmValueObject { RealmValueProperty = new List<RealmValue> { initialList } };

if (isManaged)
{
_realm.Write(() =>
{
_realm.Add(rvo);
});
}

var actualEmbeddedList = rvo.RealmValueProperty.AsList()[0];
Assert.That(initialList, Is.EqualTo(actualEmbeddedList).Using(_rvComparer));

var updatedList = (RealmValue)new List<RealmValue> { 4, 5, 6 };
_realm.Write(() =>
{
rvo.RealmValueProperty.AsList()[0] = updatedList;
});

actualEmbeddedList = rvo.RealmValueProperty.AsList()[0];
Assert.That(updatedList, Is.EqualTo(actualEmbeddedList).Using(_rvComparer));
}

[Test]
public void List_AfterCreation_EmbeddedDictionaryCanBeReassigned([Values(true, false)] bool isManaged)
{
var initialDictionary = (RealmValue)new Dictionary<string, RealmValue> { { "key1", 1 } };
var rvo = new RealmValueObject { RealmValueProperty = new List<RealmValue> { initialDictionary } };

if (isManaged)
{
_realm.Write(() =>
{
_realm.Add(rvo);
});
}

var actualDictionary = rvo.RealmValueProperty.AsList()[0];
Assert.That(initialDictionary, Is.EqualTo(actualDictionary).Using(_rvComparer));

var updatedDictionary = (RealmValue)new Dictionary<string, RealmValue> { { "key2", 2 } };
_realm.Write(() =>
{
rvo.RealmValueProperty.AsList()[0] = updatedDictionary;
});

actualDictionary = rvo.RealmValueProperty.AsList()[0];
Assert.That(updatedDictionary, Is.EqualTo(actualDictionary).Using(_rvComparer));
}

[Test]
public void List_WhenManaged_CanBeModified()
{
Expand Down Expand Up @@ -451,7 +532,6 @@ public void List_WhenManaged_WorksWithNotifications()

callbacks.Clear();
}

#endregion

#region Dictionary
Expand Down Expand Up @@ -651,6 +731,93 @@ public void Dictionary_AfterCreation_CanBeAssigned([Values(true, false)] bool is
Assert.That(rvo.RealmValueProperty == newStringVal);
}

[Test]
public void Dictionary_AfterCreation_CanBeReassigned([Values(true, false)] bool isManaged)
{
var initialDictionary = (RealmValue)new Dictionary<string, RealmValue> { { "key1", 1 } };
var rvo = new RealmValueObject { RealmValueProperty = initialDictionary };

if (isManaged)
{
_realm.Write(() =>
{
_realm.Add(rvo);
});
}

var actualDictionary = rvo.RealmValueProperty;
Assert.That(initialDictionary, Is.EqualTo(actualDictionary).Using(_rvComparer));

var updatedDictionary = (RealmValue)new Dictionary<string, RealmValue> { { "key2", 2 } };
_realm.Write(() =>
{
rvo.RealmValueProperty = updatedDictionary;
});

actualDictionary = rvo.RealmValueProperty;
Assert.That(updatedDictionary, Is.EqualTo(actualDictionary).Using(_rvComparer));
}

[Test]
public void Dictionary_AfterCreation_EmbeddedListCanBeReassigned([Values(true, false)] bool isManaged)
{
var initialList = new List<RealmValue> { new List<RealmValue> { 1, 2, 3 } };
var rvo = new RealmValueObject
{
RealmValueProperty = new Dictionary<string, RealmValue> { { "key", initialList } }
};

if (isManaged)
{
_realm.Write(() =>
{
_realm.Add(rvo);
});
}

var actualEmbeddedList = rvo.RealmValueProperty.AsDictionary()["key"].AsList();
Assert.That(initialList, Is.EqualTo(actualEmbeddedList).Using(_rvComparer));

var updatedList = (RealmValue)new List<RealmValue> { 4, 5, 6 };
_realm.Write(() =>
{
rvo.RealmValueProperty.AsDictionary()["key"] = updatedList;
});

actualEmbeddedList = rvo.RealmValueProperty.AsDictionary()["key"].AsList();
Assert.AreEqual(updatedList.AsList().Count, actualEmbeddedList.Count);
}

[Test]
public void Dict_AfterCreation_EmbeddedDictionaryCanBeReassigned([Values(true, false)] bool isManaged)
{
var embeddedDictionary = new Dictionary<string, RealmValue> { { "key1", 1 } };
var rvo = new RealmValueObject
{
RealmValueProperty = new Dictionary<string, RealmValue> { { "key", embeddedDictionary } }
};

if (isManaged)
{
_realm.Write(() =>
{
_realm.Add(rvo);
});
}

var actualEmbedded = rvo.RealmValueProperty.AsDictionary()["key"].AsDictionary();
Assert.That(embeddedDictionary, Is.EqualTo(actualEmbedded).Using(_rvComparer));

var updatedDictionary = new Dictionary<string, RealmValue> { { "key2", 2 } };
_realm.Write(() =>
{
rvo.RealmValueProperty.AsDictionary()["key"] = updatedDictionary;
});

actualEmbedded = rvo.RealmValueProperty.AsDictionary()["key"].AsDictionary();
Assert.That(updatedDictionary, Is.EqualTo(actualEmbedded).Using(_rvComparer));
}

[Test]
public void Dictionary_WhenManaged_CanBeModified()
{
Expand Down Expand Up @@ -958,7 +1125,7 @@ public void IndexedRealmValue_WithCollection_BasicTest()

#endregion

private class RealmValueComparer : IEqualityComparer<RealmValue>
internal class RealmValueComparer : IEqualityComparer<RealmValue>
{
public bool Equals(RealmValue x, RealmValue y)
{
Expand Down
Loading

0 comments on commit 9eaca6c

Please sign in to comment.