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-992] Add sync support for collections in mixed #3556

Merged
merged 21 commits into from
Apr 9, 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
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
Loading