Skip to content

Commit

Permalink
Test collection listener for updates on embedded object (#3645)
Browse files Browse the repository at this point in the history
* Add Results notification test for updates on embedded object.

* Add generated file.
  • Loading branch information
elle-j authored Jul 17, 2024
1 parent 84940b3 commit 2d856d8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Tests/Realm.Tests/Database/NotificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,53 @@ public void ResultsShouldSendNotifications()
}
}

[Test]
public void Results_WhenEmbeddedObjectIsModified_Notifies()
{
var query = _realm.All<TestNotificationObject>();
var actualChanges = new List<ChangeSet?>();
void OnNotification(IRealmCollection<TestNotificationObject> collection, ChangeSet? changes) => actualChanges.Add(changes);

Assert.That(query.Count, Is.EqualTo(0));

using (query.SubscribeForNotifications(OnNotification))
{
_realm.Refresh();

// Notification from subscribing.
Assert.That(actualChanges.Count, Is.EqualTo(1));

var testObject = _realm.Write(() => _realm.Add(new TestNotificationObject()));

_realm.Refresh();
Assert.That(actualChanges.Count, Is.EqualTo(2));
Assert.That(actualChanges[1], Is.Not.Null);
Assert.That(actualChanges[1]!.InsertedIndices, Is.EquivalentTo(new[] { 0 }));

_realm.Write(() =>
{
testObject.EmbeddedObject = new EmbeddedIntPropertyObject { Int = 1 };
});

_realm.Refresh();
Assert.That(actualChanges.Count, Is.EqualTo(3));
Assert.That(actualChanges[2], Is.Not.Null);
Assert.That(actualChanges[2]!.NewModifiedIndices, Is.EquivalentTo(new[] { 0 }));
Assert.That(actualChanges[2]!.ModifiedIndices, Is.EquivalentTo(new[] { 0 }));

_realm.Write(() =>
{
testObject.EmbeddedObject!.Int++;
});

_realm.Refresh();
Assert.That(actualChanges.Count, Is.EqualTo(4));
Assert.That(actualChanges[3], Is.Not.Null);
Assert.That(actualChanges[3]!.NewModifiedIndices, Is.EquivalentTo(new[] { 0 }));
Assert.That(actualChanges[3]!.ModifiedIndices, Is.EquivalentTo(new[] { 0 }));
}
}

[Test]
public void ListShouldSendNotifications()
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Realm.Tests/Database/TestNotificationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ public partial class TestNotificationObject : TestRealmObject

[Backlink(nameof(LinkSameType))]
public IQueryable<TestNotificationObject> Backlink { get; } = null!;

public EmbeddedIntPropertyObject? EmbeddedObject { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d856d8

Please sign in to comment.