Skip to content

Commit 2d856d8

Browse files
authored
Test collection listener for updates on embedded object (#3645)
* Add Results notification test for updates on embedded object. * Add generated file.
1 parent 84940b3 commit 2d856d8

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Tests/Realm.Tests/Database/NotificationTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,53 @@ public void ResultsShouldSendNotifications()
7777
}
7878
}
7979

80+
[Test]
81+
public void Results_WhenEmbeddedObjectIsModified_Notifies()
82+
{
83+
var query = _realm.All<TestNotificationObject>();
84+
var actualChanges = new List<ChangeSet?>();
85+
void OnNotification(IRealmCollection<TestNotificationObject> collection, ChangeSet? changes) => actualChanges.Add(changes);
86+
87+
Assert.That(query.Count, Is.EqualTo(0));
88+
89+
using (query.SubscribeForNotifications(OnNotification))
90+
{
91+
_realm.Refresh();
92+
93+
// Notification from subscribing.
94+
Assert.That(actualChanges.Count, Is.EqualTo(1));
95+
96+
var testObject = _realm.Write(() => _realm.Add(new TestNotificationObject()));
97+
98+
_realm.Refresh();
99+
Assert.That(actualChanges.Count, Is.EqualTo(2));
100+
Assert.That(actualChanges[1], Is.Not.Null);
101+
Assert.That(actualChanges[1]!.InsertedIndices, Is.EquivalentTo(new[] { 0 }));
102+
103+
_realm.Write(() =>
104+
{
105+
testObject.EmbeddedObject = new EmbeddedIntPropertyObject { Int = 1 };
106+
});
107+
108+
_realm.Refresh();
109+
Assert.That(actualChanges.Count, Is.EqualTo(3));
110+
Assert.That(actualChanges[2], Is.Not.Null);
111+
Assert.That(actualChanges[2]!.NewModifiedIndices, Is.EquivalentTo(new[] { 0 }));
112+
Assert.That(actualChanges[2]!.ModifiedIndices, Is.EquivalentTo(new[] { 0 }));
113+
114+
_realm.Write(() =>
115+
{
116+
testObject.EmbeddedObject!.Int++;
117+
});
118+
119+
_realm.Refresh();
120+
Assert.That(actualChanges.Count, Is.EqualTo(4));
121+
Assert.That(actualChanges[3], Is.Not.Null);
122+
Assert.That(actualChanges[3]!.NewModifiedIndices, Is.EquivalentTo(new[] { 0 }));
123+
Assert.That(actualChanges[3]!.ModifiedIndices, Is.EquivalentTo(new[] { 0 }));
124+
}
125+
}
126+
80127
[Test]
81128
public void ListShouldSendNotifications()
82129
{

Tests/Realm.Tests/Database/TestNotificationObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ public partial class TestNotificationObject : TestRealmObject
5858

5959
[Backlink(nameof(LinkSameType))]
6060
public IQueryable<TestNotificationObject> Backlink { get; } = null!;
61+
62+
public EmbeddedIntPropertyObject? EmbeddedObject { get; set; }
6163
}
6264
}

Tests/Realm.Tests/Generated/Realm.SourceGenerator/Realms.SourceGenerator.RealmGenerator/TestNotificationObject_generated.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)